Difficulty: Intermediate
How does Redis replication work, from the initial sync to steady state, and what consistency guarantees does it give?
Redis uses asynchronous primary-replica replication (older docs say master-slave). A replica connects to the primary, receives a full snapshot on first sync, and then gets a continuous stream of every write command the primary executes. Because the primary acknowledges writes to clients before replicas confirm them, replicas can lag and a failover can lose the most recent writes.
REPLICAOF 10.0.0.5 6379 on the replica (or set replicaof in config). SLAVEOF still works as an alias.PSYNC <replication-id> <offset>. On the first connection it has neither, so it asks for a full sync.repl-diskless-sync yes (the default since 7.0) it streams the RDB straight over the socket instead of writing it to disk first. Meanwhile, new writes are buffered for that replica.If the network blips, a full resync of a 20 GB dataset would be expensive. The primary keeps a circular replication backlog (repl-backlog-size, default 1 MB). When the replica reconnects with its replication ID and offset, and that offset is still inside the backlog, the primary sends only the missing bytes. If the gap is larger than the backlog, a full sync happens. On busy systems, raise the backlog to cover a realistic outage window, for example a few hundred MB.
| Gives you | Does not give you |
|---|---|
| Read scaling: send reads to replicas | Strong consistency: replicas can be stale |
| Data redundancy on another machine | Automatic failover by itself |
| A place to run BGSAVE without hurting the primary | Write scaling: all writes still go to one primary |
| Fast recovery after a primary restart | Protection from bad writes, they replicate too |
For automatic failover you add Redis Sentinel (for a single primary setup) or use Redis Cluster, which has replication and failover built in per shard. Managed services like ElastiCache and Memorystore handle this for you.
WAIT numreplicas timeout blocks the client until that many replicas have acknowledged the previous writes. It reduces the loss window but does not make Redis strongly consistent; a failover can still pick a replica that did not get the write.min-replicas-to-write 1 with min-replicas-max-lag 10 makes the primary refuse writes if no replica is reasonably caught up, limiting how much data an isolated primary can accept.replica-read-only yes (default) prevents accidental writes to replicas.master_link_status and lag in INFO replication.# On the replica
redis-cli -p 6380 REPLICAOF 127.0.0.1 6379
# Check status on the replica
redis-cli -p 6380 INFO replication | grep -E 'role|master_link_status|master_repl_offset'
# Check replicas and their lag on the primary
redis-cli -p 6379 INFO replication
# Wait until at least 1 replica has the previous writes (max 100 ms)
redis-cli -p 6379 SET payment:991 captured
redis-cli -p 6379 WAIT 1 100
# Promote the replica manually
redis-cli -p 6380 REPLICAOF NO ONE
INFO replication still prints role:slave for compatibility. WAIT returns how many replicas acknowledged, which may be fewer than requested if the timeout hits.
# redis.conf on the primary
repl-backlog-size 256mb # cover longer network blips
repl-diskless-sync yes # stream RDB over socket (default since 7.0)
min-replicas-to-write 1 # refuse writes if no replica is healthy
min-replicas-max-lag 10 # seconds
# redis.conf on the replica
replicaof 10.0.0.5 6379
replica-read-only yes
masterauth s3cret
A bigger backlog avoids full resyncs. The min-replicas settings limit how much data an isolated primary can accept.
Primary-Replica Replication, PSYNC, Replication Backlog, Asynchronous Replication, WAIT Command