liveBook · Manning

Idempotency

As explained in MongoDB 8.0 in Action, Third Edition

An idempotent operation produces the same resulting state when applied repeatedly as when applied once.

Idempotency means repeated application produces the same resulting state as a single application. MongoDB uses idempotent oplog operations so secondaries can replay writes consistently while replicating asynchronously. section 9.1.3

The oplog applies idempotency through ordered logical writes:

  • The primary executes database operations and records successful data-changing writes in its oplog. section 9.1.3
  • Secondaries receive and execute those operations asynchronously, using local oplog copies to track database state. section 9.1.3
  • Writes that neither change data nor succeed do not generate oplog entries. section 9.1.3

Replayable delete operation

An oplog delete records the operation type, namespace, collection UUID, document _id, timestamp, election term, format version, and wall-clock time. Applying that recorded operation once or repeatedly leaves the target dataset in the same state. section 9.1.3

Listing 9.3 A single document from the oplog collection
  {
    op: 'd',
    ns: ' sample_mflix.sessions',
    ui: new UUID("99f8f10f-e144-4419-a543-da211dd1a2de"),
    o: { _id: ObjectId("65b1a0a4edf7e1f8d9ccc396") },
    ts: Timestamp({ t: 1710267182, i: 5 }),
    t: Long("38"),
    v: Long("2"),
    wall: ISODate("2024-03-12T18:13:02.838Z")
  },

Inspect a concrete oplog document showing a delete operation and its replay-relevant metadata. section 9.1.3

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains idempotency through oplog internals, replay behavior, write filtering, and concrete delete and insert records.
Examples
Uses sample_mflix.sessions delete and insert oplog documents, including identifiers, timestamps, namespaces, and payloads.
What you can do after
Evaluate how MongoDB replays writes consistently across replica-set members.
How it connects
Connects idempotency with oplog collections, replica sets, primary nodes, secondary nodes, and asynchronous replication.

Also explained in

Compare how 17 books explain idempotency →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.