1. Redis持久化
redis可以将数据写入到磁盘中,在停机或宕机后,再次启动redis时,将磁盘中的备份数据加载到内存中恢复使用。这是redis的持久化。持久化有两种机制:RDB 快照持久化和AOF 追加文件持久化。
1.1 RDB 快照持久化
redis可以将内存中的数据写入磁盘进行持久化。在进行持久化时,redis会创建子进程来执行。
redis默认开启了快照持久化机制。
进行快照持久化的时机如下:
定期触发
redis的配置文件redis.conf相关内容如下:
# save
#
# Will save the DB if both the given number of seconds and the given
# number of write operations against the DB occurred.
#
# In the example below the behaviour will be to save:
# after 900 sec (15 min) if at least 1 key changed
# after 300 sec (5 min) if at least 10 keys changed
# after 60 sec if at least 10000 keys changed
#
# Note: you can disable saving completely by commenting out all "save" lines.
#
# It is also possible to remove all the previously configured save
# points by adding a save directive with a single empty string argument
# like in the following example:
#
# save ""
save 900 1 # 在900秒(15分钟)之后,如果至少有1个key发生变化,Redis就会自动触发BGSAVE命令创建快照。
save 300 10 #在300秒(5分钟)之后,如果至少有10个key发生变化,Redis就会自动触发BGSAVE命令创建快照。
save 60 10000 #在60秒(1分钟)之后,如果至少有10000个key发生变化,Redis就会自动触发BGSAVE命令创建快照。
BGSAVE
执行
BGSAVE
命令,手动触发RDB持久化SHUTDOWN
关闭redis时触发
1.2 AOF 追加文件持久化
redis可以将执行的所有指令追加记录到文件中持久化存储,这是redis的另一种持久化机制。
redis默认未开启AOF机制。
redis可以通过修改配置如下项开启AOF机制
appendonly yes # 是否开启AOF
appendfilename "appendonly.aof" # AOF文件
AOF机制记录操作的时机
# appendfsync always # 每个操作都写到磁盘中
appendfsync everysec # 每秒写一次磁盘,默认
# appendfsync no # 由操作系统决定写入磁盘的时机
使用AOF机制的缺点是随着时间的流逝,AOF文件会变得很大。但redis可以压缩AOF文件。
1.3 结合使用
redis允许我们同时使用两种机制,通常情况下我们会设置AOF机制为everysec 每秒写入,则最坏仅会丢失一秒内的数据。
2.Redis高可用
为了保证redis最大程度上能够使用,redis提供了主从同步+Sentinel哨兵机制。
2.1 Sentinel 哨兵
redis提供的哨兵是用来看护redis实例进程的,可以自动进行故障转移,其功能如下:
Monitoring. Sentinel constantly checks if your master and slave instances are working as expected.
Notification. Sentinel can notify the system administrator, another computer programs, via an API, that something is wrong with one of the monitored Redis instances.
Automatic failover. If a master is not working as expected, Sentinel can start a failover process where a slave is promoted to master, the other additional slaves are reconfigured to use the new master, and the applications using the Redis server informed about the new address to use when connecting.
Configuration provider. Sentinel acts as a source of authority for clients service discovery: clients connect to Sentinels in order to ask for the address of the current Redis master responsible for a given service. If a failover occurs, Sentinels will report the new address
简单理解就是哨兵带有一个心跳机制,会一直去check主服务器是否发生故障,一旦发生故障,就会立即取代原来的主服务器,变成新的主服务器,以前的主服务器就算恢复了也自动变成了从服务器。
在redis安装后,会自带sentinel哨兵程序,修改sentinel.conf配置文件
bind 172.16.33.128
port 26380
daemonize yes
logfile /var/log/redis-sentinel.log
sentinel monitor mymaster 172.16.33.127 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000 # 超时
sentinel monitor mymaster 172.16.33.127 6379 2说明
mymaster 为sentinel监护的redis主从集群起名
172.16.33.127 6379 为主从中任一台机器地址
2 表示有两台以上的sentinel认为某一台redis宕机后,才会进行自动故障转移。
启动方式:
redis-sentinel sentinel.conf
注意事项
至少三个sentinel以上
sentinel要分散运行在不同的机器上
2.2 Python客户端使用
安装
pip install redis
配置
在Flask中配置内容如下:
# redis 哨兵
REDIS_SENTINELS = [
('172.16.33.128', '26379'),
('172.16.33.129', '26379'),
('172.16.33.130', '26379'),
]
REDIS_SENTINEL_SERVICE_NAME = 'mymaster'
from redis.sentinel import Sentinel
_sentinel = Sentinel(REDIS_SENTINELS)
redis_master = _sentinel.master_for(REDIS_SENTINEL_SERVICE_NAME)
redis_slave = _sentinel.slave_for(REDIS_SENTINEL_SERVICE_NAME)
使用示例
# 读数据,master读不到数据去slave读
try:
real_code = redis_master.get(key)
except ConnectionError as e:
real_code = redis_slave.get(key)
# 写数据,只能在master里写
try:
current_app.redis_master.delete(key)
except ConnectionError as e:
logger.error(e)
3. Redis集群
Reids Cluster集群方案,内部已经集成了sentinel
机制来做到高可用。
Python客户端需要安装redis-py-cluster
.
pip install redis-py-cluster
使用
# redis 集群
# 构建所有的节点,Redis会使⽤CRC16算法,将键和值写到某个节点上
REDIS_CLUSTER = [
{'host': '172.16.33.125', 'port': '6379'},
{'host': '172.16.33.126', 'port': '6379'},
{'host': '172.16.33.127', 'port': '6379'},
]
from rediscluster import StrictRedisCluster
redis_cluster = StrictRedisCluster(startup_nodes=REDIS_CLUSTER)
# 可以将redis_cluster就当作普通的redis客户端使用
redis_master.set('name','python')
注意:
redis cluster 不支持事务
redis cluster 不支持多键操作,如mset
相关链接:
《Redis实践》 (Redis in action)