Stage 3 · Build
Redis
Redis Replication & Sentinel
Primary/replica replication, Redis Sentinel for HA, and automatic failover.
How Redis Replication Works
Redis uses asynchronous primary-replica replication. The primary receives writes and propagates them to replicas. Replicas can serve read queries, scaling read throughput. If the primary fails, a replica can be promoted to primary.
| Feature | Behavior |
|---|---|
| Replication type | Asynchronous (primary does not wait for replica ACK) |
| Data safety | Can lose writes if primary crashes before propagation |
| Read scaling | Replicas serve read queries |
| Write scaling | Not supported — only primary accepts writes |
| Replica of replica | Supported (cascading replication) |
Setting Up Replication
# On the replica — configure primary connection
REPLICAOF 10.0.0.1 6379
# Or in redis.conf
replicaof 10.0.0.1 6379
# Optional: set replica to read-only
replica-read-only yes
# Optional: set primary password if authenticated
masterauth your_password_here
# Verify replication on primary
INFO replication
# role:master
# connected_slaves:2
# slave0:ip=10.0.0.2,port=6379,state=online,offset=12345,lag=0
# slave1:ip=10.0.0.3,port=6379,state=online,offset=12344,lag=0
# Verify on replica
INFO replication
# role:slave
# master_host:10.0.0.1
# master_port:6379
# master_link_status:upREPLICAOF is dynamic — you can change the primary without restarting the replica. The replica performs a full synchronization first (RDB transfer), then switches to incremental propagation (receiving write commands).
Redis primary does not wait for replicas to acknowledge writes. If the primary crashes, unacknowledged writes are lost. For critical data, use WAIT command to block until replicas acknowledge, or use Redis Cluster with synchronous writes.
Replication Internals
# On primary: check replication buffer
INFO replication
# repl_backlog_active:1
# repl_backlog_size:1048576
# repl_backlog_first_byte_offset:12345
# repl_backlog_histlen:67890
# On replica: check replication delay
INFO replication
# master_repl_offset:123456
# slave_repl_offset:123456
# master_link_down_since_seconds:0
# Force full resynchronization (use with caution)
DEBUG RELOADThe replication backlog is a circular buffer on the primary. If a replica disconnects and reconnects within the backlog window, it can resume from where it left off. If it falls outside the backlog, a full resync is required.
Redis Sentinel
Redis Sentinel provides high availability for Redis. It monitors primary and replica instances, detects failures, and automatically promotes a replica to primary. Sentinel also notifies clients about topology changes.
| Component | Role |
|---|---|
| Sentinel process | Monitors instances, coordinates failover |
| Primary | Accepts writes, propagates to replicas |
| Replica | Read-only copy, candidate for promotion |
| Client | Discovers primary through Sentinel |
Sentinel Configuration
# sentinel.conf — run 3 Sentinels on separate machines
# Monitor primary named "mymaster"
sentinel monitor mymaster 10.0.0.1 6379 2
# primary_ip port quorum
# quorum=2 means 2 Sentinels must agree for failover
# How long to wait before declaring primary down
sentinel down-after-milliseconds mymaster 5000
# Failover timeout
sentinel failover-timeout mymaster 60000
# Number of replicas to reconfigure during failover
sentinel parallel-syncs mymaster 1
# Sentinel data directory
dir /var/lib/sentinelRun at least 3 Sentinels for quorum. The quorum setting (2 in this example) means at least 2 Sentinels must agree that the primary is down before failover begins. This prevents split-brain scenarios.
Failover Process
# 1. Sentinels detect primary is down (no PONG within down-after-milliseconds)
# 2. Sentinels elect a leader (Raft-like consensus)
# 3. Leader selects best replica for promotion:
# - Priority (replica-priority config)
# - Replication offset (most up-to-date)
# - Run ID (deterministic tiebreaker)
# 4. Selected replica is promoted to primary
# 5. Other replicas are reconfigured to replicate from new primary
# 6. Old primary is reconfigured as replica when it recovers
# Client connection pattern
redis-cli -p 26379 SENTINEL get-master-addr-by-name mymaster
# 1) "10.0.0.2" (new primary IP)
# 2) "6379"
# Force manual failover
redis-cli -p 26379 SENTINEL failover mymasterThe failover process is automatic. Clients should use Sentinel to discover the current primary rather than hardcoding IP addresses. Most Redis client libraries have built-in Sentinel support.
Sentinel is the standard solution for Redis HA. It handles primary discovery, failover, and client notification. For Redis Cluster, failover is built into the cluster protocol itself — Sentinel is not needed.
During a network partition, Sentinel may promote a new primary while the old primary is still accepting writes. When the partition heals, the old primary's writes are lost. This is the tradeoff of asynchronous replication.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.