(iv) Redis master-slave replication

Source: Internet
Author: User

About Master-slave replication

and the MySQL 's master-slave, this structure is mainly for data redundancy and prompt performance. Redis 's master-slave synchronization is asynchronous, so it does not affect the processing performance of the master. In the case of data persistence, this task can be delegated from the server to reduce the burden on the primary server.

In the master-slave structure, the slave server is generally set to read-only mode, so as to better maintain data consistency.

Redis Master-Slave synchronization has two ways, one is full synchronization and partial synchronization, from the server for the first time (never before) and the primary server to establish a connection is full synchronization, the later will be part of the synchronization, only when the partial synchronization is not successful will be full synchronization. However, you can request full synchronization directly from the server at any time.

in general, the master-slave is part of the high-frequency synchronization (also known as incremental synchronization) , that is, after the first full synchronization, no other unexpected words, the main server will synchronize the contents of the buffer to the slave server. Note that the data is not sent directly from the server, but the primary server puts the data first into its own memory buffer and then from the buffer to the slave server. Then if the master-slave direct incremental synchronization is unsuccessful, the full synchronization will be performed, that is, copying the Dump.rdb file and buffer delta data to the slave server.

The process is: The master server discovers that the data has write operations, then writes the change record to the buffer and all connected slave servers.

Here's the full synchronization process:

  • from the server is periodically sent to the primary server The PSYNC command, which provides Master_runnid (ID information for the master thatwants to get the data ) and offset(offset information) information from the server, When the primary server receives the information, it verifies that it is partially synchronized if both are verified, and if the offset verification does not pass, it is fully synchronized.

  • semi-synchronous: The primary server uses offset offsets from the server , navigates to its own buffer area, and then synchronizes the incremental data to the slave server.

  • Full Sync : The master server sends full-sync return information to the slave server and then calls the BGSAVE(backgroundsave, in fact, the Save Command) instructs to create a child process that holds the data in memory to disk, that is, writes the data to the RDB file. the memory database changes after the moment the RDB file is started will continue to occur in memory.

When the Rdb write is complete, the master server transfers the Rdb file to the slave server, saves the file to disk from the server, and then loads it into memory from the server. After the load is complete, the primary server writes the RDB to the data changes that occurred during the time the database was loaded and sent to the slave server in the format of the Redis protocol.

if more than one is sent from the server to the primary server SYNC instruction, only one BGSAVE operation is performed for the primary server , then the saved RDB file is sent to multiple slave servers.

If a network outage occurs during the synchronization process, the 2.8 version will not be processed again after the previous version, but instead will be synchronized incrementally.

Why is there Master_runid and offset from the server ? Because this information is staged before the server disconnects from the primary server, it is also intended for subsequent data synchronization needs.


Environment description

Follow the previous steps to install 2 servers:

Redis01 172.16.100.10
Redis02 172.16.100.20


Turn on firewall ports and turn off SELinux

Open 6379 Port

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/83/4C/wKiom1dv5RLS4mfeAADUNpcsLBA267.jpg-wh_500x0-wm_3 -wmp_4-s_775245447.jpg "title=" 1.jpg "alt=" Wkiom1dv5rls4mfeaadunpcslba267.jpg-wh_50 "/>

Turn off selinux

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/83/4B/wKioL1dv5RnitAcfAACkjWDivZ0128.jpg-wh_500x0-wm_3 -wmp_4-s_771839037.jpg "title=" 2.jpg "alt=" Wkiol1dv5rnitacfaackjwdivz0128.jpg-wh_50 "/>

then restart the iptables service.


Modifying a configuration file

Modify the master server configuration file and do not need to modify this file if password verification is not required, restart the Redis service after modification

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/83/4C/wKiom1dv5V3yLZohAADOZCadLwE041.jpg-wh_500x0-wm_3 -wmp_4-s_807254622.jpg "title=" 3.jpg "alt=" Wkiom1dv5v3ylzohaadozcadlwe041.jpg-wh_50 "/>

Modify the From server configuration file,

Locate The slaveof field, and if the primary server has password authentication, configure the same password as follows:

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/83/4B/wKioL1dv5ZzRNB84AAD-Y4hYySU743.jpg-wh_500x0-wm_3 -wmp_4-s_3479071749.jpg "title=" 4.jpg "alt=" Wkiol1dv5zzrnb84aad-y4hyysu743.jpg-wh_50 "/>


Verifying Master-slave replication

Set a key value data in the Lord

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/83/4C/wKiom1dv5eHiBSPsAAAqym-9nn0394.jpg-wh_500x0-wm_3 -wmp_4-s_1182764526.jpg "title=" 5.jpg "alt=" Wkiom1dv5ehibspsaaaqym-9nn0394.jpg-wh_50 "/>

