Redis持久化

  • RDB-Redis DataBase
  • AOF-Append Only File

RDB

RDB是在指定的时间间隔内将内存中的数据集快照写入磁盘,也就是行话讲的Snapshot快照,它恢复时是将快照文件直接读到内存里。
Redis会单独创建(fork)一个子进程来进行持久化,会先将数据写入到一个临时文件中,待持久化过程都结束了,再用这个临时文件替换上次持久化好的文件。整个过程中,主进程不需要进行任何IO操作,这就确保了极高的性能,如果需要进行大规模数据的恢复,且对数据恢复的完整性不是非常敏感,那RDB方式要比AOF方式更加高效。RDB的缺点是最后一次持久化后的数据可能丢失。

FORK:Fork的作用是复制一个与当前进程一样的进程,新进程的所有数据(变量,环境变量,程序计数器等)和原进程一致。但是一个全新的进程,且作为原进程的子进程,可以理解为进程间的拷贝。

#Rdb保持的是dump.rdb文件

RDB配置文件
################################ SNAPSHOTTING  ################################
#
# Save the DB on disk:
#
#   save <seconds> <changes>

//如果想禁用RDB持久化策略,只要不设置任何save指令,或者传入一个空字符参数也可以
#   save ""




//指定在多长时间内,有多少次更新操作,就将数据同步到数据文件,可以多个条件配合--save < seconds> < changes> 
//Redis默认配置文件中提供了三个条件
// save 900 1;save 300 10;save 60 10000 ;
//分别表示900秒内有一个更改;300秒内有10个更改等..
//想要让写入的数据即刻生效,不按持久化策略进行,在写完数据直接save即可。
//save从自动备份到手动即刻备份
save 900 1
save 300 10
save 60 10000

# By default Redis will stop accepting writes if RDB snapshots are enabled
# (at least one save point) and the latest background save failed.
# This will make the user aware (in a hard way) that data is not persisting
# on disk properly, otherwise chances are that no one will notice and some
# disaster will happen.
#
# If the background saving process will start working again Redis will
# automatically allow writes again.
#
# However if you have setup your proper monitoring of the Redis server
# and persistence, you may want to disable this feature so that Redis will
# continue to work as usual even if there are problems with disk,
# permissions, and so forth.

//配置成yes,表示如果数据持久化出错,则停止写入数据
//配置成no,表示你不在乎数据不一致或者有其他手段进行发现和控制
stop-writes-on-bgsave-error yes

# Compress string objects using LZF when dump .rdb databases?
# For default that's set to 'yes' as it's almost always a win.
# If you want to save some CPU in the saving child set it to 'no' but
# the dataset will likely be bigger if you have compressible values or keys.
//是否启动压缩算法
//对于存储到磁盘中的快照,可以设置是否进行压缩存储。如果是的话,redis会采用LZF算法进行压缩。
//如果不想消耗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.
//在存储快照后,还可以让redis使用CRC64算法进行数据校验,但是这样做会增加大约10%的性能消耗
//如果希望获得最大的性能提升,可以关闭此功能
rdbchecksum yes

# The filename where to dump the DB
//指定本地数据库文件名
dbfilename dump.rdb

# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
//指定数据库文件存放所在目录
dir ./

ps:命令save或者是bgsave可以即刻生成dump.rdb文件;执行flushall命令,也会产生dump
.rdb文件,但里面是空的,无意义。

如何恢复

将备份文件dump.rdb移动到redis安装目录并启动服务即可

优势

适合大规模的数据恢复,且对数据完整性和一致性要求不高

劣势

在一定间隔时间做一次备份,所以如果redis意外down掉的话,就会丢失最后一次快照的所有修改;
Fork的时候,内存中的数据被克隆了一份,大致2倍的膨胀性需要考虑

如何停止
redis-cli config set save""
RDB小结

redis fork进程作用_redis


AOF

以日志的形式来记录每个写操作,将Redis执行过的所有写指令记录(读操作不记录),只许追加文件但不可以改写文件,redis启动之初会读取该文件重新构建数据,换言之,redis重启的话就根据日志文件的内容将写指令从前到后执行一次以完成数据的恢复工作

#Aof保存的是appendonly.aof文件

AOF配置文件
############################## APPEND ONLY MODE ###############################

# By default Redis asynchronously dumps the dataset on disk. This mode is
# good enough in many applications, but an issue with the Redis process or
# a power outage may result into a few minutes of writes lost (depending on
# the configured save points).
#
# 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.
#
//AOP和RDB可以共存
# 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.
#
# Please check http://redis.io/topics/persistence for more information.
//指定是否在每次更新操作后进行日志记录
//Redis默认情况下是不把异步的数据写入磁盘。如果不开启,可能会在断电时导致一部分数据丢失
//Redis本身同步数据文件是按上面save条件来同步的,存在有的数据在一段时间只存在于内存,而不在磁盘中
appendonly no

# The name of the append only file (default: "appendonly.aof")
//指定更新日志文件名
appendfilename "appendonly.aof"

