What is redis master-slave replication?
Several servers work together. One of the servers is the main server and is used for write operations. Other slave servers are used for read operations. Master-slave replication is to ensure
the consistency of the databases on these servers.
1. One redis service simulates three hosts:
1), copy three redis configuration files, and modify the corresponding configuration information:
port 6379
pidfile /var/run/redis_6379.pid
logfile redis6379.log
dbfilename dump6379.rdb
2), start Three redis services:
redis-server redis6379.conf &
redis-server redis6380.conf &
redis-server redis6381.conf &
3), using three clients to connect three redis services:
redis-cli -p 6379 (6379 was We set up as the master server)
redis-cli -p 6380
redis-cli -p 6381
4), view the master-slave relationship of the three redis services: info replication
redis has just started, they are all masters, and there is no slave connection.
2. Write data on 6379:
set k1 v1
3. Establish a master-slave relationship between redis services:
execute on 6380: slaveof 127.0.0.1 6379
execute on 6381: slaveof 127.0.0.1 6379
slave will put the master library on All of the data is copied-full copy.
4. Write data on 6379 :
set k2 v2
slave library will copy all new data on the master library --- incremental copy.
5, written in the 6380 and 6381 data:
the SET K3 v3 ---- error, because the two servers from the server, can only read, can not write
6, the host is down:
closed 6379 service (analog down): redis -cli -p 6379 shutdown
is executed on 6380 and 6381: info replication
slave stands by in situ.
7. Host recovery: redis-server redis6379.conf &
everything works
8. Slave down:
Shut down 6380 service: redis-cli -p 6380 shutdown The
host reduces one slave, other slaves remain unchanged.
9. Slave recovery : redis-server redis6380.conf &
automatically become the master, need to re-establish the master-slave relationship.
slaveof 127.0.0.1 6379
10. Slave host:
1). Host downtime:
execute on 6379: redis-cli -p 6379 shutdown The
slave stands by in place.
2) Find the upper slave: 6380
disconnect the original master-slave relationship: slaveof no one
3), reset the master-slave relationship:
execute on 6381: slaveof 127.0.0.1 6380
4), the original host recovery: redis-server redis6379.conf &
become a lonely man .
5) 、 Heaven becomes hell: make the master a slave slave
execute on 6379: slaveof 127.0.0.1 6381
One master is configured with multiple slaves, and one slave is configured with multi-state slaves, thus forming a huge cluster architecture.
11. Sentinel mode: the automatic version of the slave.
1), set up a master and two slave, master write slave read, read and write separation
2), provide sentinel monitoring configuration file: create file redis_sentinel.conf
edit content: sentinel monitor dc-redis 127.0.0.1 6379 1
3), start sentinel service :
Redis-sentinel redis_sentinel.conf
4), the host is down:
execute on 6379: redis-cli -p 6379 shutdown
automatically starts the sentinel voting mechanism, and automatically selects the upper position of the slave.
5), the original host recovery:
redis-server redis6379.conf &
automatically subordinate to the new host.