Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:BerriAI Litellm Redis Client

From Leeroopedia
Revision as of 12:10, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/BerriAI_Litellm_Redis_Client.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Attribute Value
Sources litellm/_redis.py
Domains Caching, Redis, Infrastructure, Authentication
last_updated 2026-02-15 16:00 GMT

Overview

The Redis Client module provides factory functions for creating sync and async Redis clients with support for standalone, cluster, sentinel, URL-based, and GCP IAM authentication topologies.

Description

This module centralizes all Redis client instantiation logic for LiteLLM. It abstracts away the complexity of different Redis deployment topologies and authentication mechanisms by providing a unified interface.

Key capabilities:

  • Environment-based configuration -- Reads REDIS_* environment variables and maps them to redis.Redis constructor arguments.
  • Multiple topologies -- Supports standalone Redis, Redis Cluster (via startup_nodes), Redis Sentinel (via sentinel_nodes + service_name), and URL-based connections.
  • GCP IAM authentication -- Generates short-lived access tokens via google.cloud.iam_credentials_v1 for Redis instances protected by GCP IAM. Creates custom redis_connect_func callbacks.
  • Sync and async clients -- get_redis_client() returns a synchronous redis.Redis; get_redis_async_client() returns async_redis.Redis or async_redis.RedisCluster.
  • Connection pooling -- get_redis_connection_pool() creates an async_redis.BlockingConnectionPool with configurable timeout.
  • Debug output -- _pretty_print_redis_config() uses rich to display masked Redis configuration when debug logging is active.

The internal function _get_redis_client_logic() performs argument resolution, environment variable interpolation (supports os.environ/KEY syntax), and topology detection before returning a kwargs dict consumed by the public factory functions.

Usage

Import and call the factory functions when establishing Redis connections in the caching or proxy subsystems:

from litellm._redis import get_redis_client, get_redis_async_client

Code Reference

Source Location

/litellm/_redis.py (562 lines)

Key Functions

Function Signature Purpose
get_redis_client def get_redis_client(**env_overrides) -> redis.Redis Creates a synchronous Redis client
get_redis_async_client def get_redis_async_client(connection_pool=None, **env_overrides) -> Union[async_redis.Redis, async_redis.RedisCluster] Creates an async Redis client
get_redis_connection_pool def get_redis_connection_pool(**env_overrides) -> async_redis.BlockingConnectionPool Creates an async connection pool
get_redis_url_from_environment def get_redis_url_from_environment() -> str Constructs a Redis URL from env vars
init_redis_cluster def init_redis_cluster(redis_kwargs) -> redis.RedisCluster Initializes a sync Redis Cluster client
create_gcp_iam_redis_connect_func def create_gcp_iam_redis_connect_func(service_account: str, ssl_ca_certs: Optional[str] = None) -> Callable Creates a GCP IAM connection function
_get_redis_client_logic def _get_redis_client_logic(**env_overrides) -> dict Core argument resolution logic

Import

from litellm._redis import get_redis_client, get_redis_async_client, get_redis_connection_pool

I/O Contract

Inputs

Parameter Type Description
**env_overrides dict Key-value pairs that override environment-based configuration (host, port, url, password, startup_nodes, etc.)
connection_pool Optional[async_redis.BlockingConnectionPool] Pre-created connection pool for async client

Outputs

Return Type Description
Sync client redis.Redis or redis.RedisCluster A connected synchronous Redis client
Async client async_redis.Redis or async_redis.RedisCluster A connected asynchronous Redis client
Connection pool async_redis.BlockingConnectionPool A reusable async connection pool

Raises ValueError if neither host nor url can be determined.

Usage Examples

from litellm._redis import get_redis_client, get_redis_async_client

# Standalone Redis from environment variables
client = get_redis_client()

# Override with explicit parameters
client = get_redis_client(host="redis.example.com", port=6379, password="secret")

# Async client with URL
async_client = get_redis_async_client(url="redis://localhost:6379/0")

# Redis Cluster
import json
cluster_client = get_redis_client(
    startup_nodes=json.dumps([{"host": "node1", "port": 6379}])
)

# GCP IAM authenticated Redis
client = get_redis_client(
    gcp_service_account="projects/-/serviceAccounts/redis@my-project.iam.gserviceaccount.com",
    host="redis-host",
    port=6379,
    ssl=True,
)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment