Redis 持久化方式-RDB

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"針對Redis的話題估計有些讀者已經開始反感了,昨天還是有一些讀者困惑,這就具體講述下","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Redis持久化方式-RDB","attrs":{}},{"type":"text","text":"的實現方式~","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":" 觸發時機","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"手動觸發:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"save: 阻塞當前 Redis 服務器, 直到 RDB 過程完成爲止, 對於內存比較大的實例會造成長時間阻塞, 線上環境不建議使用","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"bgsave: Redis 進程執行 fork 操作創建子進程, RDB 持久化過程由子進程負責, 完成後自動結束。阻塞只發生在 fork 階段, 一般時間很短","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"自動觸發 RDB 的持久化機制, 例如以下場景:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用 save 相關配置, 如 “save m n”。表示 m 秒內數據集存在 n 次修改時, 自動觸發 bgsave。","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果從節點執行全量複製操作, 主節點自動執行 bgsave 生成RDB文件併發送給從節點, 更多細節見6.3節介紹的複製原理。","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"執行 debug reload 命令重新加載 Redis 時, 也會自動觸發 save 操作。(不清楚是 save 還是 bgsave)","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"默認情況下執行 shutdown 命令時, 如果沒有開啓 AOF 持久化功能則自動執行 bgsave。","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":" 流程說明","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/2e/2e2a7c30ce28b2c87f5d60e6f3cff945.webp","alt":"圖片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":" RDB的文件處理","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"保存","attrs":{}},{"type":"text","text":": RDB 文件保存在 dir 配置指定的目錄下, 文件名通過 dbfilename 配置指定。可以通過執行 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"config set dir {newDir}","attrs":{}}],"attrs":{}},{"type":"text","text":" 和 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"config set dbfilename {newFileName}","attrs":{}}],"attrs":{}},{"type":"text","text":" 運行期動態執行, 當下次運行時 RDB 文件會保存到新目錄。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"壓縮","attrs":{}},{"type":"text","text":": Redis 默認採用 LZF 算法對生成的 RDB 文件做壓縮處理, 壓縮後的文件遠遠小於內存大小, 默認開啓, 可以通過參數 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"config set rdbcompression {yes|no}","attrs":{}}],"attrs":{}},{"type":"text","text":" 動態修改。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"校驗","attrs":{}},{"type":"text","text":": 如果 Redis 加載損壞 的RDB 文件時拒絕啓動, 並打印如下日誌:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}],"attrs":{}}],"attrs":{}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"# Short read or OOM loading DB. Unrecoverable error, aborting now.","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這時可以使用 Redis 提供的 redis-check-dump 工具檢測 RDB 文件並獲取對應的錯誤報告。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":" RDB的優缺點","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"優點","attrs":{}},{"type":"text","text":":","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"RDB 是一個緊湊壓縮的二進制文件, 代表 Redis 在某個時間點上的數據快照。非常適用於備份, 全量複製等場景","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Redis 加載 RDB 恢復數據遠遠快於 AOF 的方式","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"缺點","attrs":{}},{"type":"text","text":":","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"RDB 方式數據沒辦法做到實時持久化/秒級持久化。因爲 bgsave 每次運行都要執行 fork 操作創建子進程, 屬於重量級操作, 頻繁執行成本過高","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"RDB 文件使用特定二進制格式保存, Redis 版本演進過程中有多個格式的 RDB 版本, 存在老版本 Redis 服務無法兼容新版 RDB 格式的問題","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Redis持久化推薦","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"http://mp.weixin.qq.com/s?__biz=MzU3OTc1MDM1Mg==&mid=2247493665&idx=1&sn=5ec9d3d4dbd568c57e4967ae6d66f569&chksm=fd63f89eca147188fa3bb62f661d2c118aebd21350a7d06a4b9bd4eaa6410ea9ec501c0c619c&scene=21#wechat_redirect","title":null},"content":[{"type":"text","text":"Redis 持久化方式-AOF","attrs":{}}]}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"http://mp.weixin.qq.com/s?__biz=MzU3OTc1MDM1Mg==&mid=2247493538&idx=1&sn=68ae9009394554294527af9ba0c7df11&chksm=fd63f71dca147e0b8256df08066d5ad977360e3ebe2d5409e40a15c66f18a398eb2a7eb740b4&scene=21#wechat_redirect","title":null},"content":[{"type":"text","text":"Redis 持久化方式-RDB","attrs":{}}]}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"http://mp.weixin.qq.com/s?__biz=MzU3OTc1MDM1Mg==&mid=2247494013&idx=1&sn=7b7feb4dc6eaafb239d0820d30f17414&chksm=fd63f9c2ca1470d44f1c8f745dff90ceeeafb55ed9a6133852c5b81ad8cedf30bb299757dc22&scene=21#wechat_redirect","title":null},"content":[{"type":"text","text":"Redis持久化問題定位與優化技巧","attrs":{}}]}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"http://mp.weixin.qq.com/s?__biz=MzU3OTc1MDM1Mg==&mid=2247492366&idx=3&sn=f8ef038f85a97d990e019ac03adb1250&chksm=fd63f3b1ca147aa76e4cf07c5a91b1daff9ec368530abf5ff6007b510aadc5c2b05f6911547e&scene=21#wechat_redirect","title":null},"content":[{"type":"text","text":"Redis三種持久化方式","attrs":{}}]}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章