Redis Sentinel Master-Slave Switching (failover) solution, detailed configuration

Source: Internet
Author: User
Tags failover redis
Redis Sentinel Master-Slave Switching (failover) solution, detailed configuration

Author: oyhk 2013-10-10 23:55:49 0 Reviews 629 views

Redis Source Learning notes Master-slave replication Blog Category:Redis Redis C Redis Source Learning notes article List

Because the picture is larger and the zoom is blurred, double-click to open the View ^_^

Master-slave replication is simply to synchronize data from one Redis database to another Redis database, and according to the data flow, the sender of the data we call master, the recipient of the data we call slave (Master/slave's division is not so certain, For example, B can be used as the slave of a, but also as the master of C, the following is the master-slave replication process from the perspective of slave and master respectively.

The first is the slave end, for the slave end, master-slave replication mainly experienced four stages:

First stage: Connect with Master
Phase two: Initiate a sync request to master (sync)
Phase three: Accept the RDB data from master
Phase IV: Loading an RDB file

Here's a diagram that outlines what slave is doing at each stage:



For the above illustration, it is noted that Redis receives the slaveof master_host master_port command and does not immediately establish a connection to master, but when it executes the server routine task Servercron, finds itself in Redis_repl_ Connect status, then the real connection to the maser, pseudo-code:
Python Code def Servercron (): # Server in redis_repl_connect status if Redisserver.repl_state = = Redis_repl_connect: # Initiate connection to Master Connectwithmaster () # Other routine tasks (omitted) ...

Then we take a look at the master-slave replication process, master side of the process is how, in detail before we look at the details, we first comprehensive look at the main side of a few things:



After reading this diagram, you may have the following questions:

1. Why do I send a ping to slave periodically after master sends the Rdb file?
2. What is the use of the "change" command sent by master after the Rdb file has been sent out?

Before answering the question 1, let's answer question 2 first:
Master saves the Rdb file through a child process, so master can still handle the client request without being blocked, but this also causes the "key space" to change during the time the Rdb file is saved (such as receiving a client request, executing "set name Diaocow" command), so in order to ensure the consistency of data synchronization, master will save the Rdb file during the storage of these possible changes to the database "key space" command, and then put it in the reply list of each slave, when the Rdb file is sent out master will send the contents of these replies list , and after that, if the database changes, master will still append the changed command to the reply list and send it to slave, which will guarantee the consistency of the master and slave data. Related pseudo-code:

Python code    def processcommand (CMD, ARGC, ARGV):       #   Processing Commands        call (CMD, ARGC, ARGV)         #  if the command causes a database key space change and the current Redis is a master, synchronize the change commands        if  Redisserver.update_key_space and len (redisserver.slaves)  > 0:            replicationfeedslaves (CMD, ARGC, ARGV)       Def replicationfeedslaves (CMD, ARGC, ARGV):        #  Send the change command to each slave node in the: Redis_repl_wait_bgsave_end State        for slave in  redisServer.slaves:           if slave.replstate  == REDIS_REPL_WAIT_BGSAVE_START:                continue           slave.updatenotify (CMD, ARGC, ARGV)   
Because after sending the Rdb file, master will send the "change" command to Slave, it may be over 1s or over 1 hours, so in order to prevent slave meaningless wait (for example, Master is dead), master needs to send the "keepalive" command ping periodically. To tell slave: I'm still alive, don't interrupt my connection.

Now let's look at what happens when master receives the Sync Sync command sent by slave:


The above diagram looks like branching complexity, but we can take the following points:

1. Saving the Rdb file is done in a sub-process;
2. If master is already saving the Rdb file, but no client is waiting for this bgsave, the newly added slave will need to wait until the next bgsave, instead of using the generated RDB file directly (as explained in the reason diagram)
3.master periodically checks to see if the Rdb file is saved (time event Servercron);

Next we look at how master sends an RDB file to each slave:



Well, so far we have analyzed in the master-slave replication process, master and slave on both sides of how a process flow; Finally, I draw a diagram that summarizes the process of master-slave replication (we can look at the picture and remember the specifics of it):



PS: In the master-slave copy process, any step error, will cause the whole process to start all over again, so if the Rdb file is very large or at this time is at the peak of business, the system performance will have a very big impact.

Summary:
1. Understand the concept of Master master and slave;
2. Understand the master-slave replication implementation process, especially the key steps;
3. Understand the deficiencies that exist in the current master-slave replication process;
View Picture Attachments



OYHK Study Notes

The number of visits to the website has slowly come up. For the performance of the website, we started using Redis as a caching strategy. At first, Redis is a single point, and when a machine rock machine, the service of Redis stops completely, which will affect the normal operation of other services. Crap not much to say, under the use of Redis Sentinel to do a master-slave switch cluster management. Do this cluster management time, looked a lot of information to fully understand, he is how to do.

For the Java client, see:

Http://blog.mkfree.com/posts/52b146e6479e5a64742fddd0

Reference: Http://redis.io/topics/sentinel I also read this article.

Environment configuration:

Because I do not have too many machines this time, I used vagrant to open more than one virtual machine. And then put the environment in place.

For Redis installation Please refer to: Redis Simple Official Script installation method (Linux)

Cluster configuration requires a minimum of three machines, then I have three virtual machines, three virtual machines installed the same Redis environment

IP respectively: 192.168.9.17 (Redis Sentinel cluster Monitor) 192.168.9.18 (Redis Master) 192.168.9.19 (Redis slave)

Redis configuration:

Master Redis profile, use the default profile on it, if you need to design additional parameters

From the Redis configuration file, add

#从的redis配置文件, you need to add
vim/etc/redis/6379.conf
slaveof 192.168.9.18 6379

Start Master-Slave Redis

#启动主redis (192.168.9.18)
/etc/init.d/redis_6379.conf start
#启动从redis (192.168.9.19)
/etc/init.d/redis _6379.conf start

View Master Redis Information

#查看主redis的信息
redis-cli-h 192.168.9.18 info Replication

# Replication
role:master #代表192.168.9.18:6379 This redis is the main
connected_slaves:1
slave0:192.168.9.18,6379,online

View from Redis information

#查看主redis的信息
redis-cli-h 192.168.9.19 info Replication

# Replication
role:slave #代表192.168.9.18:6379 This redis is the main
master_host:192.168.9.18
master_port:6379
master_link_status:up
master_last_io_ Seconds_ago:4
master_sync_in_progress:0
slave_priority:100
slave_read_only:1
connected_slaves : 0
Configuring the Redis Sentinel Cluster monitoring service

<

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.