06-持久化
持久化机制
redis的持久化机制有两种:
- RDB snapshotting
- 以指定间隔的时间点对database进行快照
- AOF Append-only
- 记录服务器每次收到的
写操作; - 这些操作可以在redis服务启动时
重放以重建原始的databse
- 记录服务器每次收到的
- No Persistence
- 完全禁止持久化,用于缓存
- RDB+AOF
- 组合RDB和AOF
RDB
- 是一个小巧的基于时间点的数据快照文件,它代表了redis中的数据
- RDB可以完美地用于数据备份;例如每一个小时备份一次前24h的数据;每天备份一次前30天的数据
- RDB可以方便地用于灾难恢复(文件小巧,可以被传输)
- 对于一个很大的database,相比于AOF,RDB可以非常快速地重启
- RDB可能存在数据丢失(最后一次写入RDB到redis宕机这一段时间的数据)
- RDB需要fork一个子进程来操作持久化,如果数据较大且CPU性能较差时可能导致redis短时间的停止服务
RDB 相关配置
################################ SNAPSHOTTING ################################
#
# Save the DB on disk:
# save <seconds> <changes>
# 在满足 seconds 秒内修改了 changes 次key后保存数据快照
#
# 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 ""
# 15分钟后至少有1个key修改
save 900 1
# 6分钟后至少有1个key修改
save 300 10
# 1分钟后至少有1万个key修改
save 60 10000
# 当开启快照且最后一次后台保存失败时,redis会停止尝试RDB快照;这强制可以让用户知道数据没有正确持久化;
# 如果对redis和持久化设置了正确的监视的情况下可以关闭这个特性
stop-writes-on-bgsave-error yes
# 是否开启RDB压缩, 关闭可以节省一些CPU性能
rdbcompression yes
# Since version 5 of RDB a CRC64 checksum is placed at the end of the file.
# This makes the format more resistant to corruption but there is a performance
# hit to pay (around 10%) when saving and loading RDB files, so you can disable it
# for maximum performances.
#
# RDB files created with checksum disabled have a checksum of zero that will
# tell the loading code to skip the check.
# 是否开启校验和检查; 关闭可以提高10%性能;
rdbchecksum yes
# The filename where to dump the DB
dbfilename dump.rdb
#指定 RDB和AOF保存的目录
dir ./

浙公网安备 33010602011771号