Redis'sCopy ring buffer(Replication Backlog) is to implement the master-slave nodeIncremental synchronizationThe core mechanism of (Partial Resynchronization).
Its essence is aFixed-size memory ring queue, used to temporarily store the most recently propagated write commands of the master node.
When the slave node is temporarily disconnected and reconnected, if the required data is still in the buffer, the master node can directly send incremental data to avoid the overhead of full synchronization.
1. The function of ring buffer
-
Incremental synchronization
When the node is disconnected and reconnected, priority is given to trying to recover the lost data from the buffer to avoid full synchronization (RDB transmission). -
Reduce the impact of network jitter
When the network is unstable, the buffer retains the most recent data, improving the system's fault tolerance. -
Efficient memory management
A fixed-size ring structure avoids unlimited memory growth, and old data will be overwritten by new data.
2. The core field of the ring buffer
In RedisINFO replication
In the output, fields related to the ring buffer include:
Fields | effect |
---|---|
repl_backlog_active:1 | Whether the buffer is enabled (1=enabled). |
repl_backlog_size:1048576 | Total buffer size (default 1MB, configurable). |
repl_backlog_first_byte_offset:1 | The global copy offset corresponding to the first byte in the buffer (identifies the starting point of the buffer). |
repl_backlog_histlen:979768 | The length of data actually stored in the buffer (distance from the starting point to the latest data). |
master_repl_offset:979768 | The current latest replication offset of the master node (identifies the progress of data writing). |
3. Principles of ring buffer algorithm
1. Data structure
The buffer is aCharacter array, logically considered a ring (similar to a loop queue).
Implicit management through two pointers:
-
Write pointer:correspond
master_repl_offset
, indicates the location of the latest written to the master node. -
Starting pointer:correspond
repl_backlog_first_byte_offset
, indicates the starting position of the earliest data in the buffer.
2. Write data
Each time the master node propagates a write command:
- Append commands to the buffer.
- renew
master_repl_offset
(Increase the byte length of the command). - If the buffer is full (
repl_backlog_histlen == repl_backlog_size
), overwrite the old data and move the starting pointer forward (repl_backlog_first_byte_offset
increment).
3. Coverage mechanism
-
Trigger condition:when
master_repl_offset - repl_backlog_first_byte_offset > repl_backlog_size
。 -
Coverage behavior: New data covers old data,
repl_backlog_first_byte_offset
Push forward to ensure the buffer size is fixed.
4. Synchronization logic when reconnecting from nodes
When the slave node reconnects the master node:
Send your ownslave_repl_offset
(Last offset copied).
Master node check:
- if
slave_repl_offset
exist[repl_backlog_first_byte_offset, master_repl_offset]
Within range: -
Incremental synchronization: Extract from the buffer
slave_repl_offset + 1
arrivemaster_repl_offset
The data between them is sent to the slave node. - otherwise:
- Fully synchronized: Generate an RDB snapshot and transmit all data.
4. Configuration optimization suggestions
Buffer size (repl-backlog-size
)
- It needs to be adjusted according to the network environment and data writing rate.
-
Formula suggestions:
Buffer size ≥ Maximum disconnection time × Average write rate
。 - For example: If the network may be disconnected for up to 60 seconds and the master node writes 10KB per second, the buffer will be set to at least
60s × 10KB = 600KB
(The actual suggestion is slightly larger).
Buffer retention time (repl-backlog-ttl
)
- The default is 3600 seconds (1 hour), which indicates the time when the master node retains the buffer when there is no slave node connection.
- If all slave nodes are disconnected for a long time, the buffer will be released after the timeout to save memory.
V. Sample Scenario
Assume that the buffer size is 1000 bytes, the initial state:
repl_backlog_first_byte_offset = 1 master_repl_offset = 1 repl_backlog_histlen = 0
Write 500 bytes of data
-
master_repl_offset
Become501
,repl_backlog_histlen = 500
。 - The buffer is not full, and the starting point pointer remains unchanged.
Write 600 bytes of data
- Total space required
500 + 600 = 1100
, exceeds the buffer size (1000). - Overwrite old data, start pointer forward to
101
(cover the first 100 bytes). -
repl_backlog_first_byte_offset = 101
,master_repl_offset = 1101
,repl_backlog_histlen = 1000
。
Reconnection from node disconnection
- If from the node
slave_repl_offset = 800
: - exist
[101, 1101]
Within the range, trigger incremental synchronization. - If from the node
slave_repl_offset = 50
: - Not within range, trigger full synchronization.
6. Summary
Redis's ring buffer significantly improves the robustness and performance of master-slave replication through efficient memory management and offset tracking mechanisms.
Reasonable configurationrepl-backlog-size
and monitoringrepl_backlog_histlen
It is the key to avoiding full synchronization.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.