什么是mongodb的 Snapshots and Checkpoints
发布于 4 年前 作者 TimLiu1 4537 次浏览 来自 问答

读完mongodb的官方解释,下面是我的理解 checkpoints: 每隔60秒或则journal data到达2G的时候做一次动作 snapshot: 我有两种理解

  1. 是在每个checkpoints的时候把内存中的数据持久化(我觉得这个不靠谱,这样就代表当mongodb shutdown的时候就会丢失最后60秒写的动作)
  2. 是在每个checkpoints的时候把积压在内存中等待写的动作写入journal,这样即使mongodb shutdown了,我们最多丢失60秒数据,至于为什么是60s,我觉得是时间和性能做的平衡取舍

第二个答案我觉得应该是对的,但是我不确定,所以在这提问一下

8 回复

在WiredTiger引擎下,3.6版本以前的是看下面两个条件哪个条件先满足触发

  1. 时间间隔60s
  2. journal data 大小达到2GB

3.6版本以后只关注上述条件1满足就触发 对于你说的丢失60s数据,我认为不存在,可以参考The now-durable data act as a checkpoint in the data files 然和和 durable的介绍做比对 https://docs.mongodb.com/manual/reference/glossary/#term-durable 就算此时crash or shutdown,也不影响,因为数据已经写入进服务器的journal data 也就是日志文件里面了,重启后,mongod服务器也会从服务器的journal data 恢复,不然这样的设计不是有问题不,就怕此时的写操作没有写到服务器日志,而且下面也提到了4.0版本以后不允许去设置关闭journal data 。因此journal data 就非常重要了,合理的关闭mongod服务器也就很重要了 见https://docs.mongodb.com/manual/core/journaling/#journal-process

@jxycbjhc 感谢,也就是我理解的是正确的,是每60秒会把等待执行的命令写进journal data,如果特殊情况来不及执行最多会丢失最后60秒,可以这么理解吗

@jxycbjhc 还有另外一种理解是在持续的写journal data,但是是60秒把内存中完成的写的数据同步到数据库,如果journal data关闭,那最多丢失60秒的数据

@TimLiu1 不会丢失60s, 参考 https://docs.mongodb.com/manual/core/journaling/#journal-process WiredTiger syncs the buffered journal records to disk upon any of the following conditions 里有个 At every 100 milliseconds ,也即是100毫秒 还有下面的important 提示 In between write operations, while the journal records remain in the WiredTiger buffers, updates can be lost following a hard shutdown of mongod. 也即是在两次写之间,如果非正常关闭mongod是会有数据不一致的,也就是后一个写操作没有生效,这个情况也即是单机下面会出现,其他的几种情况都会主动把在内存中的 journal records同步到硬盘里。你说的关闭journal data 不一定就是60s影响,就是数据只能恢复到上个有journal data的checkpoint,由于没有journal data 重放就不能进行了。

@jxycbjhc 感谢你的回答,学到一些新的东西,那每个60s做一个snapshot是做了一个什么动作,我感觉你还是没有完全解决我的疑惑

@TimLiu1 https://docs.mongodb.com/manual/core/backups/ 提到 了To get a correct snapshot of a running mongod process, you must have journaling enabled and the journal must reside on the same logical volume as the other MongoDB data files. Without journaling enabled, there is no guarantee that the snapshot will be consistent or valid. 快照依赖于你的操作系统,mongodb是在快照上面设置checkpoint,也即是需要有journal data ,这个解释应该就清楚了。

@jxycbjhc 这段话说snapshot依赖于journal data,但是并没有说snapshot到底是什么

@TimLiu1 Snapshots work by creating pointers between the live data and a special snapshot volume. These pointers are theoretically equivalent to “hard links.” As the working data diverges from the snapshot, the snapshot process uses a copy-on-write strategy. As a result, the snapshot only stores modified data我意思就是mongodb的快照也是类似这玩意,用的是写时复制参考 https://baijiahao.baidu.com/s?id=1636278070863441309&wfr=spider&for=pc,我倒是觉得一看这个 snapshot就是文件系统那一套。

回到顶部