How do the MongoDB journal file and oplog differ?

后端 未结 3 1811
名媛妹妹
名媛妹妹 2021-02-05 11:51

I have recently started on MongodDb and I\'m trying to explore on replica sets and crash recovery.

I have read it like journal file are write a head redo log file. oplog

3条回答
  •  太阳男子
    2021-02-05 12:02

    Oplog is just capped collection where MongoDB tracks all changes in its collections (insert, update, delete). It doesn't track read operations. MongoDB uses oplog to spread all changes within all nodes in a replica set. Secondary nodes copy and apply this changes.

    Journal is a feature of underlying storage engine. Since MongoDB 3.2 default storage engine is WiredTiger and since MongoDB 4.0 you can't disable journaling for WiredTiger. All operations are tracked in the journal files. WiredTiger uses checkpoints to recover data in case of crash. Checkpoints are created every 60 secs. In case if a crash happens between checkpoints some data can be lost. To prevent this, WiredTiger uses journal files to apply all the changes after the last checkpoint.

    In general, write flow in MongoDB looks like that:

    • High-level - when a customer writes/updates/removes data, MongoDB applies it to proper collection, updates index and inserts the change to oplog. If any of these operations fails then other related operations must be rolled back to prevent inconsistency. For this MongoDB uses WiredTiger transactions:
      1. begin transaction
      2. apply change to collection
      3. update index
      4. add the change to the oplog
      5. commit the transaction
    • Low-level - WiredTiger runs the transaction and adds the changes to journal file.

提交回复
热议问题