What does referencing instead of embedding cost you in write atomicity?
answer
- Atomicity has a scope
- One write, one document
- Two documents, two writes, one window
- Crashes make the window permanent
- Transactions exist but are not free
basics
~20 sA write to one document is atomic; a write spanning two is not. Once related data is referenced, an update can be observed or left half-applied, unless you pay for a multi-document transaction or design the drift away.
solid answer
~50 sDocument stores guarantee that a single write to a single document is all-or-nothing and isolated, no matter how many fields and nested elements it touches. Embedding puts data that must change together inside that guarantee for free. The moment you reference instead, the same change becomes two writes, and three new problems appear: a reader can catch the intermediate state; a crash between the writes leaves the two records permanently disagreeing; and concurrent writers can interleave. The remedies all cost something. A multi-document transaction restores atomicity where the product supports it, but adds contention, latency and operational limits, and is not available in every store or topology. Otherwise you design the drift away — make the second write idempotent and retryable, order the writes so the visible intermediate state is harmless, or make the derived side reconstructible and run a reconciliation job. Atomicity is a genuine argument for embedding, but not a licence to embed an unbounded relationship.
code
json · 5 lines{
"_id": "acct-9",
"status": "suspended",
"suspension": { "reason": "fraud-review", "at": "2026-08-19T09:00:00Z" }
}go deeper
Recall the boundary: one document written in one operation is all-or-nothing, and that promise stops at the edge of the document.
Explain what appears when the data is split — an observable window, permanent drift after a failure, and interleaving — and name the remedies without pretending transactions are free.
Demonstrate the judgment call on a real invariant: which rule must never be seen broken, whether the document that would hold it stays bounded and cool enough to write, and what reconciliation you would run regardless.
Own where the organisation places its consistency boundaries, what staleness each product surface is allowed to show, and whether cross-document transactions are permitted on hot paths at all.
## What "atomic per document" means Every mainstream document store guarantees that a write to a single document either applies completely or not at all, and that no reader sees it half-applied. This holds however deep the change goes: you can set a top-level field, push onto a nested array and decrement a counter three levels down in one operation, and every reader sees the state either before or after, never in between. That guarantee is the quiet reason embedding is attractive. If two facts must always agree — an account balance and the ledger entry that produced it, an order's total and its line items — putting them in one document makes their agreement a property of the storage engine rather than of your code. ## What you give up when you reference Split those two facts into two documents and the single guarantee no longer spans them. Three distinct problems appear, and candidates usually name only the first. **Visible intermediate state.** Between the two writes there is a window in which one record reflects the change and the other does not. Any concurrent reader may land in that window. How bad that is depends entirely on what the reader does with it — a stale count on a dashboard is nothing, a stale entitlement check is a security bug. **Permanent drift on failure.** If the process crashes, the connection drops, or the second write is rejected, the window never closes. Nothing in the database will repair it. Unlike a transient inconsistency, this one accumulates: every failure adds a record pair that will disagree forever until something goes looking. **Interleaving.** Two concurrent operations touching the same pair can apply their writes in different orders on each side, producing a combination neither of them intended. This is not solved by retrying. ## The remedies and their price **Multi-document transactions.** Modern document stores offer them, and they genuinely restore atomicity across documents. The price is real: extra coordination latency, contention between transactions touching the same documents, limits on how long a transaction may run and how much it may change, and a substantially higher cost when the documents live on different partitions. They exist for the cases that need them, not as a replacement for a model that keeps a rule inside one document. **Idempotent, retryable second writes.** If the second write can be replayed safely, the failure mode becomes "retry until it lands" instead of "lose the update". This usually means making the write assert a target state rather than apply a delta, so replaying it changes nothing. **Ordering for a benign intermediate state.** Choose which write goes first so that the window, if observed, is harmless or self-correcting — write the authoritative record before the derived one, so a reader sees a missing derived value rather than a wrong one. **Reconstructibility plus reconciliation.** If the second document is derivable from the first, drift is repairable by a background job that recomputes it. This is the most robust answer at scale, because it survives every failure mode rather than trying to prevent them. ## Why atomicity is not a licence to embed everything The argument runs the other way too. Pulling a large relationship inside one document to get atomicity concentrates all of its write traffic on one record. Every writer now contends for the same document, so throughput on that entity is bounded by the rate at which one record can be updated, and hot entities serialise. Embedding also reintroduces every growth problem an unbounded relationship has. When the relationship is large or hot, referencing plus one of the remedies above is the correct answer even though it is the weaker guarantee. ## How to decide Ask what rule has to hold. If there is an invariant that must never be observed broken — a state machine, a balance, an approval and the thing it approves — the data it spans wants to be in one document, provided that document stays bounded and its write rate is survivable. If the relationship is merely convenient to read together, atomicity is not the deciding factor and you should choose on locality and growth instead. ## Saying it well in an interview State the guarantee precisely (one document, one write, all-or-nothing, isolated), name the three failure modes referencing introduces, price the remedies honestly, and finish with the counterweight: the same embedding that buys atomicity also buys contention and unbounded growth, so this driver is weighed against the others rather than winning outright.
- If a multi-document transaction is available, why not just reference everything and wrap the writes?Because the guarantee is not free. Transactions add coordination latency, hold conflict state for their duration, and are far more expensive when the documents sit on different partitions; products also cap how long they run and how much they touch. Used routinely on hot paths they become the bottleneck. They are the right tool for genuinely cross-entity operations, not a substitute for putting a single invariant in a single document.
- How would you repair two referenced documents that have already drifted apart?First decide which one is authoritative — usually the one written first, or the one carrying the source event. Then make the other reconstructible from it and run a reconciliation pass that recomputes and reports mismatches rather than silently overwriting. Keep the job running afterwards: drift is produced continuously by ordinary failures, so a one-off repair only resets the clock.
- Does embedding remove all write-concurrency problems?No, it moves them. Two writers updating different embedded children still contend on the same document, so a hot parent serialises updates that separate documents would have absorbed in parallel. Embedding converts a consistency problem into a contention problem, which is often the better trade, but it is a trade.
saying these in an interview costs you the question
- Believes writes to multiple documents are atomic by default
- Treats multi-document transactions as free and unlimited
- Ignores that a crash makes the inconsistency permanent
- Embeds a hot relationship for atomicity, ignoring write contention
- Says retries alone guarantee the two writes agree