什么是主从复制
主从复制的作用
一般来说,真实项目环境中Redis都是多台的
说明
info replication
查看主从配置信息127.0.0.1:6379> info replication # 1. 查看主从复制的相关信息 # Replication role:master # 角色为 主节点 connected_slaves:0 # 从节点数量 master_failover_state:no-failover master_replid:262aa62577e042d3cf5a177e4738978b61cdf54a master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 127.0.0.1:6379>
[root@iZ2vc8owmlobwkazif1efpZ redis-config]# cp redis.conf redis6380.conf [root@iZ2vc8owmlobwkazif1efpZ redis-config]# cp redis.conf redis6381.conf [root@iZ2vc8owmlobwkazif1efpZ redis-config]# ls redis6379.conf redis6380.conf redis6381.conf redis.conf [root@iZ2vc8owmlobwkazif1efpZ redis-config]# # 6379端口的主节点配置文件修改内容 logfile "6379.log" dbfilename dump6379.rdb # 6380端口的从节点配置文件修改内容 port 6380 pidfile /var/run/redis_6380.pid logfile "6380.log" dbfilename dump6380.rdb # 6381端口的从节点配置文件修改内容 port 6381 pidfile /var/run/redis_6381.pid logfile "6381.log" dbfilename dump6381.rdb
# 主节点6379 [root@iZ2vc8owmlobwkazif1efpZ bin]# redis-server ./redis-config/redis6379.conf [root@iZ2vc8owmlobwkazif1efpZ bin]# redis-cli 127.0.0.1:6379> # 6380、6381端口的也打开,打开之后出现log文件证明成功开启服务 [root@iZ2vc8owmlobwkazif1efpZ bin]# ls 6379.log 6381.log redis-benchmark redis-check-rdb redis-config redis-server 6380.log dump.rdb redis-check-aof redis-cli redis-sentinel [root@iZ2vc8owmlobwkazif1efpZ bin]# # 查看进程 [root@iZ2vc8owmlobwkazif1efpZ bin]# ps -ef | grep redis root 5876 5554 0 16:13 pts/0 00:00:00 vim redis6379.conf root 6186 1 0 16:27 ? 00:00:02 redis-server 127.0.0.1:6379 root 6191 5938 0 16:27 pts/4 00:00:00 redis-cli root 6271 1 0 16:29 ? 00:00:02 redis-server 127.0.0.1:6380 root 6581 1 0 16:40 ? 00:00:00 redis-server 127.0.0.1:6381 root 6588 6317 0 16:41 pts/6 00:00:00 grep --color=auto redis
slaveof 127.0.0.1 端口号
)# 默认情况下,开启的服务都是主节点 127.0.0.1:6379> info replication # Replication role:master connected_slaves:0 master_failover_state:no-failover master_replid:2233ccc60b5d8b3cac1686762352c4c224fa1a3c master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 127.0.0.1:6379> # 6380 127.0.0.1:6380> info replication # Replication role:master connected_slaves:0 master_failover_state:no-failover master_replid:aec6b1b1ac65ec1030db5a040de2c80735e36160 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 127.0.0.1:6380> # 6381 127.0.0.1:6381> info replication # Replication role:master connected_slaves:0 master_failover_state:no-failover master_replid:4468e5aaba84fd369fb0d3dc1a6146bbe4b5c4f6 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 127.0.0.1:6381> # 6380从节点 寻找 主节点 127.0.0.1:6380> slaveof 127.0.0.1 6379 # 寻找主节点 OK 127.0.0.1:6380> info replication # Replication role:slave # 当前角色 master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:3 master_sync_in_progress:0 slave_repl_offset:0 slave_priority:100 slave_read_only:1 connected_slaves:0 master_failover_state:no-failover master_replid:8b032abbed7a722dfcc9edb27961a4a3500bfe23 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:0 127.0.0.1:6380> # 6381 从节点 寻找 主节点 127.0.0.1:6381> slaveof 127.0.0.1 6379 # 配置 主节点 OK 127.0.0.1:6381> info replication # Replication role:slave # 当前角色 master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:3 master_sync_in_progress:0 slave_repl_offset:112 slave_priority:100 slave_read_only:1 connected_slaves:0 master_failover_state:no-failover master_replid:8b032abbed7a722dfcc9edb27961a4a3500bfe23 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:112 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:113 repl_backlog_histlen:0 127.0.0.1:6381>
replicationof masterip masterport
)127.0.0.1:6379> info replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6380,state=online,offset=252,lag=0 # 从节点1 slave1:ip=127.0.0.1,port=6381,state=online,offset=252,lag=0 # 从节点2 master_failover_state:no-failover master_replid:8b032abbed7a722dfcc9edb27961a4a3500bfe23 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:252 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:252 127.0.0.1:6379>
# 1. 主节点设置、读取数据 127.0.0.1:6379> keys * (empty array) 127.0.0.1:6379> set name xiaosi OK 127.0.0.1:6379> get name "xiaosi" 127.0.0.1:6379> # 2. 从节点读取数据,但是不能保存新数据 127.0.0.1:6380> keys * (empty array) 127.0.0.1:6380> keys * 1) "name" 127.0.0.1:6380> get name # 从节点读取数据没问题 "xiaosi" 127.0.0.1:6380> set age 20 # 从节点保存新数据报错 (error) READONLY You can't write against a read only replica. 127.0.0.1:6380>
# 主节点宕机,从节点也能正常读取数据 127.0.0.1:6380> info replication # Replication role:slave # 角色没有变 master_host:127.0.0.1 master_port:6379 master_link_status:down # 主节点状态 master_last_io_seconds_ago:-1 master_sync_in_progress:0 slave_repl_offset:1444 master_link_down_since_seconds:18 slave_priority:100 slave_read_only:1 connected_slaves:0 master_failover_state:no-failover master_replid:8b032abbed7a722dfcc9edb27961a4a3500bfe23 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:1444 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:1444 127.0.0.1:6380> get name # 依旧能正常拿到数据 "xiaosi" 127.0.0.1:6380> # 恢复主节点之后,依然正常 [root@iZ2vc8owmlobwkazif1efpZ bin]# redis-cli 127.0.0.1:6379> get name "xiaosi" 127.0.0.1:6379> set age 20 OK 127.0.0.1:6379> get age "20" 127.0.0.1:6379> # 从节点的读取也没问题 127.0.0.1:6380> get age "20" 127.0.0.1:6380>
# 1. 只剩一个从节点,断掉一个6380的从节点 127.0.0.1:6379> info replication # Replication role:master connected_slaves:1 slave0:ip=127.0.0.1,port=6381,state=online,offset=417,lag=1 master_failover_state:no-failover master_replid:5bab385547cbbe77d3daaf46e0cc3d8433b4e9c7 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:417 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:417 127.0.0.1:6379> # 2. 主节点此时保存数据,之后从节点恢复(默认之前命令行配置的从节点,启动redis服务都是作为主节点)读取不了该条数据 [root@iZ2vc8owmlobwkazif1efpZ bin]# redis-server ./redis-config/redis6380.conf [root@iZ2vc8owmlobwkazif1efpZ bin]# redis-cli -p 6380 127.0.0.1:6380> keys * # 还未作为6379的从节点,college 拿不到 1) "age" 2) "name" 127.0.0.1:6380> # 3. 重新作为从节点,宕机过程中 主节点保存的数据 可以拿得到 127.0.0.1:6380> keys * # 未作为从节点之前 1) "age" 2) "name" 127.0.0.1:6380> slaveof 127.0.0.1 6379 OK 127.0.0.1:6380> keys * # 作为从节点之后 1) "age" 2) "name" 3) "college" 127.0.0.1:6380>
总结
原理
说明
举栗
1.修改6381端口的节点,使其成为6380端口节点的 从节点
# 6379作为总结点时查看数据 127.0.0.1:6381> keys * 1) "name" 2) "age" 3) "college" 127.0.0.1:6381> slaveof 127.0.0.1 6380 OK 127.0.0.1:6381> keys * 1) "name" 2) "age" 3) "college" 127.0.0.1:6381> # 查看节点信息 127.0.0.1:6381> info replication # Replication role:slave master_host:127.0.0.1 master_port:6380 # 当前从节点的 主节点为6380 master_link_status:up master_last_io_seconds_ago:7 master_sync_in_progress:0 slave_repl_offset:2945 slave_priority:100 slave_read_only:1 connected_slaves:0 master_failover_state:no-failover master_replid:5bab385547cbbe77d3daaf46e0cc3d8433b4e9c7 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:2945 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:2945 127.0.0.1:6381>
2.查看6379、6380端口头主节点的信息
# 6379 127.0.0.1:6379> info replication # Replication role:master connected_slaves:1 slave0:ip=127.0.0.1,port=6380,state=online,offset=2819,lag=0 master_failover_state:no-failover master_replid:5bab385547cbbe77d3daaf46e0cc3d8433b4e9c7 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:2819 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:2819 127.0.0.1:6379> # 6380 # Replication role:slave # 角色仍然为从节点 master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:5 master_sync_in_progress:0 slave_repl_offset:3043 slave_priority:100 slave_read_only:1 connected_slaves:1 slave0:ip=127.0.0.1,port=6381,state=online,offset=3043,lag=1 master_failover_state:no-failover master_replid:5bab385547cbbe77d3daaf46e0cc3d8433b4e9c7 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:3043 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:972 repl_backlog_histlen:2072 127.0.0.1:6380>
3.测试主从复制结构是否正常能存储、读取值
# 6379 主存储、读取数据 127.0.0.1:6379> set salary 16k OK 127.0.0.1:6379> keys * 1) "salary" 2) "college" 3) "name" 4) "age" 127.0.0.1:6379> get salary "16k" 127.0.0.1:6379> # 6380 读取数据 127.0.0.1:6380> keys * 1) "age" 2) "name" 3) "salary" 4) "college" 127.0.0.1:6380> get salary # 可以正常读取数据 "16k" 127.0.0.1:6380> set car audi # 尽管作为6381的主节点,还是不能存储数据 (error) READONLY You can't write against a read only replica. 127.0.0.1:6380> # 6381 读取数据 127.0.0.1:6381> keys * 1) "salary" 2) "name" 3) "age" 4) "college" 127.0.0.1:6381> get salary # 能够正常的读取数据 "16k" 127.0.0.1:6381>
问题
解决
slaveof no one
即可升级当前二级总结点为 一级总结点(谋权篡位)slave id port
,如果是配置文件配置的话,每次需要手动修改配置文件,很不方便什么是哨兵模式
sentinel.conf
# 最基本的哨兵配置,最后的1是 开启slave投票选举的新主节点 sentinel monitor myredis 127.0.0.1 6379 1
redis-sentinel sentinel.conf
# 1. 链式结构的主从复制,6379为 主节点 [root@iZ2vc8owmlobwkazif1efpZ bin]# redis-sentinel ./redis-config/sentinel.conf 8223:X 05 Jul 2021 18:24:43.683 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 8223:X 05 Jul 2021 18:24:43.683 # Redis version=6.2.1, bits=64, commit=00000000, modified=0, pid=8223, just started 8223:X 05 Jul 2021 18:24:43.683 # Configuration loaded 8223:X 05 Jul 2021 18:24:43.683 * monotonic clock: POSIX clock_gettime _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 6.2.1 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in sentinel mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 26379 | `-._ `._ / _.-' | PID: 8223 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 8223:X 05 Jul 2021 18:24:43.687 # Sentinel ID is c14e20c17e5360dd7e5a1dd2e365cd2be822adce 8223:X 05 Jul 2021 18:24:43.687 # +monitor master myredis 127.0.0.1 6379 quorum 1 8223:X 05 Jul 2021 18:24:43.687 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ myredis 127.0.0.1 6379 # 监控信息 # 2.更新为树形结构的主从复制 [root@iZ2vc8owmlobwkazif1efpZ bin]# redis-sentinel ./redis-config/sentinel.conf 8223:X 05 Jul 2021 18:24:43.683 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 8223:X 05 Jul 2021 18:24:43.683 # Redis version=6.2.1, bits=64, commit=00000000, modified=0, pid=8223, just started 8223:X 05 Jul 2021 18:24:43.683 # Configuration loaded 8223:X 05 Jul 2021 18:24:43.683 * monotonic clock: POSIX clock_gettime _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 6.2.1 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in sentinel mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 26379 | `-._ `._ / _.-' | PID: 8223 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 8223:X 05 Jul 2021 18:24:43.687 # Sentinel ID is c14e20c17e5360dd7e5a1dd2e365cd2be822adce 8223:X 05 Jul 2021 18:24:43.687 # +monitor master myredis 127.0.0.1 6379 quorum 1 8223:X 05 Jul 2021 18:24:43.687 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ myredis 127.0.0.1 6379 8223:X 05 Jul 2021 18:25:43.993 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ myredis 127.0.0.1 6379
# 1. 关闭当前主节点6379 [root@iZ2vc8owmlobwkazif1efpZ bin]# redis-sentinel ./redis-config/sentinel.conf 8625:X 05 Jul 2021 18:38:31.631 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 8625:X 05 Jul 2021 18:38:31.632 # Redis version=6.2.1, bits=64, commit=00000000, modified=0, pid=8625, just started 8625:X 05 Jul 2021 18:38:31.632 # Configuration loaded 8625:X 05 Jul 2021 18:38:31.632 * monotonic clock: POSIX clock_gettime _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 6.2.1 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in sentinel mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 26379 | `-._ `._ / _.-' | PID: 8625 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 8625:X 05 Jul 2021 18:38:31.632 # Sentinel ID is c14e20c17e5360dd7e5a1dd2e365cd2be822adce 8625:X 05 Jul 2021 18:38:31.632 # +monitor master myredis 127.0.0.1 6379 quorum 1 8625:X 05 Jul 2021 18:39:41.155 # +sdown master myredis 127.0.0.1 6379 8625:X 05 Jul 2021 18:39:41.155 # +odown master myredis 127.0.0.1 6379 #quorum 1/1 8625:X 05 Jul 2021 18:39:41.155 # +new-epoch 1 8625:X 05 Jul 2021 18:39:41.155 # +try-failover master myredis 127.0.0.1 6379 8625:X 05 Jul 2021 18:39:41.158 # +vote-for-leader c14e20c17e5360dd7e5a1dd2e365cd2be822adce 1 8625:X 05 Jul 2021 18:39:41.158 # +elected-leader master myredis 127.0.0.1 6379 8625:X 05 Jul 2021 18:39:41.158 # +failover-state-select-slave master myredis 127.0.0.1 6379 8625:X 05 Jul 2021 18:39:41.229 # +selected-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ myredis 127.0.0.1 6379 8625:X 05 Jul 2021 18:39:41.229 * +failover-state-send-slaveof-noone slave 127.0.0.1:6380 127.0.0.1 6380 @ myredis 127.0.0.1 6379 8625:X 05 Jul 2021 18:39:41.295 * +failover-state-wait-promotion slave 127.0.0.1:6380 127.0.0.1 6380 @ myredis 127.0.0.1 6379 8625:X 05 Jul 2021 18:39:41.595 # +promoted-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ myredis 127.0.0.1 6379 8625:X 05 Jul 2021 18:39:41.595 # +failover-state-reconf-slaves master myredis 127.0.0.1 6379 8625:X 05 Jul 2021 18:39:41.658 * +slave-reconf-sent slave 127.0.0.1:6381 127.0.0.1 6381 @ myredis 127.0.0.1 6379 8625:X 05 Jul 2021 18:39:42.623 * +slave-reconf-inprog slave 127.0.0.1:6381 127.0.0.1 6381 @ myredis 127.0.0.1 6379 8625:X 05 Jul 2021 18:39:42.623 * +slave-reconf-done slave 127.0.0.1:6381 127.0.0.1 6381 @ myredis 127.0.0.1 6379 8625:X 05 Jul 2021 18:39:42.674 # +failover-end master myredis 127.0.0.1 6379 8625:X 05 Jul 2021 18:39:42.675 # +switch-master myredis 127.0.0.1 6379 127.0.0.1 6380 8625:X 05 Jul 2021 18:39:42.675 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ myredis 127.0.0.1 6380 8625:X 05 Jul 2021 18:39:42.675 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ myredis 127.0.0.1 6380 8625:X 05 Jul 2021 18:40:12.718 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ myredis 127.0.0.1 6380 # 2. 发现当前主节点成为了 6380 127.0.0.1:6380> info replication # Replication role:master connected_slaves:1 slave0:ip=127.0.0.1,port=6381,state=online,offset=68469,lag=0 master_failover_state:no-failover master_replid:622f574df7ed615814c655f50c4b0f71b0ccf340 master_replid2:5bab385547cbbe77d3daaf46e0cc3d8433b4e9c7 master_repl_offset:68469 second_repl_offset:60227 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:56952 repl_backlog_histlen:11518 127.0.0.1:6380>
# 恢复6379 [root@iZ2vc8owmlobwkazif1efpZ bin]# cd /usr/local/bin/ [root@iZ2vc8owmlobwkazif1efpZ bin]# redis-server ./redis-config/redis6379.conf [root@iZ2vc8owmlobwkazif1efpZ bin]# redis-cli 127.0.0.1:6379> info replication # Replication role:slave # 变成6380的从节点 master_host:127.0.0.1 master_port:6380 master_link_status:up master_last_io_seconds_ago:0 master_sync_in_progress:0 slave_repl_offset:93312 slave_priority:100 slave_read_only:1 connected_slaves:0 master_failover_state:no-failover master_replid:622f574df7ed615814c655f50c4b0f71b0ccf340 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:93312 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:92748 repl_backlog_histlen:565 127.0.0.1:6379> # 哨兵日志 8625:X 05 Jul 2021 18:47:41.107 # -sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ myredis 127.0.0.1 6380 8625:X 05 Jul 2021 18:47:51.114 * +convert-to-slave slave 127.0.0.1:6379 127.0.0.1 6379 @ myredis 127.0.0.1 6380
优点
缺点
# Example sentinel.conf # 哨兵sentinel实例运行的端口 默认26379 port 26379 # 哨兵sentinel的工作目录 dir /tmp # 哨兵sentinel监控的redis主节点的 ip port # master-name 可以自己命名的主节点名字 只能由字母A-z、数字0-9 、这三个字符".-_"组成。 # quorum 配置多少个sentinel哨兵统一认为master主节点失联 那么这时客观上认为主节点失联了 # sentinel monitor sentinel monitor mymaster 127.0.0.1 6379 2 # 当在Redis实例中开启了requirepass foobared 授权密码 这样所有连接Redis实例的客户端都要提供密码 # 设置哨兵sentinel 连接主从的密码 注意必须为主从设置一样的验证密码 # sentinel auth-pass sentinel auth-pass mymaster MySUPER--secret-0123passw0rd # 指定多少毫秒之后 主节点没有应答哨兵sentinel 此时 哨兵主观上认为主节点下线 默认30秒 # sentinel down-after-milliseconds sentinel down-after-milliseconds mymaster 30000 # 这个配置项指定了在发生failover主备切换时最多可以有多少个slave同时对新的master进行 同步, 这个数字越小,完成failover所需的时间就越长, 但是如果这个数字越大,就意味着越 多的slave因为replication而不可用。 可以通过将这个值设为 1 来保证每次只有一个slave 处于不能处理命令请求的状态。 # sentinel parallel-syncs sentinel parallel-syncs mymaster 1 # 故障转移的超时时间 failover-timeout 可以用在以下这些方面: #1. 同一个sentinel对同一个master两次failover之间的间隔时间。 #2. 当一个slave从一个错误的master那里同步数据开始计算时间。直到slave被纠正为向正确的master那里同步数据时。 #3.当想要取消一个正在进行的failover所需要的时间。 #4.当进行failover时,配置所有slaves指向新的master所需的最大时间。不过,即使过了这个超时,slaves依然会被正确配置为指向master,但是就不按parallel-syncs所配置的规则来了 # 默认三分钟 # sentinel failover-timeout sentinel failover-timeout mymaster 180000 # SCRIPTS EXECUTION #配置当某一事件发生时所需要执行的脚本,可以通过脚本来通知管理员,例如当系统运行不正常时发邮件通知相关人员。 #对于脚本的运行结果有以下规则: #若脚本执行后返回1,那么该脚本稍后将会被再次执行,重复次数目前默认为10 #若脚本执行后返回2,或者比2更高的一个返回值,脚本将不会重复执行。 #如果脚本在执行过程中由于收到系统中断信号被终止了,则同返回值为1时的行为相同。 #一个脚本的最大执行时间为60s,如果超过这个时间,脚本将会被一个SIGKILL信号终止,之后重新执行。 #通知型脚本:当sentinel有任何警告级别的事件发生时(比如说redis实例的主观失效和客观失效等等),将会去调用这个脚本, 这时这个脚本应该通过邮件,SMS等方式去通知系统管理员关于系统不正常运行的信息。调用该脚本时,将传给脚本两个参数, 一个是事件的类型, 一个是事件的描述。 如果sentinel.conf配置文件中配置了这个脚本路径,那么必须保证这个脚本存在于这个路径,并且是可执行的,否则sentinel无法正常启动成功。 #通知脚本 # sentinel notification-script sentinel notification-script mymaster /var/redis/notify.sh # 客户端重新配置主节点参数脚本 # 当一个master由于failover而发生改变时,这个脚本将会被调用,通知相关的客户端关于master地址已经发生改变的信息。 # 以下参数将会在调用脚本时传给脚本: # # 目前总是“failover”, # 是“leader”或者“observer”中的一个。 # 参数 from-ip, from-port, to-ip, to-port是用来和旧的master和新的master(即旧的slave)通信的 # 这个脚本应该是通用的,能被多次调用,不是针对性的。 # sentinel client-reconfig-script sentinel client-reconfig-script mymaster /var/redis/reconfig.sh
场景
加一个过滤器:像布隆过滤器,是一种数据结构,对所有可能查询到的参数以hash的形式存储,在控制层先进行校验,不符合则丢弃,从而避免对从底层存储系统的压力。
设置空缓存,在缓存中查询不到返回一个空结果
这种方式存在问题,空值对应的键需要更多空间来存储;即使对空值设置了过期时间,还是会存在缓存层和数据层会有一段时间窗口不一致,这对于需要保持一致型的业务会有影响。