Spring Cloud: Redis master-slave of distributed cache

1. Build a master-slave architecture

The concurrency capability of a single node Redishas an upper limit. To further improve Redisthe concurrency capability, it is necessary to build a master-slave cluster to achieve read-write separation.

insert image description here

2. Master-slave data synchronization principle

2.1. Full synchronization

When the master-slave establishes a connection for the first time, it will perform full synchronization and mastercopy all the data of the node to slavethe node. The process is as follows:

insert image description here

Here is a question, masterhow do you know salvethat it is the first time to connect? ?

There are several concepts that can be used as the basis for judgment:

  • Replication Id: Abbreviation replid, is the mark of the data set, and idthe same data set indicates the same data set. Each masterhas a unique replid, slavewill inherit masterthe node replid.
  • offset: The offset, which will repl_bakloggradually increase as the data recorded in increases. slaveThe current synchronization is also recorded when the synchronization is completed offset. If slaveis offsetless masterthan offset, it means that slavethe data is behind masterand needs to be updated.

Therefore, slaveto do data synchronization, you must masterdeclare your own replication idsum to the user offset, masterand then you can determine which data needs to be synchronized.

Because slaveit was originally one master, with its own replidsum offset, when it became the first time slave, when the connection was established, the sum mastersent was its own sum .replidoffsetreplidoffset

masterJudging and finding slavethat the sent one replidis inconsistent with your own, it means that this is a brand new one slave, and you know that you need to do full synchronization.

masterIt will send its own replidsum to this and save this information. The following will be consistent with .offsetslaveslaveslavereplidmaster

Therefore, masterthe basis for judging whether a node is the first synchronization is to see replidwhether it is consistent .

As shown in the picture:

insert image description here

Full process description:

  • slaveThe node requests incremental synchronization;
  • masterNode judges replid, finds inconsistencies, and rejects incremental synchronization;
  • masterGenerate complete memory data RDBand send RDBit toslave;
  • slaveClear local data, masterloadedRDB;
  • masterRecord RDBthe commands in the period repl_baklogand continue logto send the commands in to slave;
  • slaveExecutes the received command, maintaining mastersynchronization with .

2.2. Incremental synchronization

Full synchronization needs to be done first RDB, and then RDBthe files are transferred through the network slave, which is too costly. Therefore, except for the first full synchronization, incremental synchronizationslave is masterperformed most of the time .

What is delta sync? It is to update only part of the data that is different from the existing one slave. masterAs shown in the picture:

insert image description here

So masterhow do you know slavewhere the difference is with your own data?

2.3. repl_backlogPrinciple

masterHow do you know slavewhere the data differs from your own?

This is about the files during full synchronization repl_baklog.

This file is an array with a fixed size, but the array is circular, that is to say, after the subscript reaches the end of the array, it will start reading and writing from 0 again , so that the data at the head of the array will be overwritten.

repl_baklogwill record Redisthe processed command log and offset, including masterthe current one offset, and the slaveone that has been copied to offset:

insert image description here

slaveThe difference between and is masterthe data that needs to be copied incrementally.offsetsalve

With the continuous data writing, masterthe offsetgradually becomes larger, slaveand it is also copied continuously, catching masterup offset:

insert image description here

until the array is filled:

insert image description here

At this point, if new data is written, the old data in the array will be overwritten. However, as long as the old data is green, it means that slavethe data has been synchronized, even if it is overwritten, it will have no effect. Because only the red part is not synchronized.

However, if slavethere is network congestion, the resulting far exceeds masterthat of :offsetslaveoffset

insert image description here

If masteryou continue to write new data, it offsetwill overwrite the old data until slavethe current one offsetis also overwritten:

insert image description here

The red part in the brown box is the data that has not been synchronized but has been overwritten. If slaveyou recover at this time, you need to synchronize, but you find that offsetyou have lost all of your own, and you cannot complete incremental synchronization. Can only do full sync.

insert image description here

3. Master-slave synchronization optimization

Master-slave synchronization can ensure the consistency of master-slave data, which is very important.

RedisThe master-slave cluster can be optimized from the following aspects :

  • Enable diskless replication in masterthe configuration to avoid disks during full synchronization .repl-diskless-sync yesIO
  • RedisThe memory usage on a single node should not be too large to reduce RDBthe excessive disks caused IO.
  • Properly increase repl_baklogthe size, slaverealize fault recovery as soon as possible when a downtime is found, and avoid full synchronization as much as possible.
  • Limit the number of nodes masteron one slavenode. If there are too many slave, you can use a master-slave-slave chain structure to reduce masterpressure.

Master-slave architecture diagram:

insert image description here

4. Summary

Briefly describe the difference between full synchronization and incremental synchronization?

  • Full synchronization: mastergenerate RDBand send complete memory data RDBto slave. Subsequent commands are recorded in repl_baklogand sent to one by one slave.
  • Incremental synchronization: slaveSubmit your own offsetto master, masterand get repl_baklogfrom offsetsubsequent commands to slave.

When to perform full synchronization?

  • slaveWhen a node connects to a node for the first time master.
  • slaverepl_baklogThe node has been disconnected for too long and offsethas been overwritten.

When is an incremental sync performed?

  • slaveWhen a node goes down and comes back up, and repl_baklogcan be found in offset.

Guess you like

Origin blog.csdn.net/qq_37726813/article/details/130956050