Home > Database > Redis > body text

How to achieve data synchronization in redis

(*-*)浩
Release: 2019-11-20 13:27:31
Original
5921 people have browsed it

How to achieve data synchronization in redis

#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

How to achieve data synchronization in redis

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
Copy after login

When the master receives psync ? -1, it knows that the slave wants to copy in full, and it will inform the slave of its runId and offset, and reply with the command fullresync {runId} {offset}. At the same time, the master will execute the bgsave command to generate the rdb file, and all write commands during the period will be written to the buffer.

slave接受到master的回复命令后,会保存master的runId和offset,slave此时处于同步状态。
slave处于同步状态,如果此时收到请求,当配置参数slave-server-stale-data yes时,会响应当前请求;slave-server-stale-data no,返回错误。
Copy after login

Master bgsave is executed and the rdb file is sent to slave. After the rdb file is sent, the write command in the buffer begins to be sent to the slave.

Slave receives the rdb file, discards all old data, and starts loading the rdb file.

After the rdb file synchronization is completed, the slave executes all write commands sent from the master buffer.

After that, every time the master executes a write command, it sends the same write command to the slave.

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!