Consistency Models

How quickly and how strictly every copy of your data must agree.

What is it?

Once data is copied across databases (replication) or regions, a question comes up: after you change one copy, when will every other copy show it, and how reliably? A system's answer is its consistency model.

- Strong consistency: every read, everywhere, always sees the latest write, as if there were only one copy. - Eventual consistency: a write may take a moment to reach every copy, but given enough time (and no new writes), all copies agree.

Explain like I'm 10

Strong consistency is like one shared whiteboard everyone reads directly: the moment someone writes, everyone sees it. Eventual consistency is like everyone taking their own photo of the whiteboard and syncing later: for a while some photos are a version behind, but eventually they all match.

Examples

The same read, two different consistency guarantees

// Strong consistency: guaranteed to see the write immediately
await write(userId, { balance: 100 });
const balance1 = await strictRead(userId); // always 100

// Eventual consistency: might briefly see a stale value
await write(userId, { balance: 100 });
const balance2 = await eventualRead(userId); // could still be the old value, briefly

How it works

Strong consistency usually routes every read through one source of truth, or requires several copies to confirm a write before it counts. That costs latency, and availability during network problems.

Eventual consistency lets each copy apply a write at its own pace. That's faster and more resilient to failures, but there's a short window where reads can return stale data.

Why does it exist?

There's an unavoidable tradeoff between how fast and reliable a system stays under failures and how up to date every read is guaranteed to be. Each app picks its spot based on how much staleness it can tolerate.

When to use it

Use strong consistency where staleness causes real harm: an account balance checked before a withdrawal, an inventory count at checkout.

Use eventual consistency where a short delay doesn't matter: a like count, a follower count, a search index.

When not to use it

Don't default to strong consistency everywhere "to be safe". It costs real latency and availability, and most data (activity feeds, view counts, recommendations) doesn't need it.

Common mistakes

  • Assuming all databases are strongly consistent by default. Many popular distributed databases default to eventual consistency for performance.

  • Using eventually consistent reads for something that truly needs strong consistency, like a payment balance.

  • Treating 'eventual' as 'never'. In a healthy system it usually resolves in milliseconds to seconds.

Practice exercises

  1. Easy:

    Give one example of data where eventual consistency is fine, and one where it isn't.

  2. Medium:

    Explain why strong consistency tends to cost more latency than eventual consistency.

  3. Hard:

    Describe a real bug that could happen if a signup flow read a user's data with eventual consistency right after writing it.

Interview questions

What's the difference between strong and eventual consistency?

Strong consistency guarantees every read sees the latest write immediately. Eventual consistency allows a short delay before all copies agree, trading immediacy for speed and resilience.

Why would a system choose eventual consistency over strong consistency?

It's usually faster and more available, especially during network issues, because a write doesn't need every copy to confirm before it's complete.

Give an example where eventual consistency would be an acceptable trade-off.

A like or view count on social media. Being off by a few for a moment harms nobody.

What is linearizability, and how is it a stronger guarantee than what people often casually call "strong consistency"?

Every operation appears to happen atomically at a single instant between its call and its return, and all clients agree on one global order, as if there were one copy updated one operation at a time. It's the strictest, most precisely defined form of strong consistency, not just "reads see recent writes".

What's the difference between strong consistency and causal consistency?

Strong consistency requires everyone to see every operation in the same real-time order. Causal consistency only requires that causally related operations (one happened because of, or after seeing, another) appear in the same order to everyone. Unrelated operations can appear in different orders to different observers, which is cheaper to guarantee.

Give a concrete scenario where causal consistency matters, but plain eventual consistency alone would produce a confusing result.

A reply reaches a reader's replica before the comment it replies to, so they see a reply to a comment that doesn't seem to exist. Causal consistency prevents this: since the reply depended on seeing the original, no replica shows the reply without the original.

What is "monotonic reads" as a consistency guarantee, and what bug does it prevent?

