redis能够将数据写入到磁盘中,在停机或宕机后,再次启动redis时,将磁盘中的备份数据加载到内存中恢复使用。这是redis的持久化。持久化有两种机制:RDB 快照持久化和AOF 追加文件持久化。node
redis能够将内存中的数据写入磁盘进行持久化。在进行持久化时,redis会建立子进程来执行。python
redis默认开启了快照持久化机制。redis
进行快照持久化的时机以下:算法
按期触发bash
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命令建立快照。复制代码
BGSAVEapp
执行BGSAVE
命令,手动触发RDB持久化ide
SHUTDOWNui
关闭redis时触发spa
redis能够将执行的全部指令追加记录到文件中持久化存储,这是redis的另外一种持久化机制。
redis默认未开启AOF机制。
redis能够经过修改配置以下项开启AOF机制
appendonly yes # 是否开启AOF
appendfilename "appendonly.aof" # AOF文件复制代码
AOF机制记录操做的时机
# appendfsync always # 每一个操做都写到磁盘中
appendfsync everysec # 每秒写一次磁盘,默认
# appendfsync no # 由操做系统决定写入磁盘的时机复制代码
使用AOF机制的缺点是随着时间的流逝,AOF文件会变得很大。但redis能够压缩AOF文件。
redis容许咱们同时使用两种机制,一般状况下咱们会设置AOF机制为everysec 每秒写入,则最坏仅会丢失一秒内的数据。
为了保证redis最大程度上可以使用,redis提供了主从同步+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要分散运行在不一样的机器上
安装
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)复制代码
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)