复制:master-slave 主从复制,master机的数据以及对数据的操作(写,过期等)同步给slave和sub-slave;
相当于slave保存了master的数据副本;
master和slave之间会建立连接用以从master向slave发送命令流来同步数据;
当连接异常断开后,slave会不断的重新创建新的连接,新连接创建成功后,可以根据之前保存的offset,来同步连接断开期间的操作,成为部分同步;如果部分同步失败,则从master全量同步;
master向slave的同步,在master和slave上都是非阻塞的。
实现:
1 在slave机向master发送命令:slaveof master-ip master-port
slaveof 127.0.0.1 6379
2 在redis.conf中配置
################################# REPLICATION #################################
# Master-Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
# 1) Redis replication is asynchronous, but you can configure a master to
# stop accepting writes if it appears to be not connected with at least
# a given number of slaves.
# 2) Redis slaves are able to perform a partial resynchronization with the
# master if the replication link is lost for a relatively small amount of
# time. You may want to configure the replication backlog size (see the next
# sections of this file) with a sensible value depending on your needs.
# 3) Replication is automatic and does not need user intervention. After a
# network partition slaves automatically try to reconnect to masters
# and resynchronize with them.
#
# slaveof <masterip> <masterport>
应用:
1 使用slave来进行复杂度大的只读;
2 避免master持久化造成的资源消耗,通过复制将数据复制到slave上,也是保存数据的一种方法;
“在使用 Redis 复制功能时的设置中,强烈建议在 master 和在 slave 中启用持久化。当不可能启用时,例如由于非常慢的磁盘性能而导致的延迟问题,应该配置实例来避免重置后自动重启。”
资料:redis