Redis 主從複製的主要架構為是星狀的結構, 基本上由一個 master 與多個 slave 組成 (最簡單的狀況)。
master ---- slave 1 |-------- slave 2 |-------- slave 3 ...
master slave
1. <----------- 傳送同步 request
2. ------------> 同步 RDB
3. ------------> 同步 AOF
. ├── Dockerfile ├── docker-compose.yml ├── master │ ├── conf │ │ └── redis.cnf │ └── data └── slave ├── conf │ └── redis.cnf └── data
... bind 0.0.0.0 ... dbfilename master.rdb ... appendfilename "appendonly.master.aof" ...
bind 0.0.0.0 ... dbfilename slave.rdb ... appendfilename "appendonly.slave.aof" ... replicaof master 6379 ...
version: "3" services: master: image: redis:7.2 ports: - "6379:6379" container_name: redis-master volumes: - ./master/conf/:/usr/local/etc/redis - ./master/data:/data command: redis-server /usr/local/etc/redis/redis.conf slave: image: redis:7.2 ports: - "6378:6379" container_name: redis-slave volumes: - ./slave/conf:/usr/local/etc/redis - ./slave/data:/data command: redis-server /usr/local/etc/redis/redis.conf depends_on: - master
$ docker exec -it redis-master redis-cli 127.0.0.1:6379> info replication # Replication role:master connected_slaves:1 slave0:ip=192.168.247.3,port=6379,state=online,offset=23842,lag=1 master_failover_state:no-failover master_replid:041e4a14d0b809e0c8f7ddaebb70e7de57c3b695 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:23842 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:23842