Full sync vs partial resync (PSYNC), the replication backlog and replica reconnection, INFO replication field breakdown, measuring replication lag, and the write-to-primary-before-replica data loss window.
A-7 — Master-Replica Replication: PSYNC, Replication Buffer, and Lag
Who this module is for: You are running Redis with replicas for read scaling or failover, but you have not understood what happens inside a replication relationship — how data flows, what happens when a replica reconnects after a disconnect, and when replication lag becomes a data consistency crisis. This module covers the full replication model.
The Replication Model
Redis uses asynchronous replication. The primary processes a write command, sends the command to connected replicas, and returns OK to the client — without waiting for replicas to acknowledge. Replicas apply commands in the order they are received, maintaining an eventually consistent copy of the primary's dataset.
Consequence: There is always a window (typically < 1ms, but potentially much longer under load or network issues) during which a write exists on the primary but not on replicas. If the primary fails in this window, the write is lost.
Setting Up Replication
# On the replica (redis.conf):
replicaof 10.0.1.50 6379
# Or at runtime:
REPLICAOF 10.0.1.50 6379
# Promote a replica to primary (disconnect from primary):
REPLICAOF NO ONE
Authentication for replication:
# replica redis.conf:
masterauth your-primary-password → authenticate with the primary
requirepass your-replica-password → protect the replica itself
Initial Sync: Full Resync
When a replica connects to a primary for the first time (or after a long disconnection), it performs a full resync:
Replica → PRIMARY: PSYNC ? -1 (no existing replication ID, no offset)
PRIMARY:
1. Sends: +FULLRESYNC {repl_id} {offset}
2. Calls BGSAVE to create an RDB snapshot
3. Streams the RDB file to the replica
4. While RDB is generating, buffers new write commands in the replication buffer
5. After RDB transfer: sends buffered commands
Replica:
1. Receives RDB file
2. Loads RDB (discarding existing dataset)
3. Applies buffered commands
4. Enters streaming replication mode
Memory impact: During full resync, the primary uses extra RAM for:
- The BGSAVE fork + copy-on-write pages
- The replication output buffer (buffering commands written during RDB transfer)
A large primary with a high write rate can use 2–3× its normal RAM during a full resync. Size your primary to handle this.
Incremental Resync: PSYNC with Replication ID and Offset
After initial sync, the replica streams commands from the primary continuously. Each command changes the replication offset — a byte count of how much data has been replicated.
Redis assigns each primary a replication ID (a random 40-character hex string). When a replica reconnects after a brief disconnect:
Replica → PRIMARY: PSYNC {repl_id} {last_offset}
PRIMARY:
- If repl_id matches AND last_offset is within the replication backlog:
→ Partial resync: stream only the commands since last_offset
→ Much faster than full resync
- If repl_id does not match OR last_offset is too old (not in backlog):
→ Full resync required
The Replication Backlog
The replication backlog is a circular buffer on the primary that holds recent write commands. Its size is configurable:
repl-backlog-size 1mb → default: 1MB
If a replica disconnects and reconnects within the time it takes to fill the backlog, it can do a partial resync. If the backlog has rolled over (the commands since the disconnect are no longer in the buffer), a full resync is required.
Size your backlog appropriately. At 100MB/s of write throughput, a 1MB backlog fills in 10ms. A replica that disconnects for even 1 second will require a full resync. Set the backlog to at least 60 seconds × write rate in bytes:
# For 10MB/s write rate, 60-second backlog:
repl-backlog-size 600mb
This trades memory (the backlog lives in RAM) for resilience to brief replica disconnections.
Monitoring Replication
INFO replication
On the primary:
role: master
connected_slaves: 2
slave0: ip=10.0.1.51,port=6379,state=online,offset=84729384,lag=0
slave1: ip=10.0.1.52,port=6379,state=online,offset=84729380,lag=1
master_replid: a3f9c2d7e8b14f2c1d9e8a7b6c5d4e3f
master_repl_offset: 84729384
repl_backlog_active: 1
repl_backlog_size: 1048576
repl_backlog_first_byte_offset: 83680808
repl_backlog_histlen: 1048576
lag — seconds since the replica last sent a REPLCONF ACK. A lag of 0 or 1 is healthy. Growing lag is a warning.
Sign in to keep reading
The rest of this module is free — sign in with Google to unlock it and track your progress.
Sign in & RegisterDiscussion
0Join the discussion