Replication is async — acked writes can vanish

Redis replication is asynchronous primary-backup: writes ack on the master before reaching replicas, so failover can lose acked writes; the backlog buys partial resyncs, WAIT is best-effort, and only min-replicas-to-write converts loss into unavailability.

Previously

One node, however well-tuned, is a single point of failure. The first answer is to keep a copy on another node — but that copy lags behind, and that lag is the whole story of this scene.

Scene 06

Replication is async — acked writes can vanish

  1. Watch
  2. Try it
  3. Predict
  4. Capture
MasterMASTERrepl-id: 823c9c8cwritesoffset: 0headtail0%BACKLOGreplication backlog · 0 B / 1.0 MBPSYNCReplica 1(823c9c8c, 0)PSYNC streamingLAG0 BBUFFERED0PSYNCReplica 2(823c9c8c, 0)PSYNC streamingLAG0 BBUFFERED0WAITnot in useLOST WRITES0no writes lost yetDATA-PLANE GUARDdisabledmin-replicas off
What to watch for

Master accepts writes; the replication backlog ring fills; PSYNC streams keep both replicas' offsets within a tick of the master. This is the steady state — and the trap.

Continue unlocks when the animation finishes.
Implementation

Highlighted lines are the ones running in the diagram right now.

Replica.psync
(replication-id, offset) handshake; partial vs full
def psync():
repl_id, offset = self.replicationId, self.offset
resp = master.PSYNC(repl_id, offset)
if resp == CONTINUE:
# backlog still covers our gap
partial_resync() # stream missed bytes
else: # FULLRESYNC <new_id> <new_offset>
self.replicationId = resp.new_id
full_resync_via_rdb() # fork + ship RDB
stream_replication_link()
Master.handleWrite
append locally, ack the client, then replicate
def handleWrite(cmd):
self.applyToDataset(cmd)
self.offset += len(cmd)
self.appendToBacklog(cmd)
replyOK(client) # ack now
for r in self.replicas:
r.send(cmd) # fire-and-forget

Where this sits in Build Redis

Scene 06 of 10, in the HA act — Replication and Sentinel — async by design.. PSYNC, replication backlog, and the AP-not-CP gotcha: WAIT doesn't fix it; min-replicas-to-write does (at the cost of unavailability).

Up next. Sentinel — replicas keep a copy. But who decides the master is dead, and who promotes a replica into its place?

All 10 scenes in Build Redis · Every curriculum

Built with Arqly
Every scene in Build Redis builds on the one before it.All 10 Build Redis scenes