(Viii) Redis entry - Redis master-slave replication

(Viii) Redis entry - Redis master-slave replication


table of Contents

  • Redis master-slave replication Profile
  • How to configure
  • Test Copy
  • Replication Works
  • Sentinel mode

1.Redis master-slave replication Profile

When the traffic system more and more, a redis server has been unable to support such a large number of visits, in order to solve this problem, one way is usually taken to be a master-slave architecture Master / Slave, Master in write-based, slave read-mostly, the master updates the master node according to the configuration, the machine is automatically synchronized to the slave node. It is often said that the separation of the main redis from copy to read and write. redis the master-slave architecture can be implemented in addition to separate read and write, may be achieved from the main conversion Shihai host fails, thereby protecting the system has a certain disaster recovery capabilities.

Redis copy function into synchronization (sync) and the propagation of commands (command propagate) two operations:

  • Wherein the synchronization status of a database from the server to update the current state of the database in which the master server.
  • The command is used to spread the operating state of the primary database is modified in the server, leading to the main database server inconsistencies from state, let the Lord back to a consistent state from the database server.

2. How to configure

Profile Modified:

How to open multiple redis service: First redis.conf configuration file three copies, by modifying the three-port analog Redis server respectively.

Open daemons: daemonize yes

Pid file name: Set pidfile. For example: /var/run/redis_6379.pid, this time when redis running as a daemon, it will default pid file is written inside /var/redis/run/redis_6379.pid

Specify the port number: three port numbers are 6379,6380,6381.

Specifies the log file: Configure the logfile. Three files are configured, 6379.log ,, 6380.log, 6381.log.

Specifies the dump file name: Configuration dbfilename

Set master-slave relationship:

The master-slave relationship redis disposed on a slave. In the master-slave relationship and the view configuration master-slave relationship using the following commands from the machine:

Master-slave configuration: slaveof main library main library IP port numbers 
to view the master-slave relationship: info replicati
the current database synchronization with other databases to stop, turn into the main database: SLAVEOF no one

Slave:

Host:

Note: need to reconnect after every disconnection Master and Slave, unless redis.conf configuration file.

3. Test the master-slave replication

Full volume copying:

SLAVEOF command execution from the machine, all data will be copied all the primary node. Each pass SLAVEOF from the master will set the amount of a full copy, back is incremental.

Incremental Copy:

Host writes new data, copy from the opportunity.

 Separate read and write:

Here we write new data from the machine

Seen from the machine at this time is read-only. In redis profile, slave-read-only state of the configurable from pig stomach machine. If the configuration item is set to no, the slave may write data.

 In this case, the new data is written to the copy machine from there to host it? FIG visible and not to copy the data from the host machine.

Host is down:

 At this time, from the state of the host how to:

Thus, the host goes down, the slave is still a slave. If the host is working properly again, this time to restore the original host role is still the host. If we execute SLAVEOF no one command to set a new host in 6380 before the host response. After the restoration with the host, then the host will not be the host again, but this time the hosts or 6380. At this time, the original host is still the host, but no longer, and the other two machines have a master-slave relationship.

4. Copy the principle

It sends a sync command to start successfully connected to the slave master.

Master ordered to start the process of archiving the background while collecting all data received to modify the set command, after the background process is finished, master transfers the entire data file to the slave, to complete a full synchronization.

Copy the full amount: The slave database service after receiving the data file, save it and load it into memory.

Incremental Copy: Master will continue to collect all the new changes in order to pass command slave, synchronization is complete

But as long as the connection is re-master, a full synchronization (full volume copy) will be automatically executed

5. Sentinel Model

In practice, if a master multi-slave mode to take, and when the host is down, it is not the role of the host role to play at this time when the host is not working, there is a conversion from the machine to a host, the host of the work undertaken, this is the sentry mode. Sentinel surveillance mode is redis is intended to operate in accordance with good (or at least ensure that the master node exists), if a host problems, Sentinel will host the next one automatically from a machine to a new host, and let other established master-slave relationship from the machine and the new host.

 How to configure:

Under the new profile directory sentinel.conf file name must not wrong, then configure the appropriate content.

sentinel monitor monitored database name (from their own name) 127.0.0.1 6379 1 
last number above 1 indicates the host let hang salve vote to see who takes over as host, after the number of votes becomes the master

 Start Sentinel mode:

redis-sentinel /myredis/sentinel.conf 
above-described directory configuration in accordance with their actual situation, may be a different directory

The host is down, two from the opportunity to vote for a new host, (this time can be seen from these operations redis log). If the host once again restored, it will become a slave under the new host to continue working.

 The main disadvantage of replication from

Since all write operations are the first to operate on the Master, and then synchronize updates to the Slave, so there is a delay from Master to Slave synchronous machine, when the system is busy, delay problem will become more severe, increasing the number of Slave machine also make this problem worse.

Guess you like

Origin www.cnblogs.com/zylhxd/p/11440184.html