Redis
Setting up Redis for caching, session storage, and horizontal scaling in LibreChat
This guide covers how to configure Redis for caching and session storage in LibreChat. Redis provides significant performance improvements and is required for horizontal scaling—if you're running multiple LibreChat instances behind a load balancer, Redis ensures consistent state across all instances.
Table of Contents
- Basic Setup
- Connection Types
- Security Configuration
- Advanced Options
- Performance Tuning
- Configuration Examples
- Resumable Streams
Basic Setup
Enable Redis
To enable Redis in LibreChat, set the following environment variable in your .env file:
Important: When USE_REDIS=true, you must also provide a REDIS_URI. The application will throw an error if Redis is enabled without a connection URI.
Connection Types
Single Redis Instance
For a standard single Redis server setup:
Redis Cluster
For Redis cluster deployments with multiple nodes:
The application automatically detects cluster mode when multiple URIs are provided.
If your redis cluster only has a single URI, you can use the USE_REDIS_CLUSTER environment variable to enable cluster mode:
Redis with TLS/SSL
For secure Redis connections:
Security Configuration
Authentication
Configure Redis authentication credentials:
Note: Separate username/password variables override credentials in the URI if both are provided.
TLS Configuration
For encrypted connections:
TLS with Elasticache
Elasticache may need to use an alternate dnsLookup for TLS connections. see "Special Note: Aws Elasticache Clusters with TLS" on this webpage: https://www.npmjs.com/package/ioredis
Advanced Options
Key Prefixing
Redis key prefixing prevents cross-deployment contamination by isolating cache data between different environments, versions, or instances sharing the same Redis server. This is essential for:
- Multi-tenant deployments: Separate staging, production, and development environments
- Blue-green deployments: Isolate cache between different application versions
Important: You cannot set both REDIS_KEY_PREFIX_VAR and REDIS_KEY_PREFIX simultaneously.
Examples of contamination without prefixing:
- Production cache overwritten by staging deployment
- Feature branch tests corrupting main branch cache
- Old deployment versions serving stale cached data
Key prefixing format:
- IoRedis client:
{prefix}::{key} - Keyv client: Handled by the store layer
Connection Limits
Configure Redis connection limits:
Connection Keep-Alive
Configure Redis ping intervals to maintain connections:
Important:
- Setting
REDIS_PING_INTERVAL=0or omitting it disables pinging entirely - Only set a positive value (in seconds) if you experience connection timeout issues
- The interval is specified in seconds and applies to both IoRedis and Keyv Redis clients
- Example values:
300(5 minutes),600(10 minutes),60(1 minute)
Selective In-Memory Caching
Force specific cache namespaces to use in-memory storage even when Redis is enabled:
Valid cache keys are defined in the CacheKeys enum from librechat-data-provider.
Performance Tuning
Connection Keep-Alive
The application implements configurable connection keep-alive:
- Ping intervals are controlled by
REDIS_PING_INTERVALenvironment variable - Default behavior: No pinging (recommended for most deployments)
- When enabled, pings both IoRedis and Keyv Redis clients at the specified interval
- Automatically clears ping intervals on disconnect/close events
Cache Strategy
The application uses a dual-client approach:
- IoRedis client: Primary Redis operations with automatic prefixing
- Keyv Redis client: Store-layer operations with prefix handling in
cacheFactory.js
Memory Optimization
Use FORCED_IN_MEMORY_CACHE_NAMESPACES to optimize performance by keeping frequently accessed, small datasets in memory while using Redis for larger caches.
Configuration Examples
Development Setup
Production Setup
Cluster Setup
Resumable Streams
Redis enables Resumable Streams for horizontally scaled deployments. When enabled, AI responses can seamlessly reconnect and resume across server instances.
Important: When USE_REDIS=true, resumable streams automatically use Redis for cross-instance coordination. This is the recommended setup for horizontally scaled deployments where users might connect to different server instances.
Note: If you're running a single LibreChat instance, Redis for resumable streams is typically overkill—the built-in in-memory mode works fine. Redis becomes essential when you have multiple LibreChat instances behind a load balancer, where a user's reconnection might hit a different server than where their stream started.
Configuration
Key Benefits (for Horizontal Scaling)
- Cross-instance continuity: Users can start a generation on one server and resume on another
- Rolling deployments: Active streams survive server restarts
- Multi-tab sync: Same conversation syncs across multiple browser tabs in a load-balanced environment
- Connection resilience: Automatic reconnection regardless of which server handles the request
Cluster Configuration
For Redis Cluster deployments, LibreChat automatically uses hash-tagged keys to ensure stream operations stay within the same cluster slot:
See Resumable Streams for more details on this feature.
How is this guide?