Skip to content
REPLICAOF & PSYNC — Master/Replica Replication
step 1/3

Reading — step 1 of 3

Master/Replica Replication

~2 min readScripting, Replication & Streams

REPLICAOF & PSYNC — Master/Replica Replication

A single Redis node is a single point of failure. Replication copies data to standby nodes — for read scaling, fault tolerance, and zero-downtime failover.

The Picture

   [ master ]
   /   |   \
 [r1] [r2] [r3]    <- replicas
  • Writes go to the master.
  • Master streams every write to every replica.
  • Replicas serve reads (scale read traffic horizontally).
  • If master dies, promote a replica.

The Old Way: SYNC

Replica connects → master snapshots its whole dataset (RDB) → sends it → then streams every subsequent write. Restart a replica? Full snapshot transfer again. Network blip? Full snapshot. Expensive.

The New Way: PSYNC

Redis 2.8+ introduced PSYNC (partial sync). The master keeps a replication backlog — a ring buffer of recent writes — plus a replication ID for the current "generation" of data.

When a replica connects, it sends PSYNC <last_known_replid> <last_known_offset>:

  • Same replid + offset still in backlog → master replies +CONTINUE\r\n and streams just the missing writes. Catches up in milliseconds.
  • Different replid or offset too old → master replies +FULLRESYNC <new_replid> 0\r\n, dumps the whole RDB, then streams new writes.

This is exactly the same pattern as MySQL binlog + GTID, PostgreSQL streaming replication + WAL position, or Kafka consumer offsets. Every replicated system reinvents this.

The Replication Stream

Every write the master executes is appended to the backlog as the raw command bytes. Replicas replay these commands to mirror state. Reads are not in the backlog — replaying a GET is pointless.

This is the same backlog you built for AOF! The two systems share the same "log every write" primitive.

Eventual Consistency

Redis replication is asynchronous by default. A write to master returns success before it reaches the replicas. If master dies right after the ACK, the writes that hadn't propagated are lost.

You can opt into stronger guarantees with WAIT <num_replicas> <ms> — block until at least N replicas have ACKed.

What You'll Build

  • REPLICAOF <host> <port> — declare role
  • ROLE — query role
  • A replication backlog that captures every write
  • PSYNC ? -1 — full resync (snapshot + backlog)
  • PSYNC <replid> <offset> — partial resync (backlog from offset)

You're implementing the foundation of every highly-available Redis deployment.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…