How it works
The explanation, with examples you can run.
How a replica follows
# on the replicaREPLICAOF 10.0.0.5 6379The replica connects, the primary sends a snapshot (an RDB produced by a fork, or streamed straight to the socket with diskless sync), then streams every write command it executes. If the connection drops briefly, the replica asks to continue from its offset in the primary's replication backlog (a partial resync); if it has fallen too far behind, it needs a full resync, another snapshot. Size repl-backlog-size for the outages you expect.
Replicas are read-only by default and can serve reads, but those reads can be slightly stale. INFO replication on the primary shows each replica's offset, and the difference from the primary's offset is how far behind it is.
Narrowing the loss window
Two tools, neither of which turns Redis into a strongly consistent system:
WAIT numreplicas timeoutblocks the client until that many replicas have acknowledged its earlier writes, or the timeout passes. The replication docs stress that this does not make a set of Redis instances a CP system: a write acknowledged by a replica can still be lost depending on the failover.min-replicas-to-writeandmin-replicas-max-lagmake a primary refuse writes when too few replicas are connected and recently caught up. A primary isolated by a network partition then stops accepting writes after a bounded time, instead of collecting writes that the promoted replica on the other side will never see.
Sentinel
Replication alone does not fail over. Redis Sentinel is a separate process (redis-sentinel, or redis-server --sentinel) that monitors primaries, decides when one is down, promotes a replica, reconfigures the others, and tells clients the new address.
sentinel monitor mymaster 10.0.0.5 6379 2sentinel down-after-milliseconds mymaster 5000sentinel failover-timeout mymaster 60000The last argument of monitor (here 2) is the quorum: how many Sentinels must agree the primary is unreachable. To actually perform a failover, a Sentinel must also be authorised by a majority of all Sentinels. Hence the advice to run at least three, on machines that fail independently. Clients do not hard-code the primary; they ask Sentinel (SENTINEL get-master-addr-by-name mymaster), and most client libraries support this.
What to expect in a failover
- A few seconds without a writable primary: detection (
down-after-milliseconds) plus election and promotion. - Possible loss of writes acknowledged by the old primary but not yet replicated.
- If the old primary was only partitioned, not dead, it may keep accepting writes from clients on its side until it is reconfigured as a replica, and those writes are discarded.
min-replicas-to-writelimits this.
So Redis with replicas and Sentinel is highly available, not strongly consistent. For a cache that is fine. For data that must not be lost, keep the source of truth somewhere with synchronous, consensus-based replication.
Check yourself
3 questions. Get them all right to finish this topic.
0 of 3 right so far. Get all three to finish this topic.