The MultiDBClient allows your application to connect to multiple Redis databases (typically replicas) to support Active-Active setups. It monitors database health and automatically fails over to the next highest-weighted healthy database when a failure is detected. It can also automatically switch back to a higher-weighted database once it becomes healthy again.
Key Concepts
- Weight: Each database has a priority. The client prefers the highest-weight healthy database.
- Circuit Breaker: Protects databases using states:
CLOSED (healthy), OPEN (unhealthy), and HALF_OPEN (probing). - Health Checks (Proactive): Background checks (defaulting to
PING) that run at intervals to detect issues before they affect traffic. - Failure Detection (Reactive): Monitors organic command failures over a moving window to trigger failover based on real-time error rates.
- Auto Fallback: If configured, the client periodically checks if a higher-weighted database has recovered to switch back to it.
- Pub/Sub: The client automatically re-subscribes to channels when a failover occurs.
MultiDBClient is designed to be a drop-in replacement for Redis or RedisCluster clients, sharing the same API.
from redis.multidb.client import MultiDBClient
from redis.multidb.config import MultiDbConfig, DatabaseConfig
cfg = MultiDbConfig(
databases_config=[
DatabaseConfig(from_url="redis://db-primary:6379/0", weight=1.0),
DatabaseConfig(from_url="redis://db-secondary:6379/0", weight=0.5),
]
)
client = MultiDBClient(cfg)
client.set("key", "value")
print(client.get("key"))