Redis的高效能是由於其將所有資料都儲存在了記憶體中,為了使Redis在重新啟動之後仍能保證資料不丟失,需要將資料從記憶體中同步到硬碟中,這一過程就是持久化. Redis支援兩種方式的持久化,一種是RDB(Redis Database)方式,一種是AOF(Append Only File)方式. 可以單獨使用其中一種或將二者結合使用
1. RDB持久化機制優點
相比於AOF,如果資料集很大,RDB的啟動效率會更高
2. RDB持久化機制缺點
3. RDB持久化機制設定
在redis.windows.conf組態檔中有如下設定:
################################ 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 at all commenting all the "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
其中,上面設定的是RDB方式資料持久化時機:
關鍵字 | 時間(Sec) | key修改量 | 解釋 |
---|---|---|---|
save | 900 | 1 | 每900秒(15分鐘)至少有1個key發生變化,則dump記憶體快照 |
save | 300 | 10 | 每300秒(5分鐘)至少有10個key發生變化,則dump記憶體快照 |
save | 60 | 10000 | 每60秒(1分鐘)至少有10000個key發生變化,則dump記憶體快照 |
1. AOF持久化機制優點
2. AOF持久化機制缺點
3. 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.
# #
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
將appendonly修改為yes,開啟aof持久化機制,預設會在目錄下產生一個appendonly.aof檔案
# appendfsync always
appendfsync everysec
# appendfsync no
上述設定為aof持久化的時機,解釋如下:
關鍵字 | 持久化時機 | 解釋 |
---|---|---|
appendfsync | always | 每執行一次更新命令,持久化一次 |
appendfsync | everysec | 每秒鐘持久化一次 |
appendfsync | no | 不會主動執行持久化,依賴於作業系統的空閒呼叫 |
命令 | RDB | AOF |
---|---|---|
啟動優先順序 | 低 | 高 |
體積 | 小 | 大 |
恢復速度 | 快 | 慢 |
資料安全性 | 丟資料 | 根據策略決定 |
輕重 | 重 | 輕 |