liveBook · Manning

Callback API

As explained in MongoDB 8.0 in Action, Third Edition

The Callback API is MongoDB's transaction interface that runs transaction operations through a callback and manages transaction completion and retries.

MongoDB’s Callback API runs transaction operations inside a callback and manages transaction startup, completion, aborts, and retries. It is the preferred alternative to the lower-level Core API for implementing multidocument transactions. section 8.4.1 section 8.4.3

A typical transaction follows these implementation steps:

  • Start a logical session and associate every transaction operation with that session. section 8.4.1
  • Define a callback containing the related reads and writes that must succeed or fail together. section 8.4.3
  • Invoke the language driver’s callback helper, such as Node.js withTransaction, to start, commit, or abort the transaction. section 8.4.3
  • Let the API retry transient transaction failures and uncertain commit results instead of implementing those mechanisms manually. section 8.4.1

Node.js transaction

Create a MongoClient, connect to MongoDB, start a session, and call session.withTransaction with an async callback. The callback can read account, customer, and transaction collections; the helper handles transaction completion and retries. Configure readConcern, writeConcern, and readPreference when the operation requires those settings. section 8.4.3

Table 8.1 Core API vs. Callback API

Core API

Callback API

Requires an explicit call to initiate and commit the transaction

Initiates a transaction, performs the specified operations, and commits them or aborts if an error occurs

Does not automatically handle errors such as TransientTransactionError and UnknownTransactionCommitResult. Instead, it allows the integration of custom error-handling mechanisms.

Automatically includes error-handling logic for TransientTransactionError and UnknownTransactionCommitResult

Requires explicitly passing a logical session to the API for each transaction

Requires explicitly passing a logical session to the API for each transaction

Compare the Callback API with the Core API for lifecycle control, error handling, and logical-session requirements. section 8.4.1

The convenience has an operational boundary: distributed transactions usually cost more than single-document writes. Prefer schema designs with embedded documents and arrays when they can keep related data together and avoid a transaction. section 8.4.3

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains sessions, callbacks, commits, aborts, and retry categories, then demonstrates driver-specific implementations without detailing driver internals.
Examples
Demonstrates account, customer, and transaction collections with Node.js, PyMongo, and Ruby callback implementations.
What it assumes
Assumes familiarity with MongoDB collections and operations, while introducing logical sessions and callback-based transaction control.
What you can do after
Apply it via MongoDB drivers to implement transactions with automatic lifecycle handling and retry support.
How it connects
Connects logical sessions, ACID transactions, retryable writes, causal consistency, schema design, and the Core API.

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.