Building a high-performance database cache for redis master-slave Replication

Source: Internet
Author: User

Building a high-performance database cache for redis master-slave Replication

1. What is redis master-slave replication?

Master-Slave replication. When a user writes data to the Master, the data file is sent to Slave through the Redis Sync mechanism, and Slave also performs the same operation to ensure data consistency; the implementation of Redis master-slave replication is very simple.

Ii. redis master-slave replication features

1. the same Master can have multiple Slaves.

2. The Server Load balancer under the Master node can also accept connections and synchronization requests from other Server Load balancer instances in the same architecture to implement cascade replication of data, namely, Master-> Slave-> slave mode;

3. The Master node synchronizes data to the slave in a non-blocking manner, which means that the Master will continue to process one or more slave read/write requests;

4. The Server Load balancer can also change the data synchronization method to a non-blocking method. When the Server Load balancer executes a new synchronization, it can still use the old data information for query. Otherwise, when slave loses contact with the master, slave will return an error to the client;

5. Master-slave replication is scalable. Multiple slave servers provide read-only query and data redundancy, while the Master node provides write operations;

6. Disable the Master data persistence mechanism by configuring and hand over the data persistence operation to Slaves to avoid the need for an independent process in the Master to complete this operation.

Iii. Principle of redis master-slave Replication

After a Slave process is started, it sends a SYNC Command to the Master to request a synchronous connection. Whether it is the first connection or re-connection, the Master will start a background process, save the data snapshot to the data file, and the Master will record all the data modification commands and cache them in the data file. After the background process completes the cache operation, the Master sends the data file to Slave. The Slave end saves the data file to the hard disk and then loads it into the memory, then the Master sends all the data modification operations to the Slave end. If the server Load balancer instance goes down due to a fault, the Server Load balancer instance is automatically reconnected after it recovers. After the Master receives the Server Load balancer connection, it sends the complete data file to the Server Load balancer instance, if the Mater receives synchronization requests from multiple Slave instances at the same time, the Master will only start a process in the background to save the data file and send it to all Slave instances to ensure that the Slave is normal.

Iv. server resource list


 

5. configuration process

About Redis installation and configuration here will not operate, would like to know friends please read: High Performance database cache redis A) http://cfwlxf.blog.51cto.com/3966339/1423106
 

3. 1. perform the following operations on the Master side: run the redis service.
 
 
  1. [root@redis_master sh]# redis-server/etc/redis/redis.conf 

Query redis running logs

# Read the output information of the log file to see the session mechanism to be executed when the Master and Slave are connected: loading the data file to the hard disk takes 0.012 seconds, we can imagine how fast the speed is. Of course, we need to evaluate it based on the data size. The service connects to port 6379, receives the Slave synchronous connection request, and enables "BGSAVE" synchronization;
 

Clear all keys in the Master database

 
 
  1. [root@redis_master sh]# redis-cli   
  2. 127.0.0.1:6379> FLUSHALL   
  3. OK   
  4. 127.0.0.1:6379> keys *   
  5. (empty list or set) 
3. 2 Slave operations are as follows:

[Root @ redis_slave ~] # Vim/etc/redis. conf

# Add the Master IP address and port

 
 
  1. # slaveof <masterip><masterport>   
  2. slaveof 192.168.8.8 6379 
Run redis
 
 
  1. [root@redis_slave ~]# redis-server/etc/redis/redis.conf 
Query Slave running logs

# Analyze the redis log to see the process of establishing a connection and data synchronization between Slave and Master. For example, sending the SYNC command, establishing a connection with the Master end 192.168.8.8: 6379, and then Slave sync started; then the Master sends the PING command to check the survival status of the Slave, and the replication continues ....

Query all keys in a database
 
 
  1. [root@redis_slave ~]# redis-cli                   
  2. 127.0.0.1:6379> keys *   
  3. (empty list or set) 
3. 3 slave2:

[Root @ redis_slave2 ~] # Vim/etc/redis. conf

# Add the IP address and port of the Slave to implement cascade replication;

 
 
  1. # slaveof <masterip><masterport>   
  2. slaveof 192.168.8.10 6379 

# Run the redis Service

 
 
  1. [root@redis_slave2 ~]# redis-server/etc/redis/redis.conf 
Query redis running logs

# The result is similar to that of Slave1, except that Slave2 is connected to slave1194258.10: 6379) to synchronize data. This is the case for MySQL cascade replication. Master> Slave1> Slave2;

# Query all keys of a Database

[Root @ redis_slave2 ~] # Redis-cli

127.0.0.1: 6379> keys *

(Empty list or set)

3. 4 master operations are as follows:
 
 
  1. [root@redis_master sh]# redis-cli   
  2. 127.0.0.1:6379> MSET ID 1005 NAMEMariaDB City BeiJing   
  3. OK   
  4. 127.0.0.1:6379> MGET ID NAME City   
  5. 1) "1005" 
  6. 2) "MariaDB" 
  7. 3) "BeiJing" 
  8. 127.0.0.1:6379> keys *   
  9. 1) "NAME" 
  10. 2) "ID" 
  11. 3) "City" 
3. 5 Client verification synchronization result slave1 end Verification
 
 
  1. [root@redis_slave ~]# redis-cli   
  2. 127.0.0.1:6379> auth !@#aedf   
  3. 127.0.0.1:6379> keys *   
  4. 1) "City" 
  5. 2) "NAME" 
  6. 3) "ID" 
  7. 127.0.0.1:6379> MGET ID NAME City   
  8. 1) "1005" 
  9. 2) "MariaDB" 
  10. 3) "BeiJing" 

Slave2 end Verification

 
 
  1. [root@redis_slave2 ~]# redis-cli   
  2. 127.0.0.1:6379> keys *   
  3. 1) "ID" 
  4. 2) "NAME" 
  5. 3) "City" 
  6. 127.0.0.1:6379> MGET ID NAME City   
  7. 1) "1005" 
  8. 2) "MariaDB" 
  9. 3) "BeiJing" 
4. Master write and Slave read Mechanism

Redis Master-Slave replication implements data read/write separation through programs, so that the Master is responsible for processing write requests, Slave is responsible for processing read requests, and Slave is responsible for processing more concurrent requests by extending the Slave, reduce the load on the Master, such:

This figure shows the process of implementing Redis Read/write splitting. By judging the user's Read/write requests, the write requests are sent to the Redis Master for processing, and the Read requests are sent to the Redis Slave for processing, for the shortcomings in this article, please give us some advice.

Blog: http://cfwlxf.blog.51cto.com/3966339/1433637

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.