RDB(Redis DataBase)
在指定的時間間隔內(nèi)將內(nèi)存中的數(shù)據(jù)集快照寫入磁盤,可以理解為Snapshot快照俭尖,它恢復(fù)時是將快照文件直接讀到內(nèi)存里哑了。
Redis會單獨創(chuàng)建(fork)一個子進程來進行持久化,會先將數(shù)據(jù)寫入到一個臨時文件中缰泡,待持久化過程都結(jié)束了定枷,再用這個臨時文件替換上次持久化好的文件。整個過程中簸州,主進程是不進行任何IO操作的拆火,這就確保了極高的性能。
如果需要進行大規(guī)模數(shù)據(jù)的恢復(fù)宜猜,且對于數(shù)據(jù)恢復(fù)的完整性不是非常敏感泼返,那RDB方式要比AOF方式更加的高效。RDB的缺點是最后一次持久化后的數(shù)據(jù)可能丟失姨拥。
默認配置
../redis.conf
#滿足以下三者之一就會觸發(fā)rdb備份
900s內(nèi)有1次操作
300s內(nèi)有10次操作
60s內(nèi)有10000次操作
#工作目錄
/var/lib/redis
#備份文件
dump.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 ""
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.
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.
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.
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 /var/lib/redis
關(guān)閉
1.修改配置文件绅喉,重新啟動redis
save ""
#save 900 1
#save 300 10
#save 60 10000
2.動態(tài)關(guān)閉
redis-cli config set save ""
相關(guān)命令
#修復(fù)損壞的rdb備份文件
redis-check-rdb --fix <rdb備份文件>
優(yōu)勢
1.適合大規(guī)模數(shù)據(jù)恢復(fù)
劣勢
1.最后一次時間間隔內(nèi)保存的數(shù)據(jù)可能丟失,數(shù)據(jù)完整性和一致性低
2.內(nèi)存中的數(shù)據(jù)被克隆一份叫乌,磁盤空間占用大
AOF
以日志的形式來記錄每個寫操作柴罐,將Redis執(zhí)行過的所有寫指令記錄下來(讀操作不記錄),只許追加文件但不可以改寫文件憨奸,redis啟動之初會讀取該文件重新構(gòu)建數(shù)據(jù)革屠,換言之,redis重啟的話就根據(jù)日志文件的內(nèi)容將寫指令從前到后執(zhí)行一次以完成數(shù)據(jù)的恢復(fù)工作。
默認配置
#默認不開啟aof備份
appendonly no
#aof備份文件名
appendfilename "appendonly.aof"
#同步策略:每秒同步(其它選項:每次同步似芝、用不同步)
# appendfsync always
appendfsync everysec
# appendfsync no
#aof文件為上次rewrite后文件大小的一倍那婉,并且大小超過64m時rewrite
#建議:生產(chǎn)中64mb太小,建議設(shè)為3G-5G
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
詳細配置
############################## 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.
#
# 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.
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 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".
# 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.
# Redis is able to automatically rewrite the log file implicitly calling
# BGREWRITEAOF when the AOF log size grows by the specified percentage.
#
# This is how it works: Redis remembers the size of the AOF file after the
# 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.
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
相關(guān)命令
#修復(fù)損壞的aof文件
redis-check-aof --fix <aof文件>
rewrite
AOF采用文件追加方式党瓮,文件會越來越大為避免出現(xiàn)此種情況详炬,新增了重寫機制,當(dāng)AOF文件的大小超過所設(shè)定的閾值時,Redis就會啟動AOF文件的內(nèi)容壓縮寞奸,只保留可以恢復(fù)數(shù)據(jù)的最小指令集.可以使用命令bgrewriteaof呛谜。
AOF文件持續(xù)增長而過大時,會fork出一條新進程來將文件重寫(也是先寫臨時文件最后再rename)蝇闭,遍歷新進程的內(nèi)存中數(shù)據(jù)呻率,每條記錄有一條的Set語句。重寫aof文件的操作呻引,并沒有讀取舊的aof文件礼仗,而是將整個內(nèi)存中的數(shù)據(jù)庫內(nèi)容用命令的方式重寫了一個新的aof文件,這點和快照有點類似逻悠。
Redis會記錄上次重寫時的AOF大小元践,默認配置是當(dāng)AOF文件大小是上次rewrite后大小的一倍且文件大于64M時觸發(fā)。
優(yōu)劣
1.appendfsync alway (同步持久化)
每次發(fā)生數(shù)據(jù)變更會被立即記錄到磁盤 性能較差但數(shù)據(jù)完整性比較好
2.appendfsync everysec (每秒同步)
異步操作童谒,每秒記錄 如果一秒內(nèi)宕機单旁,有數(shù)據(jù)丟失
3.appendfsync no (從不同步)
總結(jié)
- 相同數(shù)據(jù)集的數(shù)據(jù)而言aof文件要遠大于rdb文件,恢復(fù)速度慢于rdb
- aof運行效率要慢于rdb,每秒同步策略效率較好饥伊,不同步效率和rdb相同象浑。
- aof備份與rdb備份可以同時開啟,但redis優(yōu)先使用aof恢復(fù)琅豆。