Redis 將數據存在記憶體中,透過讀寫記憶體得到較高的併發性能。但若 Redis 意外關閉、重啟的話,數據將會丟失。 因此 Redis 提供兩種資料池久化的機制( RDB 和 AOF )
RDB 持久化是 Redis Default 的持久化方式,指在設置的間隔時間內將內存的 data snapshot 寫入硬碟中, 流程如下
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: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 ./
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 載入期間載入的鍵數
AOF 持久性記錄伺服器接收的每個寫入、刪除操作(讀取不會)。然後可以在伺服器啟動時再次重播這些操作,從而重建原始資料集。命令使用與 Redis 協定本身相同的格式進行記錄。
AOF 是通過保存被執行的寫入、刪除操作來紀錄資料庫狀態,所以 AOF 文件會隨著時間越來越大,進行復原的時間也會越來越多,甚至會對機器造成影響。為了解決這個狀況 Redis 提供 REWRITE 功能可以創建新的 AOF 文件取代舊的,新的 AOF 文件指存指令的最短操作來節省空間
redis.conf ... auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb
############################## 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
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