Once a client has seen a value (or a later one), it never sees an older value afterward. It prevents refreshing a page and seeing something you already saw disappear or revert because a later read hit a more lagged replica.

What is "read-your-writes" consistency, and how is it distinct from monotonic reads?

Read-your-writes guarantees a client always sees its own earlier writes. Monotonic reads is broader and not about authorship: a client never sees time go backward on reads, whoever made the write, including other users.

What is "monotonic writes" consistency?

Writes from one client are applied everywhere in the order that client sent them. A client's second write is never applied before its first, which could otherwise leave data in an unintended order (e.g. an 'undo' applied before the action it undoes).

What does quorum-based consistency (R + W > N) mean, and why does that formula guarantee an overlap?

N is the number of replicas holding the data, W is how many must acknowledge a write, and R is how many a read consults. If R + W > N, any W replicas that accepted a write and any R replicas a read consults share at least one replica, so every read reaches at least one replica with the latest write.

Scenario: N=3 replicas, W=2, R=1. Is this configuration guaranteed to always read the latest write?

No. R + W = 3, which isn't greater than N (3), so there's no guaranteed overlap. A read could hit the one replica that wasn't among the two that accepted the write and return a stale value.

Scenario: N=3, W=2, R=2. Is this configuration now guaranteed to read the latest write?

Yes. R + W = 4 > N (3), so any 2 replicas a read consults overlap with at least one of the 2 that accepted the write. The read sees it (or something newer), at the cost of contacting 2 replicas per read instead of 1.

What's the difference between "sequential consistency" and "linearizability"?

Both require all clients to agree on one global order of operations. Linearizability also requires that order to respect real time: if A finished before B started, A comes first. Sequential consistency drops that rule; any single order is fine as long as everyone agrees on it, even if it doesn't match real time.

What is PACELC, and how does it extend the CAP theorem's tradeoff?

CAP only covers the tradeoff during a network Partition (Availability vs Consistency). PACELC adds that Else, with no partition, a system still trades Latency vs Consistency, because waiting for more replicas to agree always costs latency. So consistency is a tradeoff almost all the time, not just during failures.

Explain CAP theorem precisely — what does it actually claim?

