directory

  1. What is Redis master-slave replication
  2. Functions and scenarios of primary and secondary replication
  3. Master/slave replication mode
  • A master from
  • From more than a master

4. Principle of master/slave replication

  • Full amount of copy
  • Part of the copy
  • conclusion

What is Redis master-slave replication

It is well known that Redis is popular because it has the advantages of fast reading speed and persistence. Redis persistence ensures that data will not be lost during power outages or restarts. But that’s not enough. Persistence is writing data to disk on a regular basis so that if one server fails, all data will still be lost. To solve this single point of failure, master slave replication was created.

Master-slave replication is the automatic replication of data from one Redis server to another Redis server. The probability of one server failing is high, but the probability of multiple servers failing simultaneously is very low. Of course, there are Master servers and Slave servers. A Master can copy data to multiple slaves. However, it is important to note that the replication is one-way, only from Master to Slave. A Master can have multiple slaves, and a Slave can have multiple slaves, but a Slave can only be subordinate to one Master. The picture below is wrong.

Functions and scenarios of primary and secondary replication

  • Data redundancy: Master/slave replication implements hot backup of data, which is equivalent to storing one copy of data on multiple servers. It is a data redundancy method other than persistence. In this way, space is exchanged for security. If the primary server fails, data can be recovered through the slave service.
  • Quick fault repair: If the Master fails, you can quickly switch one Slave to the Master to provide services to ensure project stability.
  • Read/write separation: The Master/Slave replication is very simple to achieve read/write separation. Only Master is used to write data and only Slave is used to read data. In this way, multiple slaves can be used to meet the requirements of high concurrency.
  • Load balancing: Since read/write separation is implemented, load balancing can be implemented, with multiple slaves taking over the read operations to share the load of the Master server.
  • The cornerstone of high availability: Master-slave replication is the basis on which sentinel and cluster patterns can be implemented.

Since master-slave replication has these functions, what scenarios will it be used in practice?

  • If the project requires high data security and stability, master/slave replication is used to build sentinel mode or cluster.
  • Master/slave replication is used for read/write separation of massive data to improve mosquito control efficiency.
  • In DISASTER recovery (Dr), if you rely heavily on data and are afraid of data loss when the server is down, you can use primary/secondary replication to prevent data loss.

Master/slave replication mode

A master from

This mode is rare in practical applications. In fact, one master and one slave implements read/write separation and Dr. Considering the cost, two servers are used, one Redis server Master is responsible for the read operation, and periodically copy to another Slave server. Slave The Slave server performs read and write operations. Configure two Redis connections during configuration in the project.

From more than a master

There are several types of a master and more subordinate, as shown in the figure below:

In this case, all slaves are copied from the Master. The advantage of this is that the configuration is simple. All slaves use only the Master. May increase Master load.

Take a look at the picture below:

This mode is also one Master with many slaves, but not the same as above where all slaves are copied from the Master. It makes one to two slaves copy directly from the Master, and the other slaves copy from the two slaves. There is a hierarchy. This has the advantage of reducing the load on the Master server, but it can cause all slaves attached to it to become unusable if one of them dies.

Principle of master-slave replication

Above and build a master slave replication sample, is a master and two slave, that is how Redis is how to implement it specifically?

Before explaining the principles, let’s look at some concepts of master slave replication. After starting Master:

127.0.0.1:6379 > info server# ServerRedis_version :4.0.9 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id: 514e9a11b2a67dFC redis_mode:standalone OS :Linux 5.0.0-23-generic x86_64 ARCH_bits :64 multiplexing_API :epoll atomicvar_API :atomic-builtin gCC_version :7.4.0 process_id:19688 run_id:136de716105e54294144003a881ba29cdfbccfb2 tcp_port:6379 uptime_in_seconds:4515 uptime_in_days:0 hz:10 lru_clock:5556386 executable:/usr/local/redis/etc/redis-server
config_file:/usr/local/redis/etc/redis.confCopy the code

The run_id is the unique identifier of the Redis service. Restart the Redis service number and the run_id changes. Multiple Redis clients are connected to the same server and the run_id is the same.

127.0.0.1:6379 > info replication# ReplicationRole: master connected_slaves: 2 slave0: IP = 192.168.252.53, port = 6389, state = online, offset = 5541, lag = 1 = 192.168.252.53 slave1: IP and port = 6399, state = online, offset = 5541, lag = 0 master_replid: f0c89aa8040dfe869de82ee623a1212240456d76  master_replid2:0000000000000000000000000000000000000000 master_repl_offset:5541 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:5541Copy the code

Repl_backlog_size Specifies the size of the replication cache. The default size is 1 MB. If mater_REPL_offset is within this range, the replication is considered partial; otherwise, full replication is performed.

Full amount of copy

First look at the picture below, the picture is not very good excuse me.

  1. First, the Slave will send a psync command to the Master. Since this is the first time, the Slave does not know the run_id and offset, so passing -1 means full replication.
  2. The Master sends the run_id and offset to the Slave after receiving the psync, and the Slave stores them.
  3. The Master bgSave generates the RDB and sends the RDB file to the Slave.
  4. Write data may be generated during bgSave and SEND RDB, so data is saved to REPL_back_buffer and buffer is sent to Slave.
  5. The Slave clears the data and loads the RDB and buffer to store the data.

Part of the copy

The Slave already knows the Master’s run_id and offset, so send the psync command with these two parameters. The Master knows that this is a partial replication and sends the data to the Slave via the offset.

conclusion

Both full and partial replication are used in master/slave replication, and they work together. Look at the flow chart below:

It is also important to note that if the Master restarts and its run_id changes, all slaves that depend on it will make a full copy and then make a partial copy.

The last

Feel good can point a thumbs-up support!

For more technical articles, please pay attention to wechat official number: Win the world with Java architecture