Redis持久化简介:
当使用Redis进行数据操作的时候,不可避免的能会遇到一些意外的情况导致Redis停止工作,诸如宕机,电源断电等因素。此时Redis提供了RDB以及AOF两种方式来进行数据的备份,两者各有优缺点。可以选择其中的一个单独使用,也支持两者同时使用。通常的情况下AOF保存的数据会比RDB完整,此外在两者同时使用的情况下会优先载入AOF来进行数据恢复。下面介绍下两者的优缺点及其实现。
Redis持久化之RDB:
RDB简介:
RDB(Redis DataBase)通过快照(SnapShotting)来实现在指定的时候内对数据进行保存。可以通过配置文件来修改RDB的触发,文件名等等。同也可以通过手动调用SAVE(阻塞)或者BGSAVE命令来触发RDB。
################################ SNAPSHOTTING ################################
#
# Save the DB on disk:
#
# save <seconds> <changes>
#
# 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 ""
# 配置RDB的触发条件使用save ""或者全部注释则关闭RDB
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.
# 如果bgsave发生错误,停止Redis数据写入,保证数据的一致性。
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