在使用 Redis 的过程中,你是否遇到过下面这些问题:
开启 RDB 落盘,业务频繁出现请求超时。
除了 save 和 bgsave 命令,还有哪些操作会触发 RDB 落盘?
执行了 flushall,发现 flushall 之前写的数据又冒出来了。
重启 Redis 实例之后,发现数据有丢失的情况。
set aaa 111","marks":[]}]}],"state":{}},{"type":"block","id":"Gezy-1655890668775","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"EeVs-1655890668774","leaves":[{"text":"OK","marks":[]}]}],"state":{}},{"type":"block","id":"6iRe-1655890668777","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"NVdY-1655890668776","leaves":[{"text":"127.0.0.1:6301> set bbb 111","marks":[]}]}],"state":{}},{"type":"block","id":"FSUU-1655890668779","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"Qi4K-1655890668778","leaves":[{"text":"OK","marks":[]}]}],"state":{}},{"type":"block","id":"KhYQ-1655890668781","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"1XPp-1655890668780","leaves":[{"text":"127.0.0.1:6301> bgsave","marks":[]}]}],"state":{}},{"type":"block","id":"urWQ-1655890668783","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"gg9c-1655890668782","leaves":[{"text":"Background saving started","marks":[]}]}],"state":{}},{"type":"block","id":"0M35-1655893053940","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"BoeQ-1655893053938","leaves":[{"text":"","marks":[]}]}],"state":{}},{"type":"block","id":"yIxd-1655890893749","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"Wjqt-1655890893747","leaves":[{"text":"127.0.0.1:6301> flushall","marks":[]}]}],"state":{}},{"type":"block","id":"AgMP-1655890902634","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"jxFh-1655890902633","leaves":[{"text":"OK","marks":[]}]}],"state":{}},{"type":"block","id":"o4KA-1655890668785","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"KiRd-1655890668784","leaves":[{"text":"127.0.0.1:6301> config get save","marks":[]}]}],"state":{}},{"type":"block","id":"kRtQ-1655890668787","name":"list-item","data":{"version":1,"listId":"P71n-1655890668755","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"EghP-1655890668786","leaves":[{"text":"\"save\"","marks":[]}]}],"state":{"index":1}},{"type":"block","id":"FAV8-1655890668789","name":"list-item","data":{"version":1,"listId":"P71n-1655890668755","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"c6DR-1655890668788","leaves":[{"text":"\"\"","marks":[]}]}],"state":{"index":2}},{"type":"block","id":"C3NC-1655890668791","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"IW47-1655890668790","leaves":[{"text":"127.0.0.1:6301> config get appendonly","marks":[]}]}],"state":{}},{"type":"block","id":"7mfN-1655890668793","name":"list-item","data":{"version":1,"listId":"aqXG-1655890668757","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"Vtlq-1655890668792","leaves":[{"text":"\"appendonly\"","marks":[]}]}],"state":{"index":1}},{"type":"block","id":"UHvl-1655890668795","name":"list-item","data":{"version":1,"listId":"aqXG-1655890668757","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"iTi1-1655890668794","leaves":[{"text":"\"no\"","marks":[]}]}],"state":{"index":2}},{"type":"block","id":"ezIn-1655890670875","name":"paragraph","data":{},"nodes":[{"type":"text","id":"oy5i-1655890670873","leaves":[{"text":"127.0.0.1:6301> shutdown","marks":[]}]}],"state":{}},{"type":"block","id":"vkDu-1655890680217","name":"paragraph","data":{},"nodes":[{"type":"text","id":"7K8F-1655890680216","leaves":[{"text":"","marks":[]}]}],"state":{}},{"type":"block","id":"fIYv-1655890681078","name":"paragraph","data":{},"nodes":[{"type":"text","id":"3AAT-1655890681076","leaves":[{"text":"再启动 Redis","marks":[]}]}],"state":{}},{"type":"block","id":"m6WJ-1655890686803","name":"paragraph","data":{},"nodes":[{"type":"text","id":"IxZo-1655890686802","leaves":[{"text":"127.0.0.1:6301> keys *","marks":[]}]}],"state":{}},{"type":"block","id":"9bKf-1655890695851","name":"list-item","data":{"version":1,"listId":"x4xr-1655890695842","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"Gc1y-1655890695850","leaves":[{"text":"\"aaa\"","marks":[]}]}],"state":{"index":1}},{"type":"block","id":"qZbx-1655890695853","name":"list-item","data":{"version":1,"listId":"x4xr-1655890695842","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"9jEI-1655890695852","leaves":[{"text":"\"bbb\"","marks":[]}]}],"state":{"index":2}},{"type":"block","id":"SCdk-1655890702890","name":"paragraph","data":{},"nodes":[{"type":"text","id":"y5lN-1655890702888","leaves":[{"text":"会看到我们清空Redis 之前写入的数据。显然是不符合逻辑的。","marks":[]}]}],"state":{}},{"type":"block","id":"FSnC-1655893635985","name":"paragraph","data":{},"nodes":[{"type":"text","id":"t3AM-1655893635983","leaves":[{"text":"这是因为在启动 Redis 时,会加载数据目录下的 RDB 文件,而这个 RDB 文件是 flushall 之前执行 bgsave 生成的,也就是会看到清空 Redis 之前写入的数据。这里也是“","marks":[]},{"text":"执行了 flushall,发现 flushall 之前写的数据又冒出来了。","marks":[{"type":"bold"}]},{"text":"”的原因。","marks":[]}]}],"state":{}},{"type":"block","id":"pahc-1655893636770","name":"paragraph","data":{},"nodes":[{"type":"text","id":"j3ul-1655893636769","leaves":[{"text":"所以在实例未开启 RDB 和 AOF 的情况下,如果执行 了 flushall 命令,建议再执行一次 bgsave,让 RDB 文件也清空。","marks":[]}]}],"state":{}},{"type":"block","id":"WxpZ-1655891215757","name":"paragraph","data":{},"nodes":[{"type":"text","id":"Ddwk-1655891215756","leaves":[{"text":"","marks":[]}]}],"state":{}},{"type":"block","id":"YOVw-1655891216553","name":"paragraph","data":{},"nodes":[{"type":"text","id":"SvtV-1655891216551","leaves":[{"text":"另外还测试了开启 AOF,关闭 RDB 的情况:","marks":[]}]}],"state":{}},{"type":"block","id":"04b2-1655891250870","name":"paragraph","data":{},"nodes":[{"type":"text","id":"iD7W-1655891250869","leaves":[{"text":"127.0.0.1:6301> set aaa 111","marks":[]}]}],"state":{}},{"type":"block","id":"jTpH-1655891264727","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"TiML-1655891264726","leaves":[{"text":"OK","marks":[]}]}],"state":{}},{"type":"block","id":"TJdM-1655891264729","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"ZR7d-1655891264728","leaves":[{"text":"127.0.0.1:6301> set bbb 111","marks":[]}]}],"state":{}},{"type":"block","id":"gHWu-1655891264731","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"D8FU-1655891264730","leaves":[{"text":"OK","marks":[]}]}],"state":{}},{"type":"block","id":"9yXg-1655891264733","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"AFmV-1655891264732","leaves":[{"text":"127.0.0.1:6301> bgsave","marks":[]}]}],"state":{}},{"type":"block","id":"OQRp-1655891264735","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"NQs2-1655891264734","leaves":[{"text":"Background saving started","marks":[]}]}],"state":{}},{"type":"block","id":"d3hT-1655891264737","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"DA2i-1655891264736","leaves":[{"text":"127.0.0.1:6301> flushall","marks":[]}]}],"state":{}},{"type":"block","id":"AfPR-1655891264740","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"Ca1g-1655891264739","leaves":[{"text":"OK","marks":[]}]}],"state":{}},{"type":"block","id":"iywM-1655891264742","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"FkTW-1655891264741","leaves":[{"text":"127.0.0.1:6301> config get save","marks":[]}]}],"state":{}},{"type":"block","id":"SO9H-1655891264744","name":"list-item","data":{"version":1,"listId":"MTFC-1655891264705","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"qIk9-1655891264743","leaves":[{"text":"\"save\"","marks":[]}]}],"state":{"index":1}},{"type":"block","id":"c3FN-1655891264746","name":"list-item","data":{"version":1,"listId":"MTFC-1655891264705","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"jzH2-1655891264745","leaves":[{"text":"\"\"","marks":[]}]}],"state":{"index":2}},{"type":"block","id":"LcFn-1655891264748","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"NeMi-1655891264747","leaves":[{"text":"127.0.0.1:6301> config get appendonly","marks":[]}]}],"state":{}},{"type":"block","id":"pLjp-1655891264750","name":"list-item","data":{"version":1,"listId":"7du8-1655891264707","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"aH9q-1655891264749","leaves":[{"text":"\"appendonly\"","marks":[]}]}],"state":{"index":1}},{"type":"block","id":"FU6c-1655891264752","name":"list-item","data":{"version":1,"listId":"7du8-1655891264707","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"3bKn-1655891264751","leaves":[{"text":"\"yes\"","marks":[]}]}],"state":{"index":2}},{"type":"block","id":"C6za-1655891264754","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"sUDJ-1655891264753","leaves":[{"text":"127.0.0.1:6301> shutdown","marks":[]}]}],"state":{}},{"type":"block","id":"qcQx-1655891267833","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"hOjw-1655891267832","leaves":[{"text":"","marks":[]}]}],"state":{}},{"type":"block","id":"iPXf-1655891267972","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"mcCF-1655891267970","leaves":[{"text":"启动 Redis","marks":[]}]}],"state":{}},{"type":"block","id":"blTl-1655891274733","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"9qrW-1655891274732","leaves":[{"text":"redis-cli -p 6301","marks":[]}]}],"state":{}},{"type":"block","id":"sFe2-1655891281854","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"VmIa-1655891281853","leaves":[{"text":"127.0.0.1:6301> keys *","marks":[]}]}],"state":{}},{"type":"block","id":"MHOj-1655891288833","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"BsGT-1655891288832","leaves":[{"text":"(empty list or set)","marks":[]}]}],"state":{}},{"type":"block","id":"dH9r-1655891292029","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"ZvCN-1655891292027","leaves":[{"text":"","marks":[]}]}],"state":{}},{"type":"block","id":"kKsI-1655891292163","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"ndjF-1655891292162","leaves":[{"text":"重启之后不会再看到 flushall 之前写入的数据,因为 Redis 在 启动时加载 rdb 文件后,也会加载在执行 RDB 之后 AOF 里新增的操作。而 flushall 操作就记录在 AOF 文件中。","marks":[]}]}],"state":{}},{"type":"block","id":"bxuj-1655792323906","name":"paragraph","data":{},"nodes":[{"type":"text","id":"vRWK-1655792323904","leaves":[{"text":"","marks":[]}]}],"state":{}},{"type":"block","id":"6m5v-1655792330716","name":"paragraph","data":{},"nodes":[{"type":"text","id":"9BQA-1655792330714","leaves":[{"text":"","marks":[]}]}],"state":{}},{"type":"block","id":"XUnD-1655792368251","name":"paragraph","data":{},"nodes":[{"type":"text","id":"6XtY-1655792368250","leaves":[{"text":"4 正常关闭时","marks":[{"type":"bold"}]}]}],"state":{}},{"type":"block","id":"Livf-1655893437262","name":"paragraph","data":{},"nodes":[{"type":"text","id":"wvqI-1655893437260","leaves":[{"text":"我们通过在命令行执行 shutdown 正常关闭 Redis 时,并不是所有情况都会执行一次 RDB 落盘的,这里就来分析一下不同配置,重启后的情况。","marks":[]}]}],"state":{}},{"type":"block","id":"YRAg-1655792330869","name":"paragraph","data":{},"nodes":[{"type":"text","id":"Pvv3-1655792330868","leaves":[{"text":"4/1 AOF 和 RDB 都开启的情况","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontSize","value":16},{"type":"fontFamily","value":"Arial"},{"type":"bold"}]}]}],"state":{}},{"type":"block","id":"ZH44-1655824761076","name":"paragraph","data":{},"nodes":[{"type":"text","id":"wKGS-1655824761074","leaves":[{"text":"127.0.0.1:6301> set bbb 111","marks":[]}]}],"state":{}},{"type":"block","id":"XOua-1655887765104","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"vcg7-1655887765103","leaves":[{"text":"OK","marks":[]}]}],"state":{}},{"type":"block","id":"3YI1-1655887765106","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"mEa0-1655887765105","leaves":[{"text":"127.0.0.1:6301> config get save","marks":[]}]}],"state":{}},{"type":"block","id":"iY18-1655887765108","name":"list-item","data":{"version":1,"listId":"Xb0Y-1655887765070","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"vjre-1655887765107","leaves":[{"text":"\"save\"","marks":[]}]}],"state":{"index":1}},{"type":"block","id":"r1Qt-1655887765110","name":"list-item","data":{"version":1,"listId":"Xb0Y-1655887765070","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"Baen-1655887765109","leaves":[{"text":"\"1800 1\"","marks":[]}]}],"state":{"index":2}},{"type":"block","id":"cNnj-1655887765112","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"MNSd-1655887765111","leaves":[{"text":"127.0.0.1:6301> config get appendonly","marks":[]}]}],"state":{}},{"type":"block","id":"28pw-1655887765115","name":"list-item","data":{"version":1,"listId":"eEMJ-1655887765090","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"oiHo-1655887765113","leaves":[{"text":"\"appendonly\"","marks":[]}]}],"state":{"index":1}},{"type":"block","id":"SxzK-1655887765117","name":"list-item","data":{"version":1,"listId":"eEMJ-1655887765090","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"zZWg-1655887765116","leaves":[{"text":"\"yes\"","marks":[]}]}],"state":{"index":2}},{"type":"block","id":"0YYL-1655887765119","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"mh5z-1655887765118","leaves":[{"text":"127.0.0.1:6301> shutdown","marks":[]}]}],"state":{}},{"type":"block","id":"Hv7E-1655887776706","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"p1pF-1655887776705","leaves":[{"text":"","marks":[]}]}],"state":{}},{"type":"block","id":"YEIc-1655887777012","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"YOgq-1655887777010","leaves":[{"text":"启动 Redis","marks":[]}]}],"state":{}},{"type":"block","id":"Wwkc-1655887782423","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"EaBP-1655887782421","leaves":[{"text":"127.0.0.1:6301> get bbb","marks":[]}]}],"state":{}},{"type":"block","id":"ORnG-1655887800384","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"pNvL-1655887800383","leaves":[{"text":"\"111\"","marks":[]}]}],"state":{}},{"type":"block","id":"NbAe-1655887741466","name":"paragraph","data":{},"nodes":[{"type":"text","id":"u6Pv-1655887741464","leaves":[{"text":"这种情况会在关闭时执行一次 RDB 落盘,启动加载 RDB 文件,保证重启前后数据一致。","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontSize","value":16},{"type":"fontFamily","value":"Arial"}]}]}],"state":{}},{"type":"block","id":"ONOZ-1655893911457","name":"paragraph","data":{},"nodes":[{"type":"text","id":"Tie8-1655893911456","leaves":[{"text":"","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontSize","value":16},{"type":"fontFamily","value":"Arial"}]}]}],"state":{}},{"type":"block","id":"b33Y-1655893899084","name":"paragraph","data":{},"nodes":[{"type":"text","id":"Pq1p-1655893899082","leaves":[{"text":"","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontSize","value":16},{"type":"fontFamily","value":"Arial"}]}]}],"state":{}},{"type":"block","id":"8U6t-1655824761517","name":"paragraph","data":{},"nodes":[{"type":"text","id":"ezhz-1655824761515","leaves":[{"text":"4.2 AOF 和 RDB 都未开的情况","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontSize","value":16},{"type":"fontFamily","value":"Arial"},{"type":"bold"}]}]}],"state":{}},{"type":"block","id":"gXgb-1655824772108","name":"paragraph","data":{},"nodes":[{"type":"text","id":"a0oG-1655824772107","leaves":[{"text":"127.0.0.1:6301> set ccc 111","marks":[]}]}],"state":{}},{"type":"block","id":"b0Si-1655824989380","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"u3FB-1655824989379","leaves":[{"text":"OK","marks":[]}]}],"state":{}},{"type":"block","id":"bpoJ-1655824989382","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"tmtQ-1655824989381","leaves":[{"text":"127.0.0.1:6301> config get save","marks":[]}]}],"state":{}},{"type":"block","id":"qEI3-1655824989384","name":"list-item","data":{"version":1,"listId":"99UD-1655824989362","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"bbGP-1655824989383","leaves":[{"text":"\"save\"","marks":[]}]}],"state":{"index":1}},{"type":"block","id":"O5ib-1655824989386","name":"list-item","data":{"version":1,"listId":"99UD-1655824989362","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"umOg-1655824989385","leaves":[{"text":"\"\"","marks":[]}]}],"state":{"index":2}},{"type":"block","id":"ncdx-1655824989388","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"nyl7-1655824989387","leaves":[{"text":"127.0.0.1:6301> config get appendonly","marks":[]}]}],"state":{}},{"type":"block","id":"WBnt-1655824989390","name":"list-item","data":{"version":1,"listId":"XYMJ-1655824989364","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"q2dv-1655824989389","leaves":[{"text":"\"appendonly\"","marks":[]}]}],"state":{"index":1}},{"type":"block","id":"9AGk-1655824989392","name":"list-item","data":{"version":1,"listId":"XYMJ-1655824989364","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"F6aQ-1655824989391","leaves":[{"text":"\"no\"","marks":[]}]}],"state":{"index":2}},{"type":"block","id":"cRju-1655824989394","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"p35E-1655824989393","leaves":[{"text":"127.0.0.1:6301> shutdown","marks":[]}]}],"state":{}},{"type":"block","id":"eXyh-1655824992631","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"IaaZ-1655824992630","leaves":[{"text":"","marks":[]}]}],"state":{}},{"type":"block","id":"8R9L-1655824992786","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"p7y8-1655824992785","leaves":[{"text":"然后启动 Redis","marks":[]}]}],"state":{}},{"type":"block","id":"zxMD-1655825006642","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"W1Wy-1655825006641","leaves":[{"text":"127.0.0.1:6301> get ccc","marks":[]}]}],"state":{}},{"type":"block","id":"AKWV-1655825017202","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"27eA-1655825017201","leaves":[{"text":"(nil)","marks":[]}]}],"state":{}},{"type":"block","id":"1Acc-1655825020277","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"p8dl-1655825020275","leaves":[{"text":"发现重启之前 ccc 的 key 已经丢失,","marks":[]},{"text":"因此在 master 未开启 RDB 的情况,关闭之前需要主动执行 bgsave,否则会导致数据丢失。这也是“","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontSize","value":16},{"type":"fontFamily","value":"Arial"}]},{"text":"重启 Redis 实例之后,发现数据有丢失的情况","marks":[{"type":"fontSize","value":16},{"type":"bold"}]},{"text":"”的原因。","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontSize","value":16},{"type":"fontFamily","value":"Arial"}]}]}],"state":{}},{"type":"block","id":"zs3X-1655866267151","name":"paragraph","data":{},"nodes":[{"type":"text","id":"xicm-1655866267150","leaves":[{"text":"","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontSize","value":16},{"type":"fontFamily","value":"Arial"}]}]}],"state":{}},{"type":"block","id":"nazm-1655866218485","name":"paragraph","data":{},"nodes":[{"type":"text","id":"YHzX-1655866218484","leaves":[{"text":"","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontSize","value":16},{"type":"fontFamily","value":"Arial"}]}]}],"state":{}},{"type":"block","id":"ZSr7-1655824812385","name":"paragraph","data":{},"nodes":[{"type":"text","id":"fmDH-1655824812383","leaves":[{"text":"4.3 AOF 关闭,RDB 开启的情况","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontSize","value":16},{"type":"fontFamily","value":"Arial"},{"type":"bold"}]}]}],"state":{}},{"type":"block","id":"vEt1-1655824823939","name":"paragraph","data":{},"nodes":[{"type":"text","id":"yfmX-1655824823938","leaves":[{"text":"127.0.0.1:6301> set ddd 111","marks":[]}]}],"state":{}},{"type":"block","id":"Kgtx-1655888558340","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"rnAU-1655888558339","leaves":[{"text":"OK","marks":[]}]}],"state":{}},{"type":"block","id":"F8S9-1655888558343","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"XPLp-1655888558342","leaves":[{"text":"127.0.0.1:6301> config get save","marks":[]}]}],"state":{}},{"type":"block","id":"4nkT-1655888558345","name":"list-item","data":{"version":1,"listId":"9Mr3-1655888558326","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"SwYc-1655888558344","leaves":[{"text":"\"save\"","marks":[]}]}],"state":{"index":1}},{"type":"block","id":"Q5Jr-1655888558347","name":"list-item","data":{"version":1,"listId":"9Mr3-1655888558326","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"MLRb-1655888558346","leaves":[{"text":"\"1800 1\"","marks":[]}]}],"state":{"index":2}},{"type":"block","id":"JTCf-1655888558349","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"pUfg-1655888558348","leaves":[{"text":"127.0.0.1:6301> config get appendonly","marks":[]}]}],"state":{}},{"type":"block","id":"L445-1655888558351","name":"list-item","data":{"version":1,"listId":"bbcW-1655888558328","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"fwt3-1655888558350","leaves":[{"text":"\"appendonly\"","marks":[]}]}],"state":{"index":1}},{"type":"block","id":"EX80-1655888558353","name":"list-item","data":{"version":1,"listId":"bbcW-1655888558328","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"ty84-1655888558352","leaves":[{"text":"\"no\"","marks":[]}]}],"state":{"index":2}},{"type":"block","id":"ZXq5-1655888557385","name":"paragraph","data":{},"nodes":[{"type":"text","id":"0noT-1655888557383","leaves":[{"text":"127.0.0.1:6301> shutdown","marks":[]}]}],"state":{}},{"type":"block","id":"fAej-1655888568959","name":"paragraph","data":{},"nodes":[{"type":"text","id":"Hzmi-1655888568957","leaves":[{"text":"启动 Redis","marks":[]}]}],"state":{}},{"type":"block","id":"8esc-1655888579617","name":"paragraph","data":{},"nodes":[{"type":"text","id":"qvf3-1655888579616","leaves":[{"text":"127.0.0.1:6301> get ddd","marks":[]}]}],"state":{}},{"type":"block","id":"ak95-1655888636161","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"C3NS-1655888636160","leaves":[{"text":"\"111\"","marks":[]}]}],"state":{}},{"type":"block","id":"KvFR-1655888569423","name":"paragraph","data":{},"nodes":[{"type":"text","id":"b9uE-1655888569422","leaves":[{"text":"","marks":[]}]}],"state":{}},{"type":"block","id":"LpVB-1655888557524","name":"paragraph","data":{},"nodes":[{"type":"text","id":"Cdb3-1655888557522","leaves":[{"text":"这种情况会写 RDB,重启后数据未丢失。","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontSize","value":16},{"type":"fontFamily","value":"Arial"}]}]}],"state":{}},{"type":"block","id":"O39z-1655808429874","name":"paragraph","data":{},"nodes":[{"type":"text","id":"FuU6-1655808429873","leaves":[{"text":"","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontSize","value":16},{"type":"fontFamily","value":"Arial"}]}]}],"state":{}},{"type":"block","id":"9dLG-1655866291685","name":"paragraph","data":{},"nodes":[{"type":"text","id":"mFAL-1655866291683","leaves":[{"text":"","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontSize","value":16},{"type":"fontFamily","value":"Arial"}]}]}],"state":{}},{"type":"block","id":"2f56-1655866291839","name":"paragraph","data":{},"nodes":[{"type":"text","id":"a8do-1655866291837","leaves":[{"text":"4.4 AOF 开启,RDB 关闭的情况","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontSize","value":16},{"type":"fontFamily","value":"Arial"},{"type":"bold"}]}]}],"state":{}},{"type":"block","id":"rufi-1655866349417","name":"paragraph","data":{},"nodes":[{"type":"text","id":"FhXv-1655866349416","leaves":[{"text":"127.0.0.1:6301> set eee 111","marks":[]}]}],"state":{}},{"type":"block","id":"jRgB-1655889003755","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"jPpv-1655889003754","leaves":[{"text":"OK","marks":[]}]}],"state":{}},{"type":"block","id":"fMKm-1655889003757","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"SrtN-1655889003756","leaves":[{"text":"127.0.0.1:6301> config get save","marks":[]}]}],"state":{}},{"type":"block","id":"RxG8-1655889003759","name":"list-item","data":{"version":1,"listId":"eSo1-1655889003739","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"M1yY-1655889003758","leaves":[{"text":"\"save\"","marks":[]}]}],"state":{"index":1}},{"type":"block","id":"QyCu-1655889003761","name":"list-item","data":{"version":1,"listId":"eSo1-1655889003739","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"5soX-1655889003760","leaves":[{"text":"\"\"","marks":[]}]}],"state":{"index":2}},{"type":"block","id":"Y5nl-1655889003763","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"a72b-1655889003762","leaves":[{"text":"127.0.0.1:6301> config get appendonly","marks":[]}]}],"state":{}},{"type":"block","id":"nOew-1655889003766","name":"list-item","data":{"version":1,"listId":"sETu-1655889003741","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"hCOU-1655889003765","leaves":[{"text":"\"appendonly\"","marks":[]}]}],"state":{"index":1}},{"type":"block","id":"1jb5-1655889003768","name":"list-item","data":{"version":1,"listId":"sETu-1655889003741","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"vv9B-1655889003767","leaves":[{"text":"\"yes\"","marks":[]}]}],"state":{"index":2}},{"type":"block","id":"bD3W-1655889003770","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"y0mC-1655889003769","leaves":[{"text":"127.0.0.1:6301> shutdown","marks":[]}]}],"state":{}},{"type":"block","id":"3jde-1655889006481","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"h368-1655889006480","leaves":[{"text":"启动 Redis","marks":[]}]}],"state":{}},{"type":"block","id":"tCQC-1655889021894","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"PUXR-1655889021892","leaves":[{"text":"127.0.0.1:6301> get eee","marks":[]}]}],"state":{}},{"type":"block","id":"xoQU-1655889026836","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"cXWE-1655889026835","leaves":[{"text":"\"111\"","marks":[]}]}],"state":{}},{"type":"block","id":"HEeM-1655894063820","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"ffH5-1655894063818","leaves":[{"text":"这种情况尽管不会进行 RDB 落盘,但是因为之前的操作都写入了 AOF,在 Redis 启动时,会加载 AOF 里的数据,因此也会跟关闭之前的数据保持一致。","marks":[]}]}],"state":{}},{"type":"block","id":"7vzB-1655894152756","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"mwZb-1655894152755","leaves":[{"text":"","marks":[]}]}],"state":{}},{"type":"block","id":"YAwN-1655894152917","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"9p6z-1655894152915","leaves":[{"text":"","marks":[]}]}],"state":{}},{"type":"block","id":"7Ttv-1655894153057","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"PTOy-1655894153055","leaves":[{"text":"到这里,文章开始列出的四个问题都应该找到了答案,可能列出的 RDB 细节不一定全,朋友们可以在手机端打开文章跳到下面,点击“发消息”进行交流。","marks":[]}]}],"state":{}},{"type":"block","id":"jxWH-1655894064030","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"iMEJ-1655894064029","leaves":[{"text":"或者加入数据库交流群讨论,入群请添加下方群秘微信,备注“进群”,等待群秘邀你入群。","marks":[]}]}],"state":{}},{"type":"block","id":"uC0W-1655894356010","name":"paragraph","data":{},"nodes":[{"type":"text","id":"Ki9E-1655894356011","leaves":[{"text":"","marks":[]}]}],"state":{}},{"type":"block","id":"T7WM-1655808430036","name":"paragraph","data":{},"nodes":[{"type":"text","id":"HCQf-1655808430034","leaves":[{"text":"","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontSize","value":16},{"type":"fontFamily","value":"Arial"}]}]}],"state":{}},{"type":"block","id":"Er30-1655809352544","name":"paragraph","data":{},"nodes":[{"type":"text","id":"taT4-1655809352542","leaves":[{"text":"","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontSize","value":16},{"type":"fontFamily","value":"Arial"}]}]}],"state":{}},{"type":"block","id":"GDHK-1655809366432","name":"paragraph","data":{},"nodes":[{"type":"text","id":"kmTC-1655809366430","leaves":[{"text":"","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontSize","value":16},{"type":"fontFamily","value":"Arial"}]}]}],"state":{}},{"type":"block","id":"wG4v-1655808436810","name":"paragraph","data":{},"nodes":[{"type":"text","id":"n1ZI-1655808436809","leaves":[{"text":"","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontSize","value":16},{"type":"fontFamily","value":"Arial"}]}]}],"state":{}},{"type":"block","id":"NShA-1655792324924","name":"paragraph","data":{},"nodes":[{"type":"text","id":"VAG8-1655792324923","leaves":[{"text":"标题:知道 Redis RDB 这些细节,可以少踩很多坑","marks":[]}]}],"state":{}}]">
带着上面这些问题,我们一起来聊聊 RDB 的一些细节。
触发 RDB 文件创建的命令有两条,save 和 bgsave。
save 我们知道会阻塞整个实例,通常也不太可能会用。
bgsave 命令是在后台生成 RDB 文件,Redis 仍然可以处理客户端请求。
但是并不能保证 bgsave 不会影响 Redis 所有的客户端请求,在生成 RDB的过程中,Redis 会 fork 出一个子进程,子进程和父进程会共享内存地址空间,可以保证子进程拥有父进程相同的内存数据。但是在 fork 子进程时,操作系统需要将父进程的内存页表复制给子进程。如果整个 Redis 实例占用的内存很大,那么它的内存页表也会很大,复制的时间也会比较长。
同时,这个过程会消耗大量的 CPU 资源,在复制完成之前,父进程也会被阻塞,无法处理客户端请求。
执行 fork 后,子进程可以扫描 Redis 中所有数据,然后将所有数据写入 RDB 文件。
之后,父进程仍然处理客户端的请求。父进程在处理写命令时,会重新分配新的内存地址空间,向操作系统申请新的内存使用,不再与子进程共享。这样,父子进程的内存会逐渐分离,父进程会申请新的内存空间并改变内存数据,子进程的内存数据不会受到影响。
可以看出,在生成RDB文件时,不仅消耗CPU资源,还需要消耗更多的内存空间。
上面也就是“开启 RDB 落盘,业务频繁出现请求超时”的原因。通常在生产环境,我们也应该避免在 master 实例上做 RDB。
那么除了 save 和 bgsave 命令,还有哪些常见会触发 RDB 呢?这里就来总结几种情况,这也是问题“除了 save 和 bgsave 命令,还有哪些操作会触发 RDB 落盘呢?”的答案:
1 配置了 RDB 落盘的情况
比如配置文件配置了 save xxx xxx,或者命令行执行了 config set save "xxx xxx",都表示配置了 RDB 落盘。
比如配置了 save 900 1
则表示 900 秒内如果至少有 1 个 key 的值变化,则做一次 bgsave。
我们在前面有提到 bgsave 也会对客户端请求有所影响,所以不建议在 master 上增加该参数,如果为了数据备份,建议只在 slave 增加 save 参数。
2 主从复制
次创建主从复制关系时,会在主库执行 bgsave 命令生成 RDB 文件,然后传给从库,从库加载 RDB 文件,以完成一次全量数据的传输。
因此在业务访问高峰,并且数据量比较大的情况,不建议在 master 上创建 slave。
3 Redis 在执行 flushall 命令
配置了 RDB 落盘的情况,在执行 flushall 命令时,会进行一次 RDB 落盘,但是内容是空的。目的是将 RDB 文件也清空。
但是,如果 RDB 和 AOF 都关闭的情况下,会有下面这种情况:
127.0.0.1:6301> set aaa 111
OK
127.0.0.1:6301> set bbb 111
OK
127.0.0.1:6301> bgsave
Background saving started
127.0.0.1:6301> flushall
OK
127.0.0.1:6301> config get save
"save"
""
127.0.0.1:6301> config get appendonly
"appendonly"
"no"
127.0.0.1:6301> shutdown
再启动 Redis
127.0.0.1:6301> keys *
"aaa"
"bbb"
会看到我们清空 Redis 之前写入的数据。显然是不符合逻辑的。
这是因为在启动 Redis 时,会加载数据目录下的 RDB 文件,而这个 RDB 文件是 flushall 之前执行 bgsave 生成的,也就是会看到清空 Redis 之前写入的数据。这里也是“执行了 flushall,发现 flushall 之前写的数据又冒出来了”的原因。
所以在实例未开启 RDB 和 AOF 的情况下,如果执行 了 flushall 命令,建议再执行一次 bgsave,让 RDB 文件也清空。
另外还测试了开启 AOF,关闭 RDB 的情况:
127.0.0.1:6301> set aaa 111
OK
127.0.0.1:6301> set bbb 111
OK
127.0.0.1:6301> bgsave
Background saving started
127.0.0.1:6301> flushall
OK
127.0.0.1:6301> config get save
"save"
""
127.0.0.1:6301> config get appendonly
"appendonly"
"yes"
127.0.0.1:6301> shutdown
启动 Redis
127.0.0.1:6301> keys *
(empty list or set)
重启之后不会再看到 flushall 之前写入的数据,因为 Redis 在 启动时加载 RDB 文件后,也会加载在执行 RDB 之后 AOF 里新增的操作。而 flushall 操作就记录在 AOF 文件中。
4 正常关闭时
我们通过在命令行执行 shutdown 正常关闭 Redis 时,并不是所有情况都会执行一次 RDB 落盘的,这里就来分析一下不同配置,重启后的情况。
4.1 AOF 和 RDB 都开启的情况
127.0.0.1:6301> set bbb 111
OK
127.0.0.1:6301> config get save
"save"
"1800 1"
127.0.0.1:6301> config get appendonly
"appendonly"
"yes"
127.0.0.1:6301> shutdown
启动 Redis
127.0.0.1:6301> get bbb
"111"
这种情况会在关闭时执行一次 RDB 落盘,启动时加载 RDB 文件,保证重启前后数据一致。
4.2 AOF 和 RDB 都未开的情况
127.0.0.1:6301> set ccc 111
OK
127.0.0.1:6301> config get save
"save"
""
127.0.0.1:6301> config get appendonly
"appendonly"
"no"
127.0.0.1:6301> shutdown
然后启动 Redis
127.0.0.1:6301> get ccc
(nil)
发现重启之前 ccc 的 key 已经丢失,因此在 master 未开启 RDB 的情况,关闭之前需要主动执行 bgsave,否则会导致数据丢失。这也是“重启 Redis 实例之后,发现数据有丢失的情况”的原因。
4.3 AOF 关闭,RDB 开启的情况
127.0.0.1:6301> set ddd 111
OK
127.0.0.1:6301> config get save
"save"
"1800 1"
127.0.0.1:6301> config get appendonly
"appendonly"
"no"
127.0.0.1:6301> shutdown
启动 Redis
127.0.0.1:6301> get ddd
"111"
这种情况会写 RDB,重启后数据未丢失。
4.4 AOF 开启,RDB 关闭的情况
127.0.0.1:6301> set eee 111
OK
127.0.0.1:6301> config get save
"save"
""
127.0.0.1:6301> config get appendonly
"appendonly"
"yes"
127.0.0.1:6301> shutdown
启动 Redis
127.0.0.1:6301> get eee
"111"
这种情况尽管不会进行 RDB 落盘,但是因为之前的操作都写入了 AOF,在 Redis 启动时,会加载 AOF 里的数据,因此也会跟关闭之前的数据保持一致。
到这里,文章开始列出的四个问题都应该找到了答案,可能列出的 RDB 细节不一定全,朋友们可以在手机端打开文章跳到下面,点击“发消息”进行交流。
原文链接:https://mp.weixin.qq.com/s/RiOD50FnmbWfpHzyk5nFdA