Get a key value data from the top

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/83/4C/wKioL1dv5eqA_MEaAAAoMrEWUxM063.jpg-wh_500x0-wm_3 -wmp_4-s_861575786.jpg "title=" 6.jpg "alt=" Wkiol1dv5eqa_meaaaaomrewuxm063.jpg-wh_50 "/>

To view the connection information via info , execute the following command at the Lord:

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/83/4C/wKiom1dv5fLzxSKXAAAZHBhiSxY494.jpg-wh_500x0-wm_3 -wmp_4-s_3963703395.jpg "title=" 7.jpg "alt=" Wkiom1dv5flzxskxaaazhbhisxy494.jpg-wh_50 "/>

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/83/4C/wKioL1dv5frAjGQjAABbdJf_ejg221.jpg-wh_500x0-wm_3 -wmp_4-s_1931301989.jpg "title=" 8.jpg "alt=" Wkiol1dv5frajgqjaabbdjf_ejg221.jpg-wh_50 "/>


Non-stop switching

There is no difference between master and slave in Redis, the only difference is that in this mode the data is not allowed to be written from the server, see:

I'm working on a server from a SET operation, prompt as follows

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/83/4D/wKiom1dv5oPQV1v2AAAoj3R_ThI313.jpg-wh_500x0-wm_3 -wmp_4-s_3172528787.jpg "title=" 1.jpg "alt=" Wkiom1dv5opqv1v2aaaoj3r_thi313.jpg-wh_50 "/>

In fact, the master-slave switch is to change from the Lord to the Lord. The basic steps are as follows:

Set from server writable:

Modify the configuration file, such as:

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/83/4D/wKiom1dv5oqB3UCDAACWtOByeBg840.jpg-wh_500x0-wm_3 -wmp_4-s_3503795992.jpg "title=" 2.jpg "alt=" Wkiom1dv5oqb3ucdaacwtobyebg840.jpg-wh_50 "/>

After the modification is complete, restart from the server.

After modifying and restarting the slave server, you can still synchronize data from the primary server from the server, which is not a concern.

access to the application IP replaced with IP from server :

This step ignores the fact that the business is accessing the database changes to the IP.

To set the slave server as the primary server:

Execute this command from the top

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/83/4C/wKioL1dv5pOz5lbKAAAbNQqlzXQ379.jpg-wh_500x0-wm_3 -wmp_4-s_63068319.jpg "title=" 3.jpg "alt=" Wkiol1dv5poz5lbkaaabnqqlzxq379.jpg-wh_50 "/>

The above command is to turn off replication from the server and will run from the server as a standalone master server. This is a good understanding, that is, it does not synchronize the other database server, and run alone, then the natural is the primary server.

There are other uses of the above command is to dynamically switch the main server, such as if we want to temporarily let from the server to other primary server to synchronize data, you can run, such as:

Slaveof Masterip Masterport

Note: if the current server is already A server from the server, and you use this command to make the current server as a B server from the server, that is, to synchronize data from B, then the current server will discard the previously synchronized data, start to synchronize the new master server.

This command only takes effect temporarily, temporarily overwriting the configuration file in the slaveof set, resume after reboot.

Verify that replication is stopped:

Run the info command on the original primary server , as shown below:

Although the role is the master, but SLAVE 's connection is gone.

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/83/4D/wKiom1dv5vaDOLnpAABB0SABb-I979.jpg-wh_500x0-wm_3 -wmp_4-s_114007627.jpg "title=" 4.jpg "alt=" Wkiom1dv5vadolnpaabb0sabb-i979.jpg-wh_50 "/>

we make a SET operation, and thenperform a get operation on the original slave server (REDIS02) to see if the value can also be obtained:

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/83/4C/wKioL1dv5v6w9DraAAA5HpujskY627.jpg-wh_500x0-wm_3 -wmp_4-s_459869346.jpg "title=" 5.jpg "alt=" Wkiol1dv5v6w9draaaa5hpujsky627.jpg-wh_50 "/>

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/83/4D/wKiom1dv5wXxxDD8AAAkjjeY_d4109.jpg-wh_500x0-wm_3 -wmp_4-s_936721214.jpg "title=" 6.jpg "alt=" Wkiom1dv5wxxxdd8aaakjjey_d4109.jpg-wh_50 "/>

You can see that you can't get it.

in the Execute the Info command on REDIS02 to see the result:

Show that it has become the Lord itself.

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/83/4C/wKioL1dv5w2CspTJAABAS0mT0wE388.jpg-wh_500x0-wm_3 -wmp_4-s_3269861062.jpg "title=" 7.jpg "alt=" Wkiol1dv5w2csptjaabas0mt0we388.jpg-wh_50 "/>

We can replace the original primary server with the above operation or manually switch when the primary server fails

This article is from the "Little Demon's Home" blog, so be sure to keep this source http://littledevil.blog.51cto.com/9445436/1793091

(iv) Redis master-slave replication

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.