一个redis 主从同步的坑
有一个redis
切换主从时会出现的坑,我当初在redis升级的change log上看到的,估计不少人会遇到,所以干脆写出来。
可以看这段描述 :
Also note that since Redis 4.0 replica writes are only local, and are not
propagated to sub-replicas attached to the instance. Sub-replicas instead
will always receive the replication stream identical to the one sent by
the top-level master to the intermediate replicas. So for example in the
following setup:
A ---> B ---> C
Even if B is writable, C will not see B writes and will instead have
identical dataset as the master instance A.
以前迁移redis主节点的时候,普遍都是用的这个方法:
- A-->B
- 增加新slave:A-->B-->C
- 把B设置为: config set slave-read-only no
- 把写请求切换到B,读请求切换到C
- 断开A B的主从关系, slaveof no one
- B-->C
这个流程在redis4.0以前是没有问题的,上流程中4)执行后,A B的写操作都会同步到C; 但是在redis4.0及以后, 只有A的写入能同步给C,B的操作只会在B本地执行,并不会同步给C。也就是说: 身份是role:slave的节点,它的写入操作不会SYNC给它的slave
。
举一个有意思的例子, 比如当前集群是A-->B-->C
, 其中B是可写的, redis中有个keytest
的值是100
.
然后分别在A和B上执行incr test
, 完毕后,A B C上test的值分别为:101、102、101.