edis支援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
從節點日志:
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
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配置參數總覽:
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是将資料集增量追加進日志檔案,其解決了資料持久化的實時性。