Redis heavily uses Circular Buffers in its replication implementation to maintain a backlog. Here’s something neat about how Redis handles temporary disconnections …
When a replica disconnects briefly (network hiccup, restart, etc.), Redis doesn’t panic. The master maintains a 1MB (configurable) circular buffer that stores recent write commands. Think of it as a rolling history of the last N operations.
When the replica reconnects, it sends its last known replication offset to the master. If those missing commands are still in the buffer, Redis streams just the delta - this is called partial resynchronization (PSYNC). No full data transfer needed.
If the replica was down for too long and the buffer has wrapped around (overwriting older commands), Redis does a full sync. That means generating an RDB snapshot and transferring the entire dataset.
The circular buffer size can be tuned using the repl-backlog-size parameter:
- Too small: frequent full resyncs
- Too large: more memory consumed on the master
- Just right: fast recovery for typical network blips
The default 1MB setting works for many cases, but if you’re performing heavy writes or have a flaky network, increasing it to 64MB or 128MB can save you from expensive full syncs.
Redis took this trade-off because it is an in-memory database, and persistence is optional. Hence, a simple circular buffer is used to keep track of recent write commands.
If you want to dig deeper, I have a pretty detailed write-up on the internals of Redis Replication. You will find it interesting.