數據持久化
by Aaron • 10/23/2022, 10:52:42 AM
Table of Content
Redis 將數據存在記憶體中,透過讀寫記憶體得到較高的併發性能。但若 Redis 意外關閉、重啟的話,數據將會丟失。 因此 Redis 提供兩種資料池久化的機制( RDB 和 AOF )
RDB (Redis DataBase File)
RDB 持久化是 Redis Default 的持久化方式,指在設置的間隔時間內將內存的 data snapshot 寫入硬碟中, 流程如下
- redis 根據 redis.conf 生成 .rdb 二進制文件
- redis.conf 裡設置的間隔時間到後 fork 一個子 process 出來
- 該子 process 將數據 dump 到暫時的 .rdb 文件中 (非一開始建立的)
- 完成 dump 後,將新的 .rdb 文件替換掉舊的 .rdb 文件
如何執行 RDB ?
- 立刻執行 doc
127.0.0.1:6379> BGSAVE
Background saving started
127.0.0.1:6379> LASTSAVE
(integer) 1714545738
127.0.0.1:6379> BGSAVE SCHEDULE
- 在 redis.conf 設置間隔
redis:7.2
redis.conf
################################ SNAPSHOTTING ################################
# Save the DB to disk.
#
# save <seconds> <changes> [<seconds> <changes> ...]
#
# Redis will save the DB if the given number of seconds elapsed and it
# surpassed the given number of write operations against the DB.
#
# Snapshotting can be completely disabled with a single empty string argument
# as in following example:
#
# save ""
#
# Unless specified otherwise, by default Redis will save the DB:
# * After 3600 seconds (an hour) if at least 1 change was performed
# * After 300 seconds (5 minutes) if at least 100 changes were performed
# * After 60 seconds if at least 10000 changes were performed
#
# You can set these explicitly by uncommenting the following line.
save 3600 1 300 100 60 10000 <------------- 設定執行 RDB 的時間間隔
...
# The filename where to dump the DB
dbfilename dump.rdb <------ 設定生成的 .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 ./
監控 RDB 執行狀況
root@de0af28bc900:/data# redis-cli info | grep rdb
rdb_changes_since_last_save:3 <- 上一次執行 RDB 後有多少 key 改變
rdb_bgsave_in_progress:0 <- 是否正在進行 RDB
rdb_last_save_time:1714544609 <- 上一次什麼時候進行 RDB
rdb_last_bgsave_status:ok <- 上一次 RDB 的狀況
rdb_last_bgsave_time_sec:-1 <- 上一次 RDB 花了多久
rdb_current_bgsave_time_sec:-1 <- 目前執行 RDB 已花費秒數
rdb_saves:0 <- 啟動後執行的RDB快照數量
rdb_last_cow_size:0 <- 上次 RDB 儲存作業期間寫入時複製記憶體的大小(以位元組為單位)
rdb_last_load_keys_expired:0 <- 上次 RDB 載入期間刪除的 expired 鍵的數量
rdb_last_load_keys_loaded:0 <- 上次 RDB 載入期間載入的鍵數
RDB 優點
- 適合大規模的數據恢復, 有大量資料的情況下,RDB比AOF有更短的啟動時間
RDB 缺點
- 資料的完整性不高, 在設定的期間內重啟 redis , 這期間到上一次備份的資料就會丟失
- 備份的時候佔用內存多, 備份時會複製一份資料在內存,此時內存內會有兩倍的資料量,需要考慮兩倍的內存量
AOF (Append Only File)
AOF 持久性記錄伺服器接收的每個寫入、刪除操作(讀取不會)。然後可以在伺服器啟動時再次重播這些操作,從而重建原始資料集。命令使用與 Redis 協定本身相同的格式進行記錄。
AOF 是通過保存被執行的寫入、刪除操作來紀錄資料庫狀態,所以 AOF 文件會隨著時間越來越大,進行復原的時間也會越來越多,甚至會對機器造成影響。為了解決這個狀況 Redis 提供 REWRITE 功能可以創建新的 AOF 文件取代舊的,新的 AOF 文件指存指令的最短操作來節省空間
redis.conf
...
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
設置AOF
############################## APPEND ONLY MODE ###############################
...
appendonly yes <----------- 啟用 AOF
# The base name of the append only file.
...
# - appendonly.aof.1.base.rdb as a base file.
# - appendonly.aof.1.incr.aof, appendonly.aof.2.incr.aof as incremental files.
# - appendonly.aof.manifest as a manifest file.
appendfilename "appendonly.aof" <-------- AOF 文件名稱
# For convenience, Redis stores all persistent append-only files in a dedicated
# directory. The name of the directory is determined by the appenddirname
# configuration parameter.
appenddirname "appendonlydir" <-------- AOF 放置的資料夾位置
...txt
# 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.
# appendfsync always
appendfsync everysec
# appendfsync no
監控 AOF 執行狀況
root@de0af28bc900:/data# redis-cli info | grep aof
mem_aof_buffer:8
aof_enabled:1
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_rewrites:0
aof_rewrites_consecutive_failures:0
aof_last_write_status:ok
aof_last_cow_size:0
aof_current_size:202
aof_base_size:88
aof_pending_rewrite:0
aof_buffer_length:0
aof_pending_bio_fsync:0
aof_delayed_fsync:0
AOF 優點
- 最嚴重只會丟失一秒鐘的數據
- AOF 文件過大時會啟動 REWRITE 來減少文件大小
- AOF 文件比較可讀
AOF 缺點
- 因為是記錄操作,導致比 RDB 文件大
- 效能比 RDB 差一點