一.為啥要持久化
大家都知道Redis是內(nèi)存數(shù)據(jù)庫,宕機(jī)后數(shù)據(jù)會(huì)消失。為了Redis重啟后快速恢復(fù)數(shù)據(jù),所以需要持久化機(jī)制。
總而言之,Redis持久化是為了快速的恢復(fù)數(shù)據(jù)而不是為了存儲(chǔ)數(shù)據(jù)。
二.持久化方式
大家都聽說過AOF和RDB,本文先從RDB入手,談?wù)剅edis的持久化。
2.1 RDB
RDB(Redis DataBase),是redis默認(rèn)的存儲(chǔ)方式。RDB通過快照的方式,記錄某一刻的數(shù)據(jù),不關(guān)注過程,因此會(huì)丟失最后一刻快照后更新的數(shù)據(jù)。
2.1.1 觸發(fā)快照的方式
-
配置參數(shù)定期執(zhí)行
在redis.conf默認(rèn)配置如下:
# 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
其中,格式為save 多少秒內(nèi) 數(shù)據(jù)變了多少
save "" # 不使用RDB存儲(chǔ) 不能主從
save 900 1 # 表示15分鐘(900秒鐘)內(nèi)至少1個(gè)鍵被更改則進(jìn)行快照。
save 300 10 # 表示5分鐘(300秒)內(nèi)至少10個(gè)鍵被更改則進(jìn)行快照。
save 60 10000 # 表示1分鐘內(nèi)至少10000個(gè)鍵被更改則進(jìn)行快照。
2.1.2 命令式顯式觸發(fā)
執(zhí)行save或者bgsave命令或者執(zhí)行flushall命令
127.0.0.1:6379> bgsave
Background saving started
- 執(zhí)行bgsave發(fā)生了啥
1.父進(jìn)程執(zhí)行fork操作創(chuàng)建子進(jìn)程,這個(gè)復(fù)制過程中父進(jìn)程是阻塞的,
Redis不能執(zhí)行來自客戶端的任何命令。- 子進(jìn)程創(chuàng)建RDB文件,根據(jù)父進(jìn)程內(nèi)存快照生成臨時(shí)快照文件,完成后對原有文件進(jìn)行原子替換。(RDB始終完整)
- 父進(jìn)程fork后,bgsave命令返回”Background saving started”信息并不再阻塞父進(jìn)程,并可以響應(yīng)其他命令。
2.1.3 RDB的優(yōu)缺點(diǎn)
- 優(yōu)點(diǎn)
RDB是二進(jìn)制壓縮文件,占用空間小,便于傳輸(傳給slaver)
主進(jìn)程fork子進(jìn)程,可以最大化Redis性能,主進(jìn)程不能太大,Redis的數(shù)據(jù)量不能太大,復(fù)制過程中主進(jìn)程阻塞。 - 缺點(diǎn)
不保證數(shù)據(jù)完整性,會(huì)丟失最后一次快照以后更改的所有數(shù)據(jù)。
2.2 AOF
AOF(append only file)默認(rèn)不開啟。開啟AOF持久化后Redis 將所有對數(shù)據(jù)庫進(jìn)行過寫入的命令(及其參數(shù))(RESP)記錄到 AOF 文件, 以此達(dá)到記錄數(shù)據(jù)庫狀態(tài)的目的,這樣當(dāng)Redis重啟后只要按順序回放這些命令就會(huì)恢復(fù)到原始狀態(tài)了。
-
2.2.1 配置開啟AOF
1.在redis.conf里,配置如下:
# 可以通過修改redis.conf配置文件中的appendonly參數(shù)開啟
appendonly yes
# AOF文件的保存位置和RDB文件的位置相同,都是通過dir參數(shù)設(shè)置的。
dir ./
# 默認(rèn)的文件名是appendonly.aof,可以通過appendfilename參數(shù)修改
appendfilename 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.
appendonly no
# The name of the append only file (default: "appendonly.aof")
appendfilename "appendonly.aof"
-
2.2.2 AOF文件里面是什么
RDB文件是一個(gè)二進(jìn)制壓縮文件,主要內(nèi)容之一就是數(shù)據(jù)鍵值對。那AOF文件里面又是什么呢?
AOF文件出的是redis的寫入命令及其參數(shù)。 -
2.2.3 AOF文件的保存模式
-
AOF_FSYNC_NO
不保存。但save操作會(huì)在以下情況之一被執(zhí)行:
(1)Redis 被關(guān)閉
(1)AOF 功能被關(guān)閉
(3)系統(tǒng)的寫緩存被刷新(可能是緩存已經(jīng)被寫滿,或者定期保存操作被執(zhí)行)
同時(shí)也會(huì)引起主進(jìn)程的阻塞。 -
AOF_FSYNC_EVERYSEC
默認(rèn)每一秒保存一次。在這種模式中, SAVE 原則上每隔一秒鐘就會(huì)執(zhí)行一次, 因?yàn)?SAVE 操作是由后臺子線程(fork)調(diào)用的, 所以它不會(huì)引起服務(wù)器主進(jìn)程阻塞。 -
AOF_FSYNC_ALWAYS
每執(zhí)行一個(gè)命令保存一次。在這種模式下,每次執(zhí)行完一個(gè)命令之后, WRITE 和 SAVE 都會(huì)被執(zhí)行。另外,這種模式下 SAVE 是由 Redis 主進(jìn)程執(zhí)行的,所以在 SAVE 執(zhí)行期間,主進(jìn)程會(huì)被阻塞,不能接受命令
請求。因此性能大受影響。
-
AOF_FSYNC_NO
2.3 RDB與AOF對比
- RDB存某個(gè)時(shí)刻的數(shù)據(jù)快照,采用二進(jìn)制壓縮存儲(chǔ),AOF存操作命令,采用文本存儲(chǔ)(混合)
- RDB性能高、AOF性能較低
- RDB在配置觸發(fā)狀態(tài)會(huì)丟失最后一次快照以后更改的所有數(shù)據(jù),AOF設(shè)置為每秒保存一次,則最多丟2秒的數(shù)據(jù)
- Redis以主服務(wù)器模式運(yùn)行,RDB不會(huì)保存過期鍵值對數(shù)據(jù),Redis以從服務(wù)器模式運(yùn)行,RDB會(huì)保存過期鍵值對,當(dāng)主服務(wù)器向從服務(wù)器同步時(shí),再清空過期鍵值對。
AOF寫入文件時(shí),對過期的key會(huì)追加一條del命令,當(dāng)執(zhí)行AOF重寫時(shí),會(huì)忽略過期key和del命令