redis持久化

SNAPSHOTTING

RDB:redis database

rdb是redis数据持久化的方式。redis采用SNAPSHOTTING技术对数据库中的数据进行持久化,存储在默认名为dump.rdb的文件中。(要注意,这个文件的位置取决于redis-server启动时的路径。)

在redis进行rdb持久化数据时,会fork一个完全一样的进程作为redis的子进程,由子进程来完成数据写入磁盘的工作。这样一来,不会影响redis的效率,因为主进程不进行I/O操作,但是要考虑到存储空间翻倍的问题。

save配置
################################ SNAPSHOTTING  ################################

# 禁用默认的持久化策略
# save ""
# 在save后,900秒检测一次是否有1个键发生变化,300秒检测一次是否有10键变化,60秒检测是否10000个发生了变化,都会触发save。
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error
# 此参数表示在后台将数据写入磁盘时发生错误时是否要继续写入
# 如果配置成no,表示你不在乎数据不一致或者有其他的手段发现和控制

# 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.
stop-writes-on-bgsave-error yes
rdbcompression
# 对于存储到磁盘中的快照,可以设置是否进行压缩存储。如果是的话,redis会采用LZF算法进行压缩。如果你不想消耗CPU来进行压缩的话,可以设置为关闭此功能(推荐开启)

rdbcompression yes
rdbchecksum
# 与rdbcompression类似,可以设置在压缩后进行校验和校验。大约会增加cpu 10%的能耗(推荐开启)
rdbchecksum yes
dbfilename
# The filename where to dump the DB
# 见名知意,就是rdb文件的文件名
dbfilename dump.rdb
dir
# 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.
# drb和aof文件都会存在这个目录中。 ./表示启动所在的目录。
dir ./

APPEND ONLY MODE

从rdb对数据持久化的方式来看,还是存在着一定的缺陷。当redis宕机时,如突然的断电,还是会使没来得及持久化的数据丢失。

aof是redis的另一种持久化方式,简单来说,aop会记录redis的每一条写操作(不记录读操作),并存储在一个aof文件中。

appendonly
aof新增的原因
# 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.
#
# 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.
#  开启 aof
appendonly yes
appendfilename
# The name of the append only file (default: "appendonly.aof")
appendfilename "appendonly.aof"
appendfsync
# appendfsync是aof的模式,有三种。
	- always # 同步持久化,每次数据变更会立即记录到磁盘,性能较差,但数据完整性好
    - everysec # 每秒写入一次,如果一秒内宕机,还是会有数据丢失
    - no # 不进行写入。
no-appendfsync-on-rewrite
# 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.
# 在对aof文件进行重写时是否允许 appendfsync,推荐使用no,保证数据安全性。
no-appendfsync-on-rewrite no
auto-aof-rewrite-min-size
# 设置重写的基准值
auto-aof-rewrite-min-size 64mb
auto-aof-rewrite-percentage
# 设置重写的基准值
auto-aof-rewrite-percentage 100

aof重写

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


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

小结

# RDB持久化方式能够在指定的时间间隔能对你的数据进行快照存储

# AOF持久化方式记录每次对服务器写的操作,当服务器重启的时候会重新执行这些命令来恢复原始的数据,AOF命令以redis协议追加保存每次写的操作到文件末尾.Redis还能对AOF文件进行后台重写,使得AOF文件的体积不至于过大

# 只做缓存:如果你只希望你的数据在服务器运行的时候存在,你也可以不使用任何持久化方式.
# RDB的数据不实时,同时使用两者时服务器重启也只会找AOF文件。那要不要只使用AOF呢?作者建议不要,因为RDB更适合用于备份数据库(AOF在不断变化不好备份),快速重启,而且不会有AOF可能潜在的bug,留着作为一个万一的手段。
原文地址:https://www.cnblogs.com/Ghostant/p/12423661.html