Core Concepts

Consistency Models

Follow a comment through several copies of a database and choose what a reader should be able to see.

Free~14 min

A consistency model defines what an application can promise about the results of reads and writes. It tells a reader which writes can be visible, whether a later read can return an older version, and which updates must appear in order.

One thread, different guarantees

In these examples, the primary accepts writes. A replica is a copy that receives those writes and may also serve reads. Data Replication explains how those changes reach other servers.

You refresh a comment thread and see a new comment. The next refresh lands on a replica that is behind, and the comment disappears. Each guarantee below gives the application a different rule for what it can show the reader.

Eventual consistency

You open the thread on a replica that has yet to receive a comment. The replica can return the older thread. If writes stop and replication can proceed, the copies eventually converge. That is eventual consistency. It suits a thread reader who can wait to see a new comment, and it leaves the length of that wait open.

Read-your-writes

After you post a comment and receive confirmation, refreshing should still show your comment. Read-your-writes gives that guarantee within your session, the series of requests the app makes on your behalf. Other readers may still see the older thread.

The write response can include a version marker. When you refresh, the app sends that marker as the earliest version it can use. A replica that has yet to reach that version can wait or the service can route the read to a copy that already has it.

Monotonic reads

You see another person's comment at version 12. The next refresh goes to a slower replica at version 10 and the comment disappears. Monotonic reads prevents a session from going backward through versions it has already seen.

Save version 12 after the first read. The next read waits or goes to a copy that can serve at least that version. This requires a version the service can compare across copies of the thread. Pinning requests to one steadily advancing replica can also help, but a failover or routing change can move the session to a copy that has fallen behind.

These guarantees can be used independently. A product can always show your own comment while a later refresh drops someone else's comment that you already saw. A product could also return the same old thread on every refresh while failing to show the comment you just posted. Tracking both versions lets the app preserve both promises.

Session guarantees for weakly consistent replicated data

Causal consistency

Someone reads your comment and replies to it. A third reader opens a replica that has received the reply first. The reply has no context if the original comment has yet to arrive.

Causal consistency preserves the order of operations that depend on each other. To show that reply, a replica must also be able to show the comment it depends on. Independent comments can appear in different orders to different readers. A service can track the original comment as a dependency and hold the reply until the dependency is visible.

Linearizability

You receive confirmation for a write, and then anyone reads that same value. The new read must see your write or a later one. If a read starts while the write is still in progress, either value can be valid if the rest of the operation history permits it.

With linearizability, every operation appears to take effect at one instant between its request and response. This makes reads and writes behave like operations on one shared copy even when requests overlap. It can matter for checking whether the last seat is still available after a booking has completed.

A design can direct reads to a leader whose protocol establishes that it still has authority and knows the committed state. A group of replicas can also serve a linearizable read under a protocol that establishes which history is current. During a split, a node that cannot establish those facts must wait or refuse. Herlihy and Wing on linearizability

Choose the guarantee the operation needs

What the reader needsGuaranteeWhat the system tracks
Copies eventually agree when updates stopEventual consistencyUpdates or state needed to reconcile the copies
My saved comment appears after a refreshRead-your-writesThe session's most recent write
The thread never jumps back after I have read itMonotonic readsThe session's most recent observed version
The original comment is visible when its reply is visibleCausal consistencyDependencies between writes
Reads respect the order of writes that have already completedLinearizabilityWhich node can serve and which state it can safely return

A design can offer more than one session guarantee. For example, a thread can require both read-your-writes and monotonic reads while other clients still receive local copies that lag.

Replica lag

Replica lag is the delay between a write at the primary and that write becoming visible at a replica. Different copies can take different amounts of time to catch up.

Replication can fall further behind during a burst or a slow network link. If the thread must be at most five seconds stale, specify how you measure the delay and what happens when no nearby replica meets the limit. You can route elsewhere, wait, or return an error according to the product's needs.

What consistency looks like under partition

If two sides lose contact, ask whether the local node can establish the state the operation requires. It can serve a local copy when the operation permits staleness. If the session needs a version the node has yet to apply, it may have to wait or fail the read. CAP Theorem covers the tradeoff when some live nodes cannot serve a valid result.

Interview habit

  1. Name the operation and the user-visible surprise you want to prevent.
  2. State which copy serves it and what state the read can return.
  3. Explain what happens when that copy lags or the client reconnects elsewhere.
  4. Add a freshness bound if the prompt requires one, and say what happens when the system misses it.

Common mix-ups

  • Treating read-your-writes and monotonic reads as one promise. Test each against a refresh that goes to a different copy.
  • Treating eventual convergence as a deadline. Add a measured bound when the product needs one.
  • Confusing the C in ACID with consistency across reads. The C in ACID (atomicity, consistency, isolation, and durability) concerns rules such as uniqueness and valid references. Read consistency concerns which writes a client can observe.

See also

  • CAP Theorem - what happens to each operation when copies lose contact.
  • Data Replication - how copies apply changes and how lag arises.
  • Latency vs Throughput - waiting for a required version can add work and time to a read.
  • Availability - how to reason about delayed and failed requests.
  • Database Sharding - how queries and transactions cross data partitions.
  • Idempotency - keeping retries from applying the same write twice.
  • ACID Transactions - why a set of writes needs a shared commit.
  • Quorum (forthcoming) - what agreements among copies can establish.

Used in

  • Online Chess - the owner applies moves in order. A stale copy cannot accept a move that crosses the current game sequence.
  • Group Chat with Threads - a sender sees their own message and a reconnecting reader can catch up from the last seen sequence.
  • Coding Interview Platform - editing clients converge after they exchange updates they missed during a disconnect.
  • Prompt Playground - keep a submitted run visible to the author when loading run history.

Done reading? Mark it so it sticks in your dashboard.

Discussion