Redis 是内存数据库,如果不将内存中的数据库状态保存到磁盘,那么一旦服务器进程退出会造成服务器中的数据库状态也会消失。所以 Redis 提供了数据持久化功能。Redis支持两种方式的持久化,一种是RDB方式;另一种是AOF(append-only-file)方式。两种持久化方式可以单独使用,也可以将这两种方式结合使用。
视频讲解如下:
Redis的数据持久化 |
---|
【赵渝强老师】Redis的数据持久化 |
AOF持久化以日志的形式记录服务器所处理的每一个写、删除操作,查询操作不会记录,以文本的方式记录,可以打开文件看到详细的操作记录。Redis还可以同时使用AOF持久化和RDB持久化。在这种情况下当重新启动时,Redis会优先使用AOF文件来还原数据集。因为AOF文件保存的数据集通常比RDB文件所保存的数据集更完整。
视频讲解如下:
Redis的AOF数据持久化 |
---|
【赵渝强老师】Redis的AOF数据持久化 |
一、AOF持久化机制的工作流程
Redis提供的AOF持久化机制的工作流程如下:
(1)所有的命令都会追加到AOF缓冲区中。
(2)AOF缓冲区根据对应的策略向磁盘同步操作。
(3)随着AOF文件越来越大,需要定期对AOF重写,达到压缩目的。
(4)当Redis服务器重启时,可以加载AOF文件进行数据恢复。
但在默认情况下,Redis关闭了AOF持久化的功能。这是由redis.conf中下面的参数决定的:
###### APPEND ONLY MODE ######
......
# The Append Only File is an alternative persistence mode that provides
# much better durability. For instance using the default data fsync policy
# (see later in the config file) Redis can lose just one second of
# writes in a dramatic event like a server power outage, or a single
# write if something wrong with the Redis process itself happens,
# but the operating system is still running correctly.
# AOF and RDB persistence can be enabled at the same time without problems.
# If the AOF is enabled on startup Redis will load the AOF, that is
# the file with the better durability guarantees.