Implementation:BerriAI Litellm Redis Client: Difference between revisions
Auto-imported from implementations/BerriAI_Litellm_Redis_Client.md |
Sync from local file |
||
| Line 125: | Line 125: | ||
== Related Pages == | == Related Pages == | ||
* [[BerriAI_Litellm_Constants]] - defines <code>REDIS_SOCKET_TIMEOUT</code> and <code>REDIS_CONNECTION_POOL_TIMEOUT</code> | * [[Implementation:BerriAI_Litellm_Constants]] - defines <code>REDIS_SOCKET_TIMEOUT</code> and <code>REDIS_CONNECTION_POOL_TIMEOUT</code> | ||
* [[BerriAI_Litellm_Logging_Setup]] - provides <code>verbose_logger</code> used for debug output | * [[Implementation:BerriAI_Litellm_Logging_Setup]] - provides <code>verbose_logger</code> used for debug output | ||
* [[BerriAI_Litellm_Service_Logger]] - monitors Redis call success/failure | * [[Implementation:BerriAI_Litellm_Service_Logger]] - monitors Redis call success/failure | ||
[[Category:Implementations]] | [[Category:Implementations]] | ||
Latest revision as of 10:34, 27 September 2026
| 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 toredis.Redisconstructor arguments. - Multiple topologies -- Supports standalone Redis, Redis Cluster (via
startup_nodes), Redis Sentinel (viasentinel_nodes+service_name), and URL-based connections. - GCP IAM authentication -- Generates short-lived access tokens via
google.cloud.iam_credentials_v1for Redis instances protected by GCP IAM. Creates customredis_connect_funccallbacks. - Sync and async clients --
get_redis_client()returns a synchronousredis.Redis;get_redis_async_client()returnsasync_redis.Redisorasync_redis.RedisCluster. - Connection pooling --
get_redis_connection_pool()creates anasync_redis.BlockingConnectionPoolwith configurable timeout. - Debug output --
_pretty_print_redis_config()usesrichto 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
- Implementation:BerriAI_Litellm_Constants - defines
REDIS_SOCKET_TIMEOUTandREDIS_CONNECTION_POOL_TIMEOUT - Implementation:BerriAI_Litellm_Logging_Setup - provides
verbose_loggerused for debug output - Implementation:BerriAI_Litellm_Service_Logger - monitors Redis call success/failure