Replication: Master-Slave

Difficulty: Intermediate

Question

How does Redis replication work, from the initial sync to steady state, and what consistency guarantees does it give?

Answer

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.

Step by step: how a replica syncs

  1. You run REPLICAOF 10.0.0.5 6379 on the replica (or set replicaof in config). SLAVEOF still works as an alias.
  2. The replica sends PSYNC <replication-id> <offset>. On the first connection it has neither, so it asks for a full sync.
  3. Full sync: the primary creates an RDB snapshot and sends it. With 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.
  4. The replica loads the RDB, then applies the buffered writes.
  5. Steady state: every write on the primary is forwarded to replicas as a command stream, and the replica tracks its offset.

Partial resync and the backlog

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.

What replication gives you, and what it does not

Gives youDoes not give you
Read scaling: send reads to replicasStrong consistency: replicas can be stale
Data redundancy on another machineAutomatic failover by itself
A place to run BGSAVE without hurting the primaryWrite scaling: all writes still go to one primary
Fast recovery after a primary restartProtection 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.

Tightening consistency

  • 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.

Common mistakes

  • Reading your own write from a replica right after writing it, and getting the old value. Route read-after-write paths to the primary.
  • Running a primary with persistence disabled and auto-restart enabled. It restarts empty, and replicas sync that empty dataset, wiping everything.
  • Leaving the backlog at 1 MB on a write-heavy primary, causing repeated full syncs after brief network issues.
  • Ignoring master_link_status and lag in INFO replication.
Replica initial sync and streaming. A full sync transfers a snapshot, then writes stream continuously.
  1. Replica → Primary: PSYNC ? -1 (first connection)
  2. Primary → Replica: FULLRESYNC replid offset
  3. Primary → Replica: RDB snapshot streamed
  4. Client → Primary: SET cart:42 ...
  5. Primary → Client: OK (before replica confirms)
  6. Primary → Replica: Write command forwarded

Code examples

Setting up and checking a replica

# 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.

Replication settings worth tuning

# 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.

Key points

  • Replication is asynchronous by default; replicas can lag and acknowledged writes can be lost on failover.
  • First sync is a full RDB transfer; later reconnects use PSYNC with the backlog for partial resync.
  • Replication scales reads, not writes.
  • WAIT reduces but does not eliminate the data-loss window.
  • Sentinel or Cluster is required for automatic failover.
  • Never auto-restart a primary that has persistence disabled.

Common follow-up questions

  • How does Redis Sentinel decide to fail over and which replica to promote?
  • What is a replication ID and why are there two of them after a failover?
  • How would you handle read-after-write consistency when reading from replicas?
  • What happens if the replication backlog is too small?
  • How does replication differ in Redis Cluster?

Interview tips

  • Say asynchronous early and explain what that means for data loss; it is the key insight.
  • Explain full sync vs partial resync with the backlog; most candidates skip it.
  • Use primary/replica terminology but show you recognise master/slave in older configs and INFO output.

Concepts covered

Primary-Replica Replication, PSYNC, Replication Backlog, Asynchronous Replication, WAIT Command

More Redis & Caching questions