02 / 10

What is core api and callback api?

Difficulty: 5/10
MongoDB driver APIs, async vs callback, API evolution

In MongoDB, the Core API and Callback API are approaches to handle transactions, enabling operations across multiple documents or collections as a single atomic unit. Here’s an explanation of both and their differences

Core API: The Core API is a lower-level interface for managing MongoDB transactions. It provides explicit control over the transaction lifecycle, requiring developers to:
  1. 1

    Start a transaction.

  2. 2

    Handle errors manually.

  3. 3

    Commit or abort the transaction explicitly.

javascript
Callback API: The Callback API is a higher-level abstraction that simplifies transaction management. It automates aspects like retrying on transient errors and handling unknown commit results.
  1. 1

    Reduces developer effort by automating retries and commit handling

  2. 2

    Recommended for most general use cases to simplify transaction workflows.

  3. 3

    Automatically incorporates error-handling logic for TransientTransactionError and UnknownTransactionCommmitResult.

javascript

Scenario Questions

0-2 years experience

  1. 1If you need to insert a document using the Node.js driver, how would you do it with the core API versus the callback API? Walk me through the code.
  2. 2What happens if you forget to call the callback in the callback API after a find operation? How does the driver behave?

2-5 years experience

  1. 1You have a legacy codebase that uses the callback API, but you need to add a new feature using async/await. How would you integrate the two styles without breaking existing functionality?
  2. 2During a performance test, you notice that using the callback API for bulk writes is slower than the core API's promise version. What could be causing the difference and how would you investigate?

5-8 years experience

  1. 1Our service processes millions of requests per second and uses the MongoDB driver. Discuss the trade‑offs of standardizing on the core API versus keeping the callback API for certain modules. Consider error handling, resource usage, and team onboarding.
  2. 2A bug appears only when a callback throws an exception inside a nested operation, causing the request to hang. How would you redesign the component to avoid this at scale?

8+ years experience

  1. 1We are planning a migration from the callback API to the core API across multiple microservices that have been in production for years. Outline a migration strategy that minimizes downtime and risk, and how you would measure success.
  2. 2From an architectural perspective, how does the existence of two parallel APIs affect our SDK design, versioning, and backward compatibility commitments? What policies would you put in place?

Follow-up Questions

  • How do you handle errors differently in the two APIs?
  • What impact does each style have on unit testing?
  • Can you show a quick code snippet converting a callback to a promise?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.