skip to content

On a standalone MongoDB mongod that loses power 40 seconds after its last checkpoint, which w: 1 writes survive restart: one acknowledged 5 seconds earlier, or one acknowledged 20 ms earlier?

level: seniorimportance: must knowfreq 40%

answer

  1. acknowledged does not mean on disk
  2. neither write is in the checkpoint
  3. the 100 millisecond sync decides
  4. replay happens before the server is available
  5. may be lost, not must be lost

basics

~20 s

The write from 5 seconds earlier survives, because a journal sync covered it and restart replays it. The write from 20 ms earlier may be lost: w: 1 acknowledges from memory, ahead of the next 100 ms sync.

solid answer

~50 s

Neither write is in the checkpoint, since both came after it, so everything depends on the journal. On a standalone, `w: 1` with `j` unspecified is acknowledged once the write is applied in memory, and its journal record sits in a buffer until the next sync, which by default comes within `storage.journal.commitIntervalMs` of 100 ms. The write from 5 seconds ago has long since been synced, so restart opens the last checkpoint, replays the journal and brings it back. The write from 20 ms ago falls inside the window: it is lost unless a sync happened to land in those 20 ms, whether the periodic one, another client's `j: true` write or a new journal file. So the application saw a success for a write that is gone. That is documented behaviour rather than corruption, and it needs no `--repair`.

code

plaintext · 7 lines
plaintext
t = 0 s          last checkpoint completes
t = 35 s         write A acknowledged (w: 1, j unspecified)
t = 35.1 s       periodic journal sync: A is now on disk in the journal
t = 39.98 s      write B acknowledged, journal record still in the buffer
t = 40 s         power loss
restart          open the checkpoint from t = 0, replay the journal
result           A is present; B is present only if a sync caught it

go deeper

for a junior

Remember that the server can acknowledge a write before it is on disk, and that the journal is synced about every 100 milliseconds by default.

for a middle

Explain why neither write is in the checkpoint, how the journal sync decides each one's fate, and what the restart does in order.

for a senior

Say may be lost rather than is lost, name what else forces a sync, and treat the report as expected behaviour to confirm from timestamps before suspecting corruption.

for a principal

Use this as the concrete case when a team asks what acknowledged means. Loss windows are a product decision expressed in write concern, and hardware that honours flushes is part of it.

## The scenario A standalone `mongod` runs WiredTiger with default settings. Its last checkpoint finished 40 seconds before a power loss. Two writes with write concern `w: 1` and no `j` were acknowledged: write A 5 seconds before the power loss, and write B 20 ms before it. A **standalone** is a single server with no replica set, which matters because it journals all of its collections. ## What the acknowledgement meant The manual's table for a standalone is explicit: with `w: 1` and `j` unspecified, the server acknowledges after applying the write **in memory**. At that moment: - The change is in the WiredTiger cache. - Its journal record is in WiredTiger's in-memory buffer. - Nothing yet obliges the server to have synced this write to disk. The acknowledgement says the server accepted and applied the write. It is not a statement about the disk. ## When the write reaches disk WiredTiger syncs buffered journal records to disk on any of these events: - The periodic sync, every `storage.journal.commitIntervalMs`, 100 ms by default. - A write that includes or implies `j: true`, which forces an immediate sync. That sync takes earlier buffered records along with it. - The creation of a new journal file, about every 100 MB of journal data. Checkpoints are not part of this path. Both writes arrived after the checkpoint, so neither is in the data files as of that checkpoint, and the 60-second schedule tells you nothing about their fate. ## Working out each write | Write | Acknowledged | Journal synced before power loss? | After restart | |---|---|---|---| | A | 5 s before | Yes, many sync cycles ago | Present | | B | 20 ms before | Probably not | Lost, unless another sync caught it | The manual states the bound directly: by default, the greatest extent of lost writes is those made in the last 100 ms, plus the time it takes to perform the journal writes. Write B is inside that window, so the correct verb is **may**. If the periodic sync or another client's `j: true` write landed in those 20 ms, B was carried to disk with it. The answer for A carries an assumption too. A journal sync is only as good as the storage beneath it, so a device that acknowledges a flush it has not persisted can lose even synced records. ## What recovery does On restart, `mongod` recovers before it becomes available: 1. It reads the identifier of the last checkpoint from the data files. 2. It finds the matching record in the journal files. 3. It applies the journal's operations since that point, which restores write A. A few facts complete the picture: - A write's journal record includes its index changes, so A comes back whole, with its index entries. - If the power loss interrupted a checkpoint in progress, the previous complete checkpoint is still valid, and recovery starts from it. - The manual says there is no reason to run `--repair` after this kind of crash. Repair is not part of normal recovery. - Replay time grows with the journal since the last checkpoint, and `mongod` notes journal replay in its log. ## Diagnosing the report The report usually arrives as: the application logged a success, the server rebooted, and the document is not there. The checks are short: - Compare the time of the write with the time of the power loss. A gap of a few hundred milliseconds or less fits this window exactly. - Check the write concern the application used, including any default set at the driver. - Confirm the restart completed normally and that the log shows recovery, not an error. A missing write inside the window is not corruption. Treat it as the expected result of the chosen acknowledgement. If that loss is unacceptable, the fix is asking for a stronger acknowledgement such as `j: true`, whose full promise belongs with write concern. Changing the checkpoint interval does not help, and neither does `--repair`. ## A process kill is a milder case Power loss is the harsh version of this failure. If only the `mongod` process dies, the operating system survives, so records that WiredTiger had already handed to the filesystem are still written out later. WiredTiger's log server pushes buffered records to the filesystem about every 50 ms, without that being a sync. Records still in WiredTiger's own buffer are lost in both cases.

  • On this standalone mongod, would raising storage.journal.commitIntervalMs to 500 change the answer for either write?
    Not for these two. The write from 5 seconds ago is still well clear, since 500 is the largest allowed interval, and the 20 ms write was already at risk. The change widens the window of possible loss to about 500 ms plus the journal write time, so a write acknowledged 300 ms before the power loss would move from probably safe to at risk.
  • If the power loss on this standalone hits while a checkpoint is running, is the write from 5 seconds earlier still safe?
    Yes. The checkpoint being written only becomes current when WiredTiger's metadata is atomically updated, so the previous one is still valid. Recovery starts from that checkpoint and replays the journal from there, and journal files needed for it have not been removed. The interrupted checkpoint costs nothing except a longer replay.
  • How does the answer change for a killed mongod process rather than a power loss?
    Fewer writes are at risk. The operating system survives and still holds whatever WiredTiger had already handed to the filesystem, which its log server does about every 50 ms. Only records still in WiredTiger's own memory buffer are lost, although the manual still warns that buffered updates can be lost after a hard shutdown.

  • A write acknowledged with w: 1 is already on disk.
  • Anything since the last checkpoint is lost, so up to a minute of writes is gone.
  • After a power loss you must run mongod with --repair before serving traffic.
  • A write acknowledged 20 ms before the crash is certain to be lost.
  • A missing acknowledged write proves the data files are corrupt.
  • Shortening storage.syncPeriodSecs is the way to protect that last write.