Redis支持RDB和AOF兩種持久化方式,持久化功能有效避免了因進程退出造成數據丟失的問題。
RDB持久化是把當前進程數據生成快照保存到磁盤的過程,觸發RDB持久化的過程分爲手動和自動觸發。
手動觸發RDB持久化的操作
1. 手動執行SAVE命令
執行命令:
127.0.0.1:6379> SAVE
OK
日誌:
35650:M 18 Jul 18:10:42.795 * DB saved on disk
2. 手動執行BGSAVE命令
執行命令:
127.0.0.1:6379> BGSAVE
Background saving started
日誌:
35650:M 18 Jul 18:12:48.613 * Background saving started by pid 39301
39301:C 18 Jul 18:12:48.803 * DB saved on disk
39301:C 18 Jul 18:12:48.805 * RDB: 8 MB of memory used by copy-on-write
35650:M 18 Jul 18:12:48.881 * Background saving terminated with success
BGSAVE是針對SAVE阻塞做的優化,Redis涉及RDB持久化的操作優先使用BGSAVE。
自動觸發RDB持久化的機制
1. save相關配置,如save m<seconds> n<changes>,m秒內數據集有n次修改,觸發BGSAVE。
配置:
save 900 1
save 300 10
save 60 10000
日誌:
89428:M 18 Jul 18:46:49.809 * 10000 changes in 60 seconds. Saving...
89428:M 18 Jul 18:46:49.810 * Background saving started by pid 99472
99472:C 18 Jul 18:46:50.030 * DB saved on disk
99472:C 18 Jul 18:46:50.032 * RDB: 8 MB of memory used by copy-on-write
89428:M 18 Jul 18:46:50.113 * Background saving terminated with success
2. 新建主節點的一個從節點,主節點自動BGSAVE生成RDB文件,發送給從節點。
從節點執行命令:
127.0.0.1:6389> SLAVEOF 127.0.0.1 6379
OK
從節點日誌:
99498:S 18 Jul 18:56:27.721 * SLAVE OF 127.0.0.1:6379 enabled (user request from 'id=2 addr=127.0.0.1:55238 fd=6 name= age=210 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=slaveof')
99498:S 18 Jul 18:56:27.829 * Connecting to MASTER 127.0.0.1:6379
99498:S 18 Jul 18:56:27.829 * MASTER <-> SLAVE sync started
99498:S 18 Jul 18:56:27.830 * Non blocking connect for SYNC fired the event.
99498:S 18 Jul 18:56:27.830 * Master replied to PING, replication can continue...
99498:S 18 Jul 18:56:27.830 * Partial resynchronization not possible (no cached master)
99498:S 18 Jul 18:56:27.911 * Full resync from master: 2f949fed88eb2df127e45a4870dce9fe1b37f2b2:1
99498:S 18 Jul 18:56:28.184 * MASTER <-> SLAVE sync: receiving 5241839 bytes from master
99498:S 18 Jul 18:56:28.330 * MASTER <-> SLAVE sync: Flushing old data
99498:S 18 Jul 18:56:28.330 * MASTER <-> SLAVE sync: Loading DB in memory
99498:S 18 Jul 18:56:28.388 * MASTER <-> SLAVE sync: Finished with success
主節點日誌:
89428:M 18 Jul 18:56:27.831 * Slave 127.0.0.1:6389 asks for synchronization
89428:M 18 Jul 18:56:27.831 * Full resync requested by slave 127.0.0.1:6389
89428:M 18 Jul 18:56:27.831 * Starting BGSAVE for SYNC with target: disk
89428:M 18 Jul 18:56:27.907 * Background saving started by pid 99508
99508:C 18 Jul 18:56:28.105 * DB saved on disk
99508:C 18 Jul 18:56:28.106 * RDB: 8 MB of memory used by copy-on-write
89428:M 18 Jul 18:56:28.182 * Background saving terminated with success
89428:M 18 Jul 18:56:28.270 * Synchronization with slave 127.0.0.1:6389 succeeded
3. 執行DEBUG RELOAD重新加載Redis時,會觸發SAVE操作。
執行命令:
127.0.0.1:6379> DEBUG RELOAD
OK
日誌:
35650:M 18 Jul 18:40:22.379 * DB saved on disk
35650:M 18 Jul 18:40:22.433 # DB reloaded by DEBUG RELOAD
4. 執行SHUTDOWN時,會觸發SAVE操作。
執行命令:
127.0.0.1:6379> SHUTDOWN
日誌:
89428:M 18 Jul 19:08:17.003 # User requested shutdown...
89428:M 18 Jul 19:08:17.003 * Saving the final RDB snapshot before exiting.
89428:M 18 Jul 19:08:17.098 * DB saved on disk
89428:M 18 Jul 19:08:17.098 * Removing the pid file.
89428:M 18 Jul 19:08:17.098 * Removing the unix socket file.
89428:M 18 Jul 19:08:17.098 # Redis is now ready to exit, bye bye...
RDB配置參數總覽:
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
AOF持久化,以獨立日誌的方式記錄每次寫命令,解決了數據持久化的實時性問題。AOF的工作流程包括:append(命令寫入),sync(數據同步),rewrite(文件重寫)和load(重啓加載)。
AOF的rewrite過程可以手動觸發,和自動觸發。
1. 手動觸發
執行命令:
127.0.0.1:6379> BGREWRITEAOF
Background append only file rewriting started
日誌:
99615:M 18 Jul 19:44:28.610 * Background append only file rewriting started by pid 99639
99615:M 18 Jul 19:44:28.900 * AOF rewrite child asks to stop sending diffs.
99639:C 18 Jul 19:44:28.900 * Parent agreed to stop sending diffs. Finalizing AOF...
99639:C 18 Jul 19:44:28.900 * Concatenating 0.00 MB of AOF diff received from parent.
99639:C 18 Jul 19:44:28.902 * SYNC append only file rewrite performed
99639:C 18 Jul 19:44:28.903 * AOF rewrite: 4 MB of memory used by copy-on-write
99615:M 18 Jul 19:44:28.923 * Background AOF rewrite terminated with success
99615:M 18 Jul 19:44:28.923 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)
99615:M 18 Jul 19:44:28.923 * Background AOF rewrite finished successfully
2. 自動觸發
根據下面2個參數自動觸發。
auto-aof-rewrite-min-size 64mb,運行AOF重寫時,文件最小空間。
auto-aof-rewrite-percentage 100,當前AOF文件空間,和上次重寫後AOF文件空間的比值。
AOF配置參數總覽:
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
RDB和AOF兩種持久化方式對比:
1. RDB生成的是二進制文件,佔用空間小,恢復效率高。AOF生成的是遵守Redis序列化協議的文本文件,佔用空間大,恢復效率低。
~/keepmoving/6379/data $ file dump.rdb appendonly.aof
dump.rdb: data
appendonly.aof: ASCII text, with CRLF line terminators
2. RDB過程對操作系統造成的負載,一般比AOF要大,RDB是對全量數據集做快照,AOF是將數據集增量追加進日誌文件,其解決了數據持久化的實時性。
若感興趣可關注訂閱號”數據庫最佳實踐”(DBBestPractice).