Asynchronous leader-follower replication: a command stream, partial resync from a backlog, and a full RDB resync when it falls too far behind.
The problem
One Redis instance is a single point of failure and a read bottleneck. Replication keeps live copies on other nodes for read scaling and as the material for failover — cheaply, by streaming the master's commands asynchronously.
How it works
- A replica (`replicaof host port`) connects to the master. When the link is healthy the master streams every dataset-changing command (client writes, key expirations, evictions) to it.
- Every master has a replication ID (a history) and an offset that grows with each byte of the stream. (ID, offset) identifies an exact version of the dataset.
- After a disconnect the replica sends PSYNC with its replid and offset; if the master's replication backlog still holds the missing part, only that part is sent (partial resync). Otherwise a full resync happens.
- Full resync: the master forks, writes an RDB (or streams it over the socket with diskless replication), buffers new writes, ships the RDB, then sends the buffered commands. Many replicas can share one RDB.
- Replication is asynchronous: the master does not wait for replicas, so a write acknowledged to the client can be lost on failover. `WAIT n timeout` blocks until n replicas acknowledge — fewer lost writes, but still not a CP system.
- `min-replicas-to-write` and `min-replicas-max-lag` refuse writes when too few replicas are close behind, bounding the data-loss window instead of letting it grow without limit.
- Replicas are read-only by default, do not expire keys themselves (the master sends DEL), and by default ignore maxmemory (the master evicts); replicas can chain from other replicas.
- A promoted replica starts a new replication ID but remembers the old one, so other replicas can still partially resync after a failover.
- With persistence off on the master, an automatic restart brings it back empty and its replicas copy the emptiness. Keep persistence on, or disable auto-restart.
Examples
redis.conf on a replica and the write-safety guard on the master.
# replica
replicaof 10.0.0.10 6379
replica-read-only yes
# master: refuse writes if fewer than 1 replica is within 10 s of lag
min-replicas-to-write 1
min-replicas-max-lag 10
repl-backlog-size 64mb
Check lag, then ask for stronger acknowledgement on one critical write.
redis-cli INFO replication
redis-cli SET order:42 paid
redis-cli WAIT 1 100
In practice
- Read scaling — Send slow O(N) reads to replicas, accepting small staleness; writes always go to the master.
- Backups — Run RDB snapshots on a replica so the master avoids fork and disk I/O (keep the master's own persistence in mind).
- Monitoring — Watch master_repl_offset vs slave_repl_offset, replication lag and `sync_full` counts; frequent full syncs mean the backlog is too small.
- Pitfall — Treating WAIT as strong consistency: acknowledged writes can still be lost across a failover.
Trade-offs
Pros
- Cheap, low-latency and non-blocking on the master
- Read scaling and warm standbys with trivial configuration
- Partial resync makes brief disconnects cheap
Cons
- Asynchronous: acknowledged writes can be lost on failover
- Replica reads can be stale
- Full resyncs fork the master and move the whole dataset