# The fsync() call tells the Operating System to actually write data on disk
# instead of waiting for more data in the output buffer. Some OS will really flush
# data on disk, some other OS will just try to do it ASAP.
#
# Redis supports three different modes:
#
# no: don't fsync, just let the OS flush the data when it wants. Faster.
# always: fsync after every write to the append only log. Slow, Safest.
# everysec: fsync only one time every second. Compromise.
#
# The default is "everysec", as that's usually the right compromise between
# speed and data safety. It's up to you to understand if you can relax this to
# "no" that will let the operating system flush the output buffer when
# it wants, for better performances (but if you can live with the idea of
# some data loss consider the default persistence mode that's snapshotting),
# or on the contrary, use "always" that's very slow but a bit safer than
# everysec.
#
# More details please check the following article:
# http://antirez.com/post/redis-persistence-demystified.html
#
# If unsure, use "everysec".

//指定AOF配置策略
//共有三个可选值
//no:表示等操作系统进行数据缓存同步到磁盘(快)
//always:同步持久化,每次发生数据变更会被立即记录到磁盘中,性能较差但数据完整性好
//everysec 表示每秒同步一次(折中,默认值),如果一秒内死机,有数据丢失
# appendfsync always
appendfsync everysec
# appendfsync no

# When the AOF fsync policy is set to always or everysec, and a background
# saving process (a background save or AOF log background rewriting) is
# performing a lot of I/O against the disk, in some Linux configurations
# Redis may block too long on the fsync() call. Note that there is no fix for
# this currently, as even performing fsync in a different thread will block
# our synchronous write(2) call.
#
# In order to mitigate this problem it's possible to use the following option
# that will prevent fsync() from being called in the main process while a
# BGSAVE or BGREWRITEAOF is in progress.
#
# This means that while another child is saving, the durability of Redis is
# the same as "appendfsync none". In practical terms, this means that it is
# possible to lose up to 30 seconds of log in the worst scenario (with the
# default Linux settings).
#
# If you have latency problems turn this to "yes". Otherwise leave it as
# "no" that is the safest pick from the point of view of durability.

no-appendfsync-on-rewrite no

# Automatic rewrite of the append only file.
# latest rewrite (if no rewrite has happened since the restart, the size of
# the AOF at startup is used).
#
# This base size is compared to the current size. If the current size is
# bigger than the specified percentage, the rewrite is triggered. Also
# you need to specify a minimal size for the AOF file to be rewritten, this
# is useful to avoid rewriting the AOF file even if the percentage increase
# is reached but it is still pretty small.
#
# Specify a percentage of zero in order to disable the automatic AOF
# rewrite feature.

//重写机制
//设置重写的基准值
//默认配置是当AOF文件大小是上次rewrite后大小的一倍且文件大于64M时触发
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

# An AOF file may be found to be truncated at the end during the Redis
# startup process, when the AOF data gets loaded back into memory.
# This may happen when the system where Redis is running
# crashes, especially when an ext4 filesystem is mounted without the
# data=ordered option (however this can't happen when Redis itself
# crashes or aborts but the operating system still works correctly).
#
# Redis can either exit with an error when this happens, or load as much
# data as possible (the default now) and start if the AOF file is found
# to be truncated at the end. The following option controls this behavior.
#
# If aof-load-truncated is set to yes, a truncated AOF file is loaded and
# the Redis server starts emitting a log to inform the user of the event.
# Otherwise if the option is set to no, the server aborts with an error
# and refuses to start. When the option is set to no, the user requires
# to fix the AOF file using the "redis-check-aof" utility before to restart
# the server.

# Note that if the AOF file will be found to be corrupted in the middle
# the server will still exit with an error. This option only applies when
# Redis will try to read more data from the AOF file but not enough bytes
# will be found.
aof-load-truncated yes

如果AOF文件出错,可以使用check命令修复
//修复dump就是redis-check-dump --fix …

redis-check-aof --fix appendonly.aof
Rewrite

AOF采用文件追加方式,文件会越来越大,为了避免出现这种情况,新增了重写机制。当AOF文件的大小超过所设定的阈值时,Redis就会启动AOF文件的内容压缩,只保留可以恢复数据的最小指令集,可以使用命令bgrewriteaof

重写原理

AOF文件持续增长而过大时,会fork出一条新进程来将文件重写(也是先写临时文件再rename),遍历新进程内存中的数据,每条记录有一条的set语句。重写aof文件的操作,并没有读取旧的aof文件,而是将内存中的数据库内容用命令的方式重写了一个新的aof文件,这点和快照有点类似。

重写触发机制

Redis会记录上次重写时AOF的大小,默认配置是当AOF文件大小是上次rewrite后大小的一倍且文件大于64M时触发

优势

灵活AOF配置机制

劣势

相同数据集而言 aof文件要远大于rdb文件,恢复速度慢于rdb
aof运行效率要慢于rdb,每秒同步策略效率较好,不同步效率和rdb相同

AOF小结

redis fork进程作用_redis_02