10. Replication: High Availability
Building Highly Available Redis Deployments
Redis replication provides high availability and read scaling. A master-replica setup ensures data redundancy. Replicas can serve read-only queries. This module covers replication architecture, configuration, and failover strategies for building highly available Redis deployments.
Replication is essential for production deployments. It provides data redundancy and high availability. Replicas automatically sync with the master. If the master fails, a replica can be promoted. Replication also enables read scaling — replicas can serve read queries, reducing load on the master. Understanding replication is essential for building reliable Redis deployments.
This module covers replication architecture, configuration, monitoring, and failover strategies with practical examples.
Replication Architecture
Redis replication is asynchronous. The master sends data to replicas. Replicas apply updates asynchronously. Replication uses a replication ID and offset for consistency. Replicas can be read-only. This architecture provides data redundancy and read scalability.
When a replica connects to a master, it performs a full synchronization (RDB transfer) followed by incremental updates. The replication ID identifies the dataset. The offset tracks the position in the replication stream. This ensures replicas stay consistent with the master. Replication is reliable and efficient for production use.
# Master configuration (redis.conf) port 6379 # Replica configuration (redis.conf) port 6380 replicaof 127.0.0.1 6379 # Replica with read-only (default) replica-read-only yes # Check replication status INFO replication # On master: # role:master # connected_slaves:1 # slave0:ip=127.0.0.1,port=6380,state=online,offset=12345,lag=0 # On replica: # role:slave # master_host:127.0.0.1 # master_port:6379 # master_link_status:up
Handling Failover
Failover is the process of switching from a failed master to a replica. Redis supports manual failover with the REPLICAOF command. Redis Sentinel provides automated failover with monitoring and election. Redis Cluster provides built-in failover. Choose the right failover strategy for your deployment.
Manual failover is simple but requires human intervention. Sentinel provides automated failover without human intervention, but requires configuration. Cluster provides built-in failover with automatic detection. For production deployments, use Sentinel or Cluster for automated failover.
# Manual failover # On replica: Promote to master REPLICAOF NO ONE # On old master (after recovery): Demote to replica REPLICAOF new_master_ip new_master_port # Redis Sentinel configuration (sentinel.conf) sentinel monitor mymaster 127.0.0.1 6379 2 sentinel down-after-milliseconds mymaster 5000 sentinel failover-timeout mymaster 60000 sentinel parallel-syncs mymaster 1 # Start Sentinel redis-sentinel sentinel.conf
Monitoring Replication
Monitoring replication is essential for reliability. Use INFO replication to check replication status. Monitor replication lag. Check for disconnected replicas. Use monitoring tools like Redis Insight. Replication monitoring ensures data consistency and availability.
Key metrics to monitor: master_link_status (up/down), master_last_io_seconds_ago, connected_slaves, slave_lag. If replication lag grows, investigate network or performance issues. If replicas disconnect, investigate network stability. Regular monitoring prevents replication issues from becoming critical.
- INFO replication: Check replication status and lag.
- Replication Lag: Monitor delay between master and replicas.
- Disconnected Replicas: Check for disconnected replicas.
- Master Link Status: Monitor master-replica connection health.
- Redis Insight: GUI for monitoring replication.
Knowledge Check
Ready to test your understanding of 10. Replication: High Availability?