Due to Redis’ single point of performance and our innate need to back up data, Redis provides a master-slave replication service.
In this article, docker Compose is used to build a master and slave Redis service.
Configure the parsing
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # the REPLICATION # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # slaveof connected to the Master Slave 】 【 configuration 172.25.0.101 6379 # [Slave] Read-only mode # slave-read-only yes # [Slave] password # masterauth <master-password> # [Slave] Whether to allow response to query during replication, # serve-stale-data yes # [slave] Slave is promoted to Master priority, Valid only in sentinel mode # slave-priority 100 # [slave] IP address that the slave reports to the Master # slave-announce-ip 5.5.5.5 # [slave] The slave reports to the Master Report own port # slave-announce-port 1234 # [slave] interval for slave ping Master # rep-ping -slave-period 10 # [Master/ slave] timeout # Repl-timeout 60 # [Master] Diskless repl-diskless-sync no # [Master] # repl-diskless-sync-delay 5 # [Master] Enable Nagle algorithm to reduce traffic usage. Repl -disable-tcp-nodelay no # [Master] Indicates the size of the ring buffer log, which is used by the Slave after disconnection to avoid full replication. Default 1MB # repl-backlog-size 1MB # [Master] When Master disconnects all slaves for a specified time, Master clears the backlog # repl- backlogs - TTL 3600 # Master Master rejects all write operations # min-slaves to-write 3 # 【Master】 Master rejects all write operations # min-slaves-max-lag 10 when the delay is greater than the specified number of secondsCopy the code
Service building
The directory structure
├─ data │ ├─ data │ ├─ data │ ├─ data │ ├─ data │ ├─ data │ ├─ data │ ├─ data │ ├─ data │ ├─ data │ ├─ ├─ data ├─Copy the code
Compose File
A subnet is defined for easy operation, exposing ports 6371 (Master), 6372, and 6373.
version: "3"
networks:
redis-replication:
driver: bridge
ipam:
config:
- subnet: 172.25. 0. 0/ 24
services:
master:
image: redis
container_name: redis-master
ports:
- "6371:6379"
volumes:
- "./master/redis.conf:/etc/redis.conf"
- "./master/data:/data"
command: ["redis-server"."/etc/redis.conf"]
restart: always
networks:
redis-replication:
ipv4_address: 172.25. 0101.
slave1:
image: redis
container_name: redis-slave-1
ports:
- "6372:6379"
volumes:
- "./slave1/redis.conf:/etc/redis.conf"
- "./slave1/data:/data"
command: ["redis-server"."/etc/redis.conf"]
restart: always
networks:
redis-replication:
ipv4_address: 172.25. 0102.
slave2:
image: redis
container_name: redis-slave-2
ports:
- "6373:6379"
volumes:
- "./slave2/redis.conf:/etc/redis.conf"
- "./slave2/data:/data"
command: ["redis-server"."/etc/redis.conf"]
restart: always
networks:
redis-replication:
ipv4_address: 172.25. 0103.
Copy the code
The instance configuration
Master:
Basically do not need to configure, the simplest is to specify a port.
port 6379
protected-mode no
repl-diskless-sync no
repl-disable-tcp-nodelay no
Copy the code
Slave:
The configuration of the instance is consistent, because subnets are defined and there are no port conflicts.
Port 6379 protected-mode no slaveof 172.25.0.101 6379 slave-read-only yes slave-serve-stale-data yesCopy the code
Start the service
ocker-compose up -d
Creating network "replication_redis-replication" with driver "bridge"
Creating redis-slave-1 ... done
Creating redis-master ... done
Creating redis-slave-2 ... done
Copy the code
If you look at the Master log, you can see that two Slave replication requests were received:
1:M 18 Aug 2021 15:50:31.772 * Replica 172.25.0.102:6379 asks for synchronization 1:M 18 Aug 2021 15:50:31.772 * Full * Replication backlog created by replica 172.25.0.102:6379 1:M 18 Aug 2021 15:50:31.772 * Replication backlog created, my new replication IDs are '5d27746f14ee9be9694d794f96de6ba14a669dd1' and '0000000000000000000000000000000000000000' 1:M 18 Aug 2021 15:50:31.772 * Starting BGSAVE for SYNC with Target: Disk 1:M 18 Aug 2021 15:50:31.773 * Background saving started by PID 19 19:C 18 Aug 2021 15:50:31.777 * DB saved on disk 19:C 18 Aug 2021 15:50:31.777 * RDB: 0 MB of memory used by copy-on-write 1:M 18 Aug 2021 15:50:31.822 * Background saving terminated with success 1:M 18 Aug 2021 15:50:31.823 * Synchronization with replica 172.25.0.102:6379 Succeeded 1:M 18 Aug 2021 15:50:32.170 * replica 172.25.0.103:6379 asks for synchronization 1:M 18 Aug 2021 15:50:32.170 * Full resync requested by replica 172.25.0.103:6379 1:M 18 Aug 2021 15:50:32.170 * Disk 1:M 18 Aug 2021 15:50:32.171 * Background saving started by PID 20 20:C 18 Aug 2021 15:50:32.175 * DB saved on disk 20:C 18 Aug 2021 15:50:32.175 * RDB: 0 MB of memory used by copy-on-write 1:M 18 Aug 2021 15:50:32.225 * Background saving terminated with success 1:M 18 Aug 2021 15:50:32.226 * Synchronization with replica 172.25.0.103:6379 SucceededCopy the code
Viewing the Slave log, you can see the whole process of establishing a connection:
1:S 18 Aug 2021 15:50:31.771 * Connecting to MASTER 172.25.0.101:6379
1:S 18 Aug 2021 15:50:31.771 * MASTER <-> REPLICA sync started
1:S 18 Aug 2021 15:50:31.771 * Non blocking connect for SYNC fired the event.
1:S 18 Aug 2021 15:50:31.771 * Master replied to PING, replication can continue...
1:S 18 Aug 2021 15:50:31.772 * Partial resynchronization not possible (no cached master)
1:S 18 Aug 2021 15:50:31.773 * Full resync from master: 5d27746f14ee9be9694d794f96de6ba14a669dd1:0
1:S 18 Aug 2021 15:50:31.823 * MASTER <-> REPLICA sync: receiving 175 bytes from master to disk
1:S 18 Aug 2021 15:50:31.823 * MASTER <-> REPLICA sync: Flushing old data
1:S 18 Aug 2021 15:50:31.823 * MASTER <-> REPLICA sync: Loading DB in memory
1:S 18 Aug 2021 15:50:31.828 * Loading RDB produced by version 6.2.5
1:S 18 Aug 2021 15:50:31.828 * RDB age 0 seconds
1:S 18 Aug 2021 15:50:31.828 * RDB memory usage when created 1.83 Mb
1:S 18 Aug 2021 15:50:31.829 * MASTER <-> REPLICA sync: Finished with success
Copy the code
test
Log in to Master and try to write a new Key.
127.0.0.1:6371> set hello world
OK
Copy the code
Log in to Slave and check whether it can be read:
127.0.0.1:6372 > get the hello world ""Copy the code
Slave Attempted write operations:
127.0.0.1:6372> set hello redis
(error) READONLY You can't write against a read only replica.
Copy the code