Consistency Models and Distributed Databases Questions
Data correctness across distributed systems: strong versus eventual consistency, the CAP and PACELC trade-offs, consensus and quorum reads/writes, and consistency-versus-availability decisions. Covers how distributed databases reconcile replicas and what guarantees applications can rely on. A staple of distributed-systems and architecture interviews.
What is eventual consistency? Using a food-delivery-style app as your running example, describe one workflow where eventual consistency is acceptable (for example, order-history or delivery-analytics replication) and one where it is not (for example, capturing a payment). Explain what you would actually do to reduce the business risk created by the gap between when a write happens and when every reader sees it.
Sample Answer
Direct answer
Eventual consistency means that after a write stops happening, all replicas of the data will eventually converge on the same value, but there is no guarantee about how long that takes or what a reader sees in the meantime. It trades a temporary window of staleness for lower write latency and higher availability, and it is the right default for data where a slightly-stale read is harmless, and the wrong default where a stale read causes real damage.
Structured elaboration
Whether eventual consistency is acceptable comes down to one question: what does the application actually do with a stale read?
- Tolerant workloads: anything the user does not act on financially or safety-critically in the moment. Order history, delivery-tracking analytics, recommendation feeds, and dashboard counters are all fine to serve slightly stale, because a few seconds of lag has no real consequence.
- Intolerant workloads: anything where a stale read causes an incorrect real-world action. Capturing a payment, decrementing the last unit of inventory, or checking an account balance before a withdrawal are all cases where a stale read can produce double-charges, oversells, or overdrafts.
The dividing line is not the technology, it is the cost of being wrong for a few hundred milliseconds to a few seconds.
Worked example
Picture a food-delivery app.
- Acceptable: the "your driver is 4 stops away" tracker and the "orders this month" analytics dashboard read from an asynchronously-replicated read replica. If that replica is a second behind, the customer sees the driver's position update a second late, which nobody notices.
- Not acceptable: the moment a customer taps "place order" and their card is charged. If two replicas of the payment-capture record briefly disagree about whether the charge already happened, a naive retry can charge the card twice. This path needs a strongly-consistent read (or an idempotency key tied to the order, so a retry is safe regardless of replication lag).
A second, different domain shows the same trade-off with a different shape of consequence. Picture a social-feed app instead: a user posts a photo and immediately likes their own post. Because "post visible to followers" and "like count" are two independently-replicated pieces of data, a reader can briefly see a user-visible anomaly: the poster's own like counted in the total but the post itself not yet visible in a follower's feed, or the reverse, the post visible but the like count still showing the pre-like value. Nobody's money or safety is at stake here, so full strong consistency for every post and every counter would be a wildly expensive fix for a cosmetic problem. The mitigation is much cheaper than moving to strong consistency everywhere: have the poster's own client apply an optimistic local update (show "liked", show the post as posted, immediately, from the write they just issued) regardless of what the shared aggregate view currently shows, while everyone else's feed is allowed to catch up asynchronously over the next second or two. This is the same "read-your-writes for the writer only" idea as the food-delivery payment case, just applied to a cosmetic anomaly instead of a financial one, which is the point: the fix pattern generalizes across very different domains and severities.
Trade-offs and mitigations
You rarely need to make the whole system strongly consistent to fix this. Options, cheapest first:
- Read-your-writes for the writer only: route the customer's own immediate post-order reads (or, in the social-feed case, the poster's own view of their own post) to the primary or a replica guaranteed to have applied their write, while everyone else's dashboard or feed keeps reading from a lagging replica.
- Idempotency keys on the write path itself, so even if a client retries under uncertainty, the payment is captured at most once regardless of what any read shows.
- Reserve strong consistency for the specific field that matters (payment status, inventory count for the last few units) rather than promoting the entire order record, or the entire social graph, to strong consistency, which would slow down the majority of reads that never needed it.
The common mistake is treating "eventual consistency" as a single global switch. In practice it is a per-field decision: most of an application, whether it is a checkout flow or a social feed, can tolerate staleness, and only the handful of fields tied to money, safety, or the acting user's own immediate perception of their own action need the latency cost of strong consistency.
Explain the CAP theorem and how CAP trade-offs actually manifest in real distributed databases (for example, Cassandra, MongoDB, CockroachDB, Spanner). For a financial payments system versus a shopping-cart analytics system, recommend consistency and availability settings (for example, quorum sizes, synchronous vs asynchronous replication) and justify your choices in terms of user experience and failure modes.
Sample Answer
Direct answer
CAP forces a real distributed database to choose, during a network partition, between staying available and staying consistent, and different production databases make that choice differently by default: Cassandra and DynamoDB default to availability (AP), MongoDB defaults to consistency on its primary-driven writes (closer to CP), and CockroachDB and Spanner are built CP from the ground up, using consensus per range of data. For a financial payments system you want a CP configuration with a majority write quorum, because a lost or double-applied write is unacceptable. For a shopping-cart analytics dashboard you want an AP configuration tuned for availability, because a few seconds of staleness is invisible and losing availability during a network blip is the worse outcome.
Structured elaboration
- Financial payments (recommend CP, majority quorum, synchronous replication): use a write quorum requiring a strict majority of replicas (for N=5 replicas, W=3, R=3, so R+W=6 > N=5, which guarantees every read sees the latest committed write). Replicate synchronously to at least that majority before acknowledging the write, so a client is never told a payment succeeded when it could still be lost on a single-node failure. The cost is added write latency and the possibility of temporarily refusing writes if a majority is unreachable, both acceptable trade-offs for money movement.
- Shopping-cart analytics (recommend AP, low quorum, asynchronous replication): use a low write quorum (W=1, sometimes called ONE) so a write is acknowledged the instant a single replica accepts it, and replicate asynchronously to the rest. Reads can go to whichever replica is nearest, tolerating a stale count. During a partition, both sides of the cluster keep serving, which matters far more for a dashboard than any staleness bound does.
Worked example (executed quorum arithmetic)
For N=5 replicas, is R=3, W=3 strongly consistent, and how many node failures can each side tolerate?
def strongly_consistent(N, R, W):
return (R + W) > N
Running this for N=5, R=3, W=3: R+W = 6 > N = 5, so strongly_consistent returns True (executed; confirmed). A write still succeeds with up to N - W = 2 replicas down, and a read still succeeds with up to N - R = 2 replicas down, which is the majority-quorum configuration recommended above for the financial case.
Compare that to the fast, availability-favoring configuration used for the analytics dashboard: N=3, R=1, W=1. Here R+W = 2, which is not greater than N=3, so strongly_consistent returns False (executed; confirmed). A write only needs 1 of 3 replicas to succeed (tolerating 2 node failures), which is exactly the low-latency, high-availability behavior the dashboard workload wants and can afford, because an occasional stale read costs nothing.
Trade-offs and pitfalls
The mistake to avoid is picking one quorum configuration for the whole database. The financial system and the analytics dashboard are not the same workload wearing different UI: the payments path needs R+W>N (majority quorum) and synchronous replication because the cost of being wrong is a lost or double-applied dollar; the analytics path deliberately drops that guarantee because the cost of being wrong is a number that is off by a few seconds, which nobody notices, in exchange for materially better latency and availability. Applying the payments-grade quorum to the dashboard would slow it down for no benefit; applying the dashboard's low quorum to payments would risk lost money for a latency win nobody needed there.
Two teams own services with conflicting requirements: Team A needs strong consistency on writes (which raises latency), Team B needs sub-50ms reads. Propose architecture and policy options (consistency models, API design, caching, eventual-consistency compromises) that reconcile both requirements while minimizing complexity and operational risk.
Sample Answer
Direct answer
The two teams' requirements are not actually incompatible on the SAME data, they are only in conflict if you insist on serving both from one undifferentiated read/write path. The fix is to split the responsibility: Team A's strongly-consistent writes stay strongly consistent (accept the latency), while Team B's reads are served from a fast, eventually-consistent read path, most simply a cache or read replica in front of the same source of truth, so Team B never touches the write path's latency at all.
Structured elaboration
- Keep the write path strongly consistent for Team A: writes go through a quorum (or a single leader) so the moment a write is acknowledged, it is durable and correct. This does not change; Team A's requirement is non-negotiable and it lives entirely on the write side.
- Decouple Team B's reads onto a fast path: introduce a cache, a read replica, or a materialized view that Team B reads from, refreshed asynchronously from the strongly-consistent source. This gets Team B's sub-50ms target because a cache read never pays the write path's coordination cost, at the cost of Team B's reads being slightly stale relative to the absolute latest write.
- Make the staleness bound explicit, not implicit: agree with Team B on how stale "sub-50ms reads" is allowed to be (a bounded-staleness guarantee, not unbounded eventual consistency), and monitor the actual replication lag against that bound so a degradation is caught before it silently exceeds what Team B's use case can tolerate.
- API design matters: expose the two access patterns as genuinely different API calls or endpoints (a strongly-consistent "read after my own write" endpoint for Team A's own confirmation flow, and a fast "read the latest available" endpoint for Team B), rather than one endpoint with an ambiguous consistency guarantee that neither team can rely on confidently.
Worked example
A checkout service (Team A) needs its own write, and its own immediate confirmation read, to be strongly consistent, since telling a customer "order placed" before the write is durable risks losing the order. A separate order-status widget elsewhere in the product (Team B, sub-50ms reads) does not need that guarantee: it reads from a cache populated asynchronously from the same underlying order data, refreshed within, say, a 200ms bound. Team A's write path and Team B's read path can share the same source of truth without either team paying for the other's requirement.
Trade-offs and pitfalls
The main operational cost of this split is now having two paths to keep healthy instead of one: the cache or replica feeding Team B's fast reads needs its own monitoring (is it actually staying within the agreed staleness bound, and what happens if replication falls behind), and a bug in cache invalidation can silently violate Team B's freshness assumption without anyone noticing until a customer complains. The common mistake is trying to satisfy both teams with a single tunable "consistency level" setting on one shared path, which forces a compromise that under-serves both (either Team A's writes get riskier, or Team B's reads get slower) instead of giving each team exactly what they actually need.
Dynamo-style distributed databases typically expose more than one consistency level to the application rather than a single fixed guarantee. Name three common levels, explain what each one actually guarantees to the caller, and give one realistic use case where you would pick that level over the others.
Sample Answer
Direct answer
Dynamo-style databases commonly expose three consistency levels an application can choose per operation: strong (contact every replica / ALL), quorum (majority, R + W > N), and eventual (contact one replica / ONE). Strong consistency contacts every replica and always returns the latest committed write, at the cost of the highest latency and the lowest availability during a partition; eventual consistency returns whatever a nearby replica has, fastest and most available, but possibly stale; quorum consistency sits between the two, requiring only a majority of replicas to agree (R + W > N), which gives a strong practical guarantee (every read quorum is guaranteed to overlap every write quorum in at least one replica) without paying the cost of contacting every single replica on every operation.
Structured elaboration
- Strong (ALL) reads: the read is guaranteed to reflect the most recent successful write, as if there were only one copy of the data. Implemented by requiring every replica (R = N or W = N) to participate, or by always routing to the current write leader in systems that have one.
- Eventual (ONE) reads: the read may return an older value if it lands on a replica that has not yet received the latest write. Implemented by reading from whichever single replica is closest or least loaded, no quorum coordination required.
- Quorum (majority) reads: a read or write is acknowledged only after a majority of replicas respond (for N=3, a quorum is 2; for N=5, a quorum is 3). Choosing R and W so that R + W > N guarantees every read quorum overlaps every write quorum by at least one replica, so a quorum read is guaranteed to see the most recent quorum-acknowledged write, without the latency and availability cost of waiting on every single replica the way ALL does.
Worked example
- Strong (ALL) reads: a user checks their own account balance immediately after a transfer. They must see the transfer reflected, so the read pays the latency cost of confirming with every replica (or the leader).
- Eventual (ONE) reads: a public-facing "total likes on this post" counter. A read that is a few seconds behind is invisible to the user experience and the read stays cheap and highly available.
- Quorum reads: an inventory count during checkout, where ALL would be too slow and too fragile (any single slow replica blocks the read), but ONE risks showing stale stock and overselling the last unit. QUORUM (for N=3, R=2, W=2, so R+W=4 > N=3) gives a strong, majority-backed answer while still tolerating one replica being slow or down, which is the practical default most production Dynamo-style deployments reach for when they need "correct and fast" rather than either extreme.
Trade-offs and pitfalls
The three levels are a latency/availability-versus-freshness dial, not a correctness hierarchy where "stronger is always better." Choosing ALL for every read on a high-traffic, low-stakes field (like a like-counter) needlessly funnels all that traffic through every replica and makes the system less available during a partition, for a guarantee the product never needed. The common mistake is picking the strongest level available "to be safe" instead of matching the level to what a stale read would actually cost.
Compare ACID guarantees with the BASE model (Basically Available, Soft state, Eventually consistent) used by many distributed and NoSQL systems. Discuss the trade-offs in latency, availability, and developer complexity, and give examples of applications that can tolerate eventual consistency along with techniques to manage the resulting complexity.
Sample Answer
Direct answer
ACID (Atomicity, Consistency, Isolation, Durability) is the guarantee model of traditional relational databases: every transaction leaves the data in a valid state, transactions do not interfere with each other, and once committed a write survives failures. BASE (Basically Available, Soft state, Eventually consistent) is the looser model many distributed and NoSQL systems adopt instead: the system stays available even during faults, its state may be in flux, and it only promises replicas will converge eventually, not immediately. The trade is availability and latency now, correctness later, versus correctness now, at the cost of availability and latency.
Structured elaboration
| ACID | BASE | |
|---|---|---|
| Core promise | Transaction is atomic, isolated, and durable the instant it commits | System stays available; data converges over time |
| Typical cost | Coordination (locking, quorum, or consensus) on every write | Little to no coordination on writes |
| Write latency | Higher, pays for coordination | Lower, writes accepted locally and propagated async |
| Availability under partition | Lower (may refuse writes to stay correct) | Higher (keeps accepting writes on both sides) |
| Developer burden | Lower (the database enforces correctness) | Higher (application must handle stale reads and conflicting writes) |
Worked example
A banking ledger needs ACID: if a transfer debits one account and credits another, both must happen together or not at all, and a concurrent read must never see the money "missing" between the two steps. Losing that guarantee for lower latency is not an acceptable trade for money movement.
A social-media "like count" or a product's "recently viewed" list can run on BASE: if a like posted a moment ago has not yet propagated to every replica, the count is off by one for a few seconds and nobody is harmed. The application gets a large availability and latency win in exchange for tolerating that brief inconsistency, and it can hide the seam entirely from the user (a like button that instantly shows "liked" locally, regardless of what the aggregate counter currently displays).
Trade-offs and pitfalls
BASE does not mean "no guarantees," it means the guarantees are weaker and the application must compensate for the gap: idempotent writes so a retry under uncertain state does not double-apply, conflict-resolution logic (last-write-wins, CRDTs, or application-level merge rules) for when two replicas disagree, and UI or business-process design that tolerates a visible staleness window. The common mistake is picking BASE for latency reasons without budgeting for that compensating logic, which produces silent correctness bugs (double-counted actions, lost updates) rather than the loud failures ACID would have produced instead.
Unlock Full Question Bank
Get access to all 12 Consistency Models and Distributed Databases interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.