Redis's master-slave synchronization

Source: Internet
Author: User

Synchronous

Why should Redis implement master-slave synchronization?

Master-Slave synchronization, with the following benefits:

    1. Realize the backup of data, reduce the loss of single point of failure;
    2. Helps to achieve load balancing. Originally a server responsible for all the data read and write, and through the master-slave synchronization, you can have more than the server is responsible for data read, and the main server is responsible for the data write, reduce the single point of pressure.


Partial resynchronization

Redis starting from 2.8, the introduction of Psync, support partial resynchronization: The primary server and disconnected from the server during this time, the primary server data may be only a small number of changes, then when the primary server re-connected from the server, there is no need to send the data from the primary server to the server for data coverage, only to the inconsistent Data to the slave server, which avoids the primary server for data backup and network transmission of data.



The process of synchronization

The following uses two machines, a (10.15.62.11:6379) as the slave server, B (10.15.62.12:6380) as the primary server, describes A and b for the first full synchronization and partial resynchronization process.

  1. A slaveof 10.15.62.12 6380 command is sent to a by the client, and a is returned to the client after a command is received +ok;
  2. Establish sockets between A and B;
  3. A send ping command to B, there are two purposes: 1) Verify that the socket between AB has no problem; 2) b server can handle the command, external service.
  4. B to a return to pong;
  5. If a masterauth is set, it indicates that authentication is required. Then a will send the auth command to B, and the command parameter is the value of Masterauth.
  6. If a sends a value that is the same as B's Requirepass value, it is validated.
  7. A to B send replconf Listening-port 6379,b will save the port of a, the only purpose is: Users use the client to B send info replication command, display from the server port.
  8. A to B send Psync? -1;
  9. b After accepting the Psync command, send the Run-id and own copy offset to A,a will save the run ID of B and copy offset of B as its own copy offset; b then perform the bgsave operation, generate an RDB data from the subprocess, Also save the write commands received during the execution of the Bgsave command.
  10. B sends the RDB data to A,a to empty its database, loads the RDB data of B into memory, and B sends the copy command to a.
  11. Synchronization completed;
  12. When writing data to B, B passes the command to a.
  13. A and B are suddenly disconnected;
  14. A and B reconnect;
  15. A to B send Psync <run-id> <offset>......run-id is the run of the master server that you save Id,offset is its own copy offset.
  16. b receives the Psync and its parameters, discovers that the run ID is the same as its own run ID, and finds that the data after offset is in its own replication backlog, requiring only partial resynchronization and returning a +continue to a.
  17. b Send the data inside the backlog buffer to a.
  18. Synchronize again.


Code implementation:

Not to be continued.



Resources:

"Redis Design and Implementation" chapter 15th

A brief analysis of Redis master-slave synchronous source-slave
















Redis's master-slave synchronization

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.