#The master-slave synchronization mechanism of Redis can ensure data synchronization between the redis master and slave.
synchronization method includes: full replication and incremental copy (Recommended learning: redis video tutorial )
# all copied
When the slave is started for the first time, it connects to the Master and sends the PSYNC command in the format of psync {runId} {offset}
{runId} 为master的运行id;{offset}为slave自己的复制偏移量。 slave第一次连接master时,slave并不知道master的runId,也不知道自己偏移量,这时候slave会传一个问号和-1,告诉master节点是第一次同步。格式为psync ? -1
slave接受到master的回复命令后,会保存master的runId和offset,slave此时处于同步状态。 slave处于同步状态,如果此时收到请求,当配置参数slave-server-stale-data yes时,会响应当前请求;slave-server-stale-data no,返回错误。
Incremental copy
If there is an abnormal situation such as network interruption or command loss, when the master-slave connection is restored, because the slave node has previously saved its own copied The offset and the running ID of the master node. Therefore, they will be sent to the master node as psync parameters, requiring partial replication operations, in the format of psync {runId} {offset}. After receiving the psync command, the master node first checks whether the parameter runId is consistent with itself. If it is consistent, it means that the current master node was copied before; then it is searched in its own replication backlog buffer according to the parameter offset. If the offset After the data is stored in the buffer, a continue response is sent to the slave node, indicating that partial copying can be performed; otherwise, full copying is performed. The master node sends the data in the replication backlog buffer to the slave node according to the offset to ensure that the master-slave replication enters a normal state.The above is the detailed content of How to achieve data synchronization in redis. For more information, please follow other related articles on the PHP Chinese website!