LESSON 05 · Causality

Resolve concurrent versions

Independent writers can update the same key without one obvious winner.

SYSTEM VIEW

The storage path

Diagram view
Alice’s clock—
Bob’s clock—
Items kept—

Watch two shoppers change one cart from the same version, see the clocks catch the conflict, then replay it under last-write-wins.

  1. A cart was written through Replica X, so its vector clock is {X:1}: one counter per replica, counting the writes that replica coordinated. Alice reads the cart through X and Bob reads it through Y. Both get the same version, {X:1}.

    Vector clock
    one counter per replica
    Both read
    {X:1}

    What changed

    • Cart at {X:1} on X and Y
    • Alice reads {X:1}
    • Bob reads {X:1}
  2. Alice adds a book and writes the cart back through X, sending the {X:1} she read with it. X coordinates the write, so it adds one to its own counter: Alice’s version is {X:2}.

    Read
    {X:1}
    Wrote
    {X:2}

    What changed

    • Book added through X
    • X’s counter 1 → 2
  3. At nearly the same moment, Bob adds a lamp to the {X:1} cart he read and writes it through Y. Y adds one to its own counter, so Bob’s version is {X:1, Y:1}. Neither replica has seen the other’s write.

    Read
    {X:1}
    Wrote
    {X:1, Y:1}

    What changed

    • Lamp added through Y
    • Y’s counter 0 → 1
  4. X and Y swap versions and compare counter by counter: {X:2} is ahead on X, {X:1, Y:1} on Y. Neither includes the other, so both replicas keep both as concurrent siblings. Had Bob read {X:2} first, his {X:2, Y:1} would replace it.

    Ahead
    X on Alice’s · Y on Bob’s
    Had Bob read {X:2}
    {X:2, Y:1} replaces it

    What changed

    • X holds both versions
    • Y holds both versions
    • 2 siblings
  5. Alice reads next and gets both carts. She merges them, book and lamp, and writes the result through X as {X:3, Y:1}, which includes both. Amazon’s Dynamo paper describes this merge; a plain union can bring back a removed item.

    Merged
    {X:3, Y:1}: book + lamp
    Costs
    metadata, tombstones, retries

    What changed

    • Alice gets both carts
    • Merged cart at {X:3, Y:1}
  6. Replay the two writes under last-write-wins, stamped 12:00:01 for the book and 12:00:02 for the lamp. X tells Alice ok, then Y’s lamp arrives and its higher stamp replaces the book. A vector clock detects the conflict; the business rule decides it.

    Last-write-wins
    drops an accepted write
    Compare-and-set
    rejects the stale edit
    A cart, a balance
    need different policies

    What changed

    • Writes carry timestamps
    • Alice told ok
    • Alice’s book

Space plays · ← → step · Home resets

THE IDEA

How it works

Two clients can read one version, change it differently, and write nearly together. Last-write-wins keeps the highest timestamp; it is simple but can discard an accepted update, especially when clocks disagree. Compare-and-set, which writes only if the version is still the one you read, rejects a stale edit. Another design keeps siblings, meaning both concurrent values, for the application to reconcile. A preference, a cart, and a balance require different policies.

A vector clock detects which case you have. It holds one counter per replica, counting the writes that replica coordinated. Take replicas X and Y and a cart written through X, giving clock {X:1}. Alice reads it, adds a book through X, and produces {X:2}. Bob read the same {X:1}, adds a lamp through Y, and produces {X:1, Y:1}. Compare counter by counter: the first is ahead on X and the second is ahead on Y, so neither includes the other. They are concurrent siblings. Had Bob read {X:2} first, his write would be {X:2, Y:1}, at least as high everywhere, so it could safely replace its ancestor.

The next reader receives both carts, merges them into one with the book and the lamp, and writes back through X as {X:3, Y:1}. Amazon’s Dynamo paper describes this style of cart merge. Metadata size, tombstones, retries, and client logic add cost, and a plain union can bring back an item someone removed. A vector clock detects conflict; it cannot decide the business outcome.

DECISION POINT

Two offline devices edit an account balance. Should the store silently retain the newest wall-clock value?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Concurrent writes can both be accepted without a single correct timestamp winner.
  • Causal metadata detects conflicts; domain rules resolve them.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 2
ApplyQUESTION 01

Version P has clock {X:3, Y:1} and version Q has clock {X:2, Y:2}. What should a replica conclude?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 2 checkpoints answered correctly to complete this lesson.