前言
透過 docker 建置 Redis,並測試 RDB、AOF 混合備份
環境
參考前一篇 w4560000 - Redis AOF 備份測試
RDB、 混合備份
aof-use-rdb-preamble yes/no
aof-use-rdb-preamble 開啟後,AOF rewrite 後的 aof 檔案,會以 RDB 二進制格式儲存資料,當後續有操作命令一樣會繼續回寫 AOF 檔
AOF 檔會變成前半部是 RDB 二進制格式,後半部則是正常可閱讀的操作命令
此方式同時保有 RDB 的優點 (二進制檔、復原快速)、AOF 的優點 (紀錄操作命令,減少異常狀況發生時資料損失)
但因此方式觸發時機是 AOF rewrite 時觸發,需注意 AOF rewrite 的時機點
頻繁備份會造成 Redis 服務負擔
調整 redis.conf
bind 127.0.0.1 172.18.0.2
protected-mode no
dir ./
# AOF
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
# AOF rewrite
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 0
auto-aof-rewrite-min-size 1mb
aof-load-truncated no
aof-use-rdb-preamble yes
# 建立 redis 容器
docker run --name redis1 \
--network=redis_network \
--ip 172.18.0.2 \
-p 6379:6379 \
-v /home/leozheng0411/redis:/usr/local/etc/redis \
-v /home/leozheng0411/redis1data:/data \
-d redis:6.0.0 \
redis-server /usr/local/etc/redis/redis.conf
# 更新測試資料
docker exec -it redis1 redis-cli SET Key1 1
# 查看 appendonly.aof 檔
cat appendonly.aof
# 輸出
*2
$6
SELECT
$1
0
*3
$3
SET
$4
Key1
$1
1
# 手動執行 AOF rewrite
docker exec -it redis1 redis-cli bgrewriteaof
# 再次查看 appendonly.aof 檔
cat -A appendonly.aof
# 輸出
REDIS0009M-z^Iredis-ver^E6.0.0M-z$
redis-bitsM-@@M-z^EctimeM-BM-.M-oM-^OdM-z^Hused-memM-BM-x2^M^@M-z^Laof-preambleM-@^AM-~^@M-{^A^@^@^DKey1M-@^AM-^?M-;M-4^XM-E^Vu^LG
# 再次更新測試資料
docker exec -it redis1 redis-cli SET Key2 2
# 再次查看 appendonly.aof 檔
REDIS0009M-z^Iredis-ver^E6.0.0M-z$
redis-bitsM-@@M-z^EctimeM-BM-.M-oM-^OdM-z^Hused-memM-BM-x2^M^@M-z^Laof-preambleM-@^AM-~^@M-{^A^@^@^DKey1M-@^AM-^?M-;M-4^XM-E^Vu^LG*2^M$
$6^M$
SELECT^M$
$1^M$
0^M$
*3^M$
$3^M$
SET^M$
$4^M$
Key2^M$
$1^M$
2^M$
可以發現目前 AOF 檔,AOF rewrite 後會以二進制儲存,而後續的操作命令一樣會回寫到 AOF 檔
災難異常復原模擬
目前資料
Redis Key | Value |
---|---|
Key1 | 1 |
Key2 | 2 |
# 強制關閉服務
docker rm -f redis1
# 重啟服務
docker run --name redis1 \
--network=redis_network \
--ip 172.18.0.2 \
-p 6379:6379 \
-v /home/leozheng0411/redis:/usr/local/etc/redis \
-v /home/leozheng0411/redis1data:/data \
-d redis:6.0.0 \
redis-server /usr/local/etc/redis/redis.conf
# 查看 Log 確認啟動時有載入 AOF 檔裡的 RDB preamble
docker logs redis1
# 輸出
1:C 19 Jun 2023 06:12:34.871 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 19 Jun 2023 06:12:34.871 # Redis version=6.0.0, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 19 Jun 2023 06:12:34.871 # Configuration loaded
1:M 19 Jun 2023 06:12:34.873 * Running mode=standalone, port=6379.
1:M 19 Jun 2023 06:12:34.873 # Server initialized
1:M 19 Jun 2023 06:12:34.874 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1:M 19 Jun 2023 06:12:34.874 * Reading RDB preamble from AOF file...
1:M 19 Jun 2023 06:12:34.874 * Loading RDB produced by version 6.0.0
1:M 19 Jun 2023 06:12:34.874 * RDB age 548 seconds
1:M 19 Jun 2023 06:12:34.874 * RDB memory usage when created 0.82 Mb
1:M 19 Jun 2023 06:12:34.874 * RDB has an AOF tail
1:M 19 Jun 2023 06:12:34.874 * Reading the remaining AOF tail...
1:M 19 Jun 2023 06:12:34.874 * DB loaded from append only file: 0.000 seconds
1:M 19 Jun 2023 06:12:34.874 * Ready to accept connections
# 確認資料正常復原
docker exec -it redis1 redis-cli GET Key1
# 輸出
"1"
docker exec -it redis1 redis-cli GET Key2
# 輸出
"2"
參考文件
稀土掘金 Jony_zhang - Redis 数据持久化(RDB/AOF/混合持久化)
知乎 程序员囧辉 - 面试必问的 Redis:RDB、AOF、混合持久化
騰訊雲開發者社區 似水流年o - Redis RDB+AOF混合持久化
轉載請註明來源,若有任何錯誤或表達不清楚的地方,歡迎在下方評論區留言,也可以來信至 leozheng0621@gmail.com
如果文章對您有幫助,歡迎斗內(donate),請我喝杯咖啡