Redis from entry to abandonment series (8) Active and standby synchronization

Redis from entry to abandonment series (8) Active and standby synchronization

The example in this article is based on: 5.0.4

In the current stage where we talk about distributed no matter what application (is it really necessary???), our redis will be the master and backup. Well, if the data stored in redis is not important, we don't need to do [manual dog head] ]. In order to be further distributed, let us first understand the principle of CAP~

  • Consistent
  • Availability
  • Partition tolerance (fault tolerance)

In a distributed system, only CP or AP can be guaranteed, so which mechanism is used in Redis?

Redis uses the AP mechanism, and the master and backup of Redis synchronize data asynchronously. That is to say, when the Redis main service accepts the client's modification request, it will return immediately. During this period, even if the network of the standby service node is blocked (well, most of the optical cables are cut out), the main service still runs smoothly. . At this time, the Redis standby node is in a situation where the data is lagging behind. At this time, when the Redis master node synchronizes, it will use snapshot synchronization to send data to the standby node (described below). Data synchronization under normal circumstances All are synchronized through incremental buffers (that is, my optical cable is invincible).

Enable active and standby

replicaof <masterip> <masterport>
//当master有设置了密码的时候需要配置
masterauth <master-password>

My cable is invincible

When the backup node is in normal condition, redis will set up a replication buffer for the replica node on the master node, and then asynchronously synchronize the instructions in the buffer to the replica node each time. When the replica node consumes, it will also inform the master node to consume got there.

Let's take a look at the settings configuration of the replication buffer first.

//当replica节点断开一段时间之后,如果其消费的进度还在该缓存区内,那么可以继续执行增量同步。
repl-backlog-size 1mb
//当没有replica节点的时候,缓存区的设置过期时间
repl-backlog-ttl 3600

If the replication buffer is full, the previous data will be overwritten from the beginning, and then the replica node needs to be fully replicated, so a reasonable value of the replication buffer needs to be set here to prevent full replication of the replica node (eg: repl-backlog-size = Restart slave instance duration * master instance offset write amount per second).

Excavator cuts fiber optic cable

Snapshot synchronization is a time-consuming operation, bgsave the data to the disk, and then transfer the snapshot file to the backup node. When the backup node is not connected to the master node for a long time/a new backup node has just been added to the cluster, snapshot synchronization needs to be performed.

There are two strategies for snapshot synchronization:

  • Disk backup: The master service creates a new process, writes the RDB file to disk, and then the parent process incrementally transfers the file to the standby node.
  • Diskless snapshot: The master service creates a new process and directly writes the rdb file to the socket of the standby node

After the diskless snapshot is enabled, the diskless snapshot cannot be provided by default for other standby nodes during each transfer, and the new standby node will queue up for the next diskless snapshot. The time can be adjusted to achieve parallel transmission of multiple copies. .

When using non-SSD disks with relatively large bandwidth, using diskless snapshots is much better than disk backups.

Diskless snapshots can be set up with the following configuration:

repl-diskless-sync no
//当收到第一个无盘快照请求时,等待多少秒之后接受其他备节点的无盘快照请求
repl-diskless-sync-delay 5

write at the end

In fact, writing this thing really takes time~ But when I see someone likes/favorites, I am still very happy, and I feel recognized~

Thank you for your attention~

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324156429&siteId=291194637