In a distributed system, during a network partition (some nodes can't reach others), you must choose between Consistency (every node sees the same latest data) and Availability (every request gets a response). You can't guarantee both at that moment. It says nothing about normal operation, where both are often possible.

Common misconception: "CAP theorem means a system is either CP or AP, all the time." Why is this an oversimplification?

CAP's tradeoff applies only during a partition; otherwise a well-designed system can be both consistent and available. Many systems are tunable per operation instead of locked to one label. And CAP ignores the latency-vs-consistency tradeoff that exists even without a partition (which PACELC adds).

What is a vector clock, and what problem does it solve in a system with no single global clock?

A small set of per-replica counters attached to each write, incremented by the replica that made it. Comparing two vector clocks shows whether one write happened before the other, after it, or neither (a true conflict). It orders events across machines without perfectly synced clocks.

What is a CRDT (Conflict-free Replicated Data Type), and how does it let replicas merge conflicting writes without coordination?

A data structure designed so merging any two independently updated copies always gives the same result, whatever the merge order. For example, an increment-only counter merges by summing each replica's increments. Replicas accept writes independently and merge later, with no locking or coordination during the write.

What is "read repair" in a leaderless replicated system, and how does it help move toward consistency?

When a read consults several replicas and they disagree, it finds the stale ones and writes the newer value back to them as part of that read. Over time this brings lagging replicas back in sync without a separate repair process.

What is "tunable consistency" (as in systems like Cassandra), and why might different requests in the same app choose different levels?

Each read or write sets its own consistency level (e.g. how many replicas must respond) instead of one system-wide setting. An app might use a strong, higher-quorum read for a payment balance and a fast single-replica read for a view counter, paying for the guarantee only where the data needs it.

Scenario: a shopping cart uses eventual consistency. A user adds an item on their phone, then immediately opens the site on their laptop and doesn't see it. What guarantee is missing, and what would fix this specific flow?

Read-your-writes. The laptop's read hit a replica that hadn't received the phone's write. Fix it by routing this user's reads, for a short time, to a replica known to have their writes (or to the primary) instead of any replica.

Scenario: an inventory count uses eventual consistency across regions. Two customers in different regions both see "1 in stock" and both complete checkout. What went wrong, and how would you prevent overselling?

Neither region's replica had seen the other's near-simultaneous decrement, so both saw stock that no longer existed once both writes landed. That's a classic result of concurrent writes under eventual consistency. Prevent it with a strongly consistent check for the decrement-and-reserve step (through a single source of truth), or accept it and handle oversells afterward (e.g. refund or backorder one order).

Why is "eventually consistent" not the same as "eventually correct"? What happens if writes never stop?

It only promises that if writes stopped, every replica would eventually converge to the same value. It says nothing about that value being correct for the business. And if writes never stop, replicas may never fully agree, always chasing a moving target.

What does "consistency" mean specifically in the CAP theorem, and how does it differ from the "C" in ACID?

In CAP it means linearizability: every read sees the latest write, as if there were one copy. In ACID it means a transaction only moves the database from one valid state (meeting its constraints and invariants) to another. That's about correctness rules, not how fast replicas agree.

Mechanically, why can a strongly consistent system become unavailable during a network partition?

Guaranteeing reads see the latest write usually needs a quorum of reachable, agreeing replicas (the R+W>N logic). If a partition leaves no side with a majority, the system can't safely confirm the guarantee, so it refuses requests until the partition heals rather than return stale or conflicting data.

What's the practical latency cost difference between reading from a single node versus reading with a quorum?

A single-node read is one round trip and returns when that node answers. A quorum read contacts several replicas and waits for enough to respond (often comparing answers), so it's bounded by the slowest of them. That's noticeably slower, especially when replicas are in distant regions.

What is causal consistency's relationship to "happens-before" ordering?

It's defined by it. If A happens-before B (B read a value A wrote, or they ran in sequence on the same client), every replica must show A before B. Operations with no happens-before link can appear in any order on different replicas.

Scenario: a chat app shows a reply before the message it was replying to, on one user's screen. What consistency guarantee failed?

Causal consistency. The reply depends on the original message existing, so any weaker model (like plain eventual consistency without causal tracking) can let a replica show them out of order.

Why do many real-world systems mix consistency levels rather than picking one globally?

Staleness costs very different amounts for different data. A wrong payment balance is a damaging bug; a view count off by a few for a second is invisible. Strong consistency everywhere wastes latency and availability on data that doesn't need it, so systems pay for it only where staleness causes harm.

What's a "stale read," precisely, and is it always a bug?

A read that returns a value older than the latest completed write, because a replica (or cache) hadn't applied that write yet. Whether it's a bug depends on the data: harmless for an eventually consistent view counter, serious for an account balance used to approve a withdrawal.

Follow-up: give one scenario where a stale read is completely fine, and one where it's a serious bug.

Fine: a profile's follower count a few seconds behind. Serious: a fraud check reading a stale "not flagged" status just after the account should have been flagged, letting through the very transaction it was meant to block.

What does it mean for a consistency model to provide guarantees "per session" versus "global" guarantees?

A per-session guarantee (like read-your-writes or monotonic reads) holds only from one client's view of its own operations; other clients may see a different order. A global guarantee (like linearizability) gives one agreed order for every client at once, which is harder and more expensive.

Why is achieving strict linearizability across multiple geographically distant data centers particularly costly, and what do most geo-distributed systems do instead?

It needs coordination with distant replicas on every operation, and the speed of light sets a floor on that round trip across continents, often tens to low hundreds of milliseconds per operation. Most geo-distributed systems use a weaker model for most operations (causal consistency, or strong consistency within a region plus async cross-region replication) and keep global linearizability for the few operations that truly need it.