Redis high-availability solution master-slave synchronization
Preface
This environment is based on the Centos 7.8 system to build a Redis learning environment. For
specific construction, please refer to Redis-5.0.9 Environment Deployment
Redis comes with a master-slave synchronization function to solve the problem of database redundancy, and the configuration is relatively simple. Next I will introduce in detail
The role of master-slave replication
- Data redundancy: Master-slave replication realizes hot backup of data, which is a data redundancy method besides persistence.
- Failure recovery: When the master node has a problem, the slave node can provide services to achieve rapid failure recovery; in fact, it is a kind of service redundancy.
- Load balancing: On the basis of master-slave replication, with the separation of read and write, the master node can provide the write service and the slave node provides the read service (that is, the application connects to the master node when writing Redis data, and the application connects to the slave node when reading Redis data) , Share the server load; especially in the scenario of writing less and reading more, sharing the read load by multiple slave nodes can greatly increase the concurrency of the Redis server.
- Read-write separation: It can be used to realize read-write separation. The main library writes and reads from the library. The read-write separation can not only increase the load capacity of the server, but also change the number of slave libraries according to changes in demand;
- The cornerstone of high availability: In addition to the above functions, master-slave replication is also the basis for the implementation of sentinels and clusters. Therefore, master-slave replication is the basis for Redis high availability.
Master-slave replication process
Redis master-slave replication process:
- Slave establishes connection with master and sends sync command
- The master will start a background process to save the database snapshot to a file, and the master master process will start to collect new write commands and cache them.
- After saving in the background, send this file to slave
- Slave saves this file to the hard drive
Master-slave full data synchronization process
A master can have multiple slaves, and a slave can have multiple slaves. In this way, a powerful multi-level server cluster architecture is formed
Environmental preparation
role | node | ip | Redis-Version |
---|---|---|---|
Main server | reids-yum | 192.168.5.11 | Redis-5.0.9 |
Slave server | reids_source_code | 192.168.5.12 | Redis-5.0.9 |
Main server configuration
[root@reids-yum ~]# vim /etc/redis.conf
bind 192.168.5.11
[root@reids-yum ~]# systemctl restart redis
Configure from the server
[root@reids_source_code ~]# vim /etc/redis/redis.conf
bind 192.168.5.12
replicaof 192.168.5.11 6379
[root@reids_source_code ~]# systemctl restart redis
View master-slave synchronization status
Master-
slave
test master-slave synchronization effect
# 主写入数据
192.168.5.11:6379> set name wan
OK
192.168.5.11:6379> set age 20
OK
192.168.5.11:6379> get gender
"man"
# 从查看写入
192.168.5.12:6379> keys *
1) "gender"
2) "age"
3) "name"
192.168.5.12:6379> mget name age gender
1) "wan"
2) "20"
3) "man"
# 主删除数据
192.168.5.11:6379> del age
(integer) 1
# 从查看数据
192.168.5.12:6379> keys *
1) "gender"
2) "name"
The master-slave synchronization configuration is successful! ! !