Distributed systems / Consistency models (strong vs eventual)

Consistency models (strong vs eventual)

When data lives on several machines, what does a read return just after a write? Seven steps follow one stock count through three replicas, the strong way and the eventual way.

Intermediate7 steps~7 min
Writer — changes the stock from 5 to 4
Replica — one machine’s copy of the value
Reader — asks any replica for the value
// THE SHORT VERSION

In 30 seconds

When data is copied to several machines, a consistency model is the promise about what a read returns after a write.

Strong consistency: once a write is confirmed, every read sees it, as if there were one copy. The cost is slower writes, and refused requests when the copies cannot reach each other.

Eventual consistency: the write is confirmed at once and the copies catch up shortly after. It is fast and stays available, but a read may briefly return an old value. Guarantees such as read-your-writes sit in between.

Consistency models (strong vs eventual), step by step

STEPS01 / 07

Strong here means linearizability, drawn simply: real systems usually confirm a write once a majority of replicas has it, not all of them. Timings are illustrative.

STEP 01 / 07 One value, three copies
THE MATH
stock = 5 on R1, R2 and R3
  1. Step 1: One value, three copies. A shop keeps the number of items in stock on three machines, so that it survives a failure and can serve many readers. Each copy is called a replica. While nothing changes, all three hold 5 and it does not matter which one answers a read.
  2. Step 2: A write reaches one copy. Someone buys an item, and the writer tells R1 to set the stock to 4. R1 then sends the change to the others, which takes time: 60 ms to R2 and 100 ms to R3. Until it arrives, the copies disagree. A consistency model is the rule for what readers may see during and after that gap.
  3. Step 3: Strong: wait for the copies. With strong consistency the system behaves as if there were a single copy. The write is confirmed only when the replicas have the new value, here after 105 ms, and the value becomes visible everywhere at that moment. Any read that starts after the OK returns 4, whichever replica answers.
  4. Step 4: Eventual: answer first. With eventual consistency the write is confirmed as soon as R1 has it, after 5 ms, and the other replicas catch up in the background. For 95 ms a read may return 4 or the old 5, depending on which replica answers. The promise is only that, if the writes stop, all copies end up the same.
  5. Step 5: Read-your-writes. Stale reads are most confusing for the person who just made the change: they buy the item, reload, and the page still says 5. Read-your-writes is a guarantee in between. Your own reads are sent to a replica that already has your write; other people may still see the old value for a moment.
  6. Step 6: The price, when a link fails. The difference becomes a hard choice when replicas cannot reach each other. R3 has missed the write and cannot know it. A strongly consistent system makes R3 refuse to answer; an eventually consistent one lets it answer 5. One gives up availability, the other freshness: the trade-off named by the CAP theorem.
  7. Step 7: Which apps need which. The choice depends on what a wrong answer costs. Selling the last seat twice or losing money is not acceptable, so those operations need strong consistency and accept the slower writes. A like count that is a few seconds old harms nobody, so it can be eventual and fast. Many systems use both, per kind of data.
VALUES SO FAR
stock5value
copies3replicas
write····on R1
copying····lag
strong····slower
eventual····faster
window····stale
no link····trade-off

Spotted a mistake on this page?