Stage 3 · Distributed data · Hard · 20 min

Time, clocks and ordering

Why machines cannot agree on the time, how a skewed clock loses writes, and how Lamport and vector clocks order events instead.

What each interview level is expected to know

Learn first: Replication and sharding

After this lesson you can:

  • Explain why machines disagree on the time, and how that can lose a write.
  • Order events with Lamport clocks.
  • Use vector clocks to spot writes that happened at the same time.

Start here

See it in 60 seconds

A correction lost to a slow clock

An edit made after seeing another can carry an older timestamp when its machine's clock runs slow, and last-writer-wins then throws it away.

Scene 1 of 5 · Two clocks

There are two copies of user:42, and node B's clock runs 100 ms behind node A's.

Node ANode Aclock 12:00:00.000Node BNode Bclock 11:59:59.900
  • Node A — healthy — clock 12:00:00.000
  • Node B — healthy — clock 11:59:59.900
  1. Two clocks. There are two copies of user:42, and node B's clock runs 100 ms behind node A's.
  2. A saves a name. Node A saves the name "Ana", stamped 12:00:00.000, and copies it to B.
  3. B corrects it. B sees "Ana" and corrects it to "Anna" 80 ms after A's edit, stamped 11:59:59.980.
  4. The copy arrives. The correction reaches A stamped 11:59:59.980, which looks 20 ms older than "Ana".
  5. The correction is lost. Last-writer-wins keeps "Ana" on both nodes, so B's correction disappears without a word.
  6. Now you try. Pick how nodes order edits, and replay the same two edits. For example: Both nodes keep "Anna": the correction survives. B's counter is one higher than anything it had seen, so its edit sorts after "Ana".

Scene 1 of 5: Two clocks. There are two copies of user:42, and node B's clock runs 100 ms behind node A's.

Key points

  1. No two machines agree exactly on the time.
  2. A slow clock makes a later edit look older.
  3. Last-writer-wins then keeps the older edit without a word.
  4. Lamport and vector clocks order edits by what each node had seen.

Go deeper in the Clocks & ordering lab →

In plain words

Every machine has its own clock, and no two agree exactly. B's clock might run 100 ms behind A's. If the newest timestamp wins, an edit made on B after seeing A's edit can still look older, and it is thrown away. Lamport clocks and vector clocks count events instead of reading the time, so an edit made after seeing another always sorts after it.

A Lamport clock works in three steps. Each machine keeps a counter and adds 1 for every event. Every message carries the sender's counter. On receiving one, a machine sets its counter to one more than the larger of the two. So whatever it does next sorts after the message. A vector clock keeps one counter per machine, so it can also tell when two edits happened without either seeing the other.

The problem it solves

Machines cannot agree on the exact time, so timestamps from different machines cannot reliably say which event came first. Systems that keep the write with the highest timestamp can lose data when a clock is off.

Why it matters

Two replicas of user:42 take edits 50 ms apart. Which edit is newer? If the answer depends on each machine's clock, a correction can quietly disappear.

A worked example

Node A saves the name Ana at 12:00:00.000. Node B's clock runs 100 ms behind. B sees Ana and changes it to Anna 80 ms later, so its own clock stamps the change 12:00:00.080 − 0.100 s = 11:59:59.980. That stamp is lower than Ana's, so keeps Ana and the correction is lost.

Think of it like…

Think of two friends who edit a shared shopping list by text. Each stamps a message with the time on their own watch. One watch runs two minutes slow, so a later correction looks older and is ignored.

Where this analogy stops working

A wrong clock that hides a later change is the useful part. But a friend can read both messages and see which one is the correction. A database compares only the stamps. Counters fix the order of edits that saw each other. For two edits made at the same time, the app must still merge them.

Try it

See it working

Live simulationOpen the full lab →

Lab · Distributed data

Clocks & ordering

Three replicas of user:42 take edits on random nodes and copy them to each other. Skew node B's clock and see last-writer-wins drop newer edits; then see Lamport and vector clocks order the edits by what each node had seen.

Try it

Lab time 3.0 s
Node A, holds v10Node Aholds v10clock 3.000 sNode B, holds v10Node Bholds v10clock 3.000 sNode C, holds v10Node Cholds v10clock 3.000 s
  • Node A — holds v10 — healthy — clock 3.000 s
  • Node B — holds v10 — healthy — clock 3.000 s
  • Node C — holds v10 — healthy — clock 3.000 s
  • An edit being copied, with its stamp
Edits
10
Lost silently
3
Problem:
Newer edit lost
0
Kept side by side
0
Merged
0

Numbers add up:10 edits: 3 lost silently (0 of them newer edits), the rest kept or replaced by a later editThe lab checks this sum as it runs, so a counting mistake would show here.

Controls

Ordering
How nodes order edits

Each write is stamped with its node's wall clock; the highest stamp wins.

Traffic and clocks
0 ms
2

Nodes also reply to some edits they receive, so the Edits counter grows faster than this.

Recent edits

EditNodeStampWhat happened
v10B@2158kept
v9A@1978lost: same-time edit dropped
v8A@1855kept
v7A@1705kept
v6C@1484kept
v5A@945kept
v4B@828lost: same-time edit dropped
v3C@816lost: same-time edit dropped

Stamp: a wall-clock time in ms, a Lamport counter (#), or a vector of what each node had seen ([A, B, C]).

What just happened

  1. 2.2sError: B keeps v10 and drops v9 without a word; neither saw the other.
  2. 1.0sError: C keeps v5 and drops v3 without a word; neither saw the other.
  3. 1.0sError: A keeps v5 and drops v4 without a word; neither saw the other.
  4. 0.0sInfo: Last writer wins. Node B's clock runs 0 ms behind.
What this model simplifies
  • Time is slowed so you can watch each copy travel. Network delays here are 60 to 300 ms.
  • Only node B's clock is off, by a fixed amount. Real clocks also drift and jump when they resync.
  • A node sometimes replies to an edit it receives with its own edit 20 to 80 ms later, like a person reacting to a change.
  • Every copy arrives. Real stores also repair nodes that missed copies, with read repair and anti-entropy.
  • There is one key. A real store keeps a clock per key, and vector clocks there are pruned as they grow.

Mechanics

How it works

Two nodes, A and B, edit one document title, and C keeps one value. The pictures show what each kind of stamp lets C decide: which edit came later, and which edits happened apart.

  1. Stamp each write

    Every write gets a stamp that says when it was made. A wall clock stamps it with the time on that machine. A Lamport clock stamps it with one counter. A vector clock stamps it with one counter for each node.

  2. Copy it to the replicas

    Each write travels to the other nodes with its stamp. A node that receives it compares that stamp with the stamp on the value it holds. Under last-writer-wins, the node keeps the higher wall-clock time and drops the other write.

  3. Order what can be ordered

    Pick a case to follow below

    With this case: By wall clock, "Final" looked 80 ms older than "Draft", so last-writer-wins dropped the newer edit.

    Node A, clock rightNode Aclock rightNode B, clock 100 ms behind, NOWNode Bclock 100 ms behindsaw "Draft"NOWReplica C, keeps one titleReplica Ckeeps one title
    • Node A — clock right — healthy
    • Node B — clock 100 ms behind — healthy — NOW — saw "Draft"
    • Replica C — keeps one title — healthy
    • Copy with its stamp
    Step 1 of 3

    Step 1 of 3 · Node B

    Done: A writes "Draft", and B receives it

    What happens
    A stamps the edit with its clock: 10:00:00.050.
    Why
    B now knows about "Draft", so whatever B writes next comes after it.
    What changes
    B shows "Draft" to its user.
    Next
    20 ms later, B writes "Final"
    "Draft" from A
    10:00:00.050
    A's clock
    "Final" from B
    –
    B's clock
    Replica C keeps
    –
    not decided yet

    Step 1 of 3: Node B. A writes "Draft", and B receives it. A stamps the edit with its clock: 10:00:00.050.

    A Lamport clock is a counter on each node. A node adds 1 for each write, and raises its counter to any higher number it receives. So a write made after seeing another carries a higher number, whatever the wall clocks say.

    In the picture, B's clock runs 100 ms behind A's. B sees A's edit "Draft", then writes "Final" 20 ms later. By wall clock, "Final" looks older, so last-writer-wins drops it with no error.

    With a Lamport clock, "Draft" is stamped 1. B raises its counter to 1 when it sees it, so "Final" is stamped 2 and wins. The counter follows what each node had seen.

  4. Spot same-time writes

    Pick a case to follow below

    With this case: The Lamport stamps tied at 1, so a fixed rule kept "Red" and silently dropped "Blue".

    Node A, clock rightNode Aclock right"Red" · 1Node B, clock 100 ms behindNode Bclock 100 ms behind"Blue" · 1Replica C, keeps one title, NOWReplica Ckeeps one titleNOW
    • Node A — clock right — healthy — "Red" · 1
    • Node B — clock 100 ms behind — healthy — "Blue" · 1
    • Replica C — keeps one title — healthy — NOW
    • Copy with its stamp
    Step 1 of 2

    Step 1 of 2 · Replica C

    Done: A writes "Red" and B writes "Blue", at the same moment

    What happens
    Neither has seen the other's edit, so each counter goes from 0 to 1.
    Why
    Neither edit came after the other. They are concurrent.
    What changes
    Both copies head to replica C.
    Next
    Replica C sees a tie, and a fixed rule picks "Red"
    "Red" from A
    1
    "Blue" from B
    1
    Replica C keeps
    –
    not decided yet

    Step 1 of 2: Replica C. A writes "Red" and B writes "Blue", at the same moment. Neither has seen the other's edit, so each counter goes from 0 to 1.

    What these pictures simplify. These pictures use two nodes, one replica and stated stamps, with B's clock 100 ms behind. Real clocks drift by different amounts, and are corrected from time to time. The clocks lab above runs many writes.

    A vector clock keeps one counter per node, such as [2,1,0]. If every counter in one stamp is at least as high as in the other, that write came later. If each stamp is higher in one place, neither write saw the other. Only a vector clock shows this, so the store can keep both for the app to merge.

    In the picture, A writes "Red" and B writes "Blue", and neither has seen the other. Lamport stamps tie at 1, so a fixed rule keeps one edit and drops the other without a word.

    Vector stamps show the truth: [1,0] is higher in one place and [0,1] in the other. Neither edit came first, so replica C keeps both for the app to merge.

Use it when

  • Replicas that accept writes in several places, such as a leaderless key-value store.
  • Shopping carts and documents where losing an edit is worse than showing two versions.
  • Logs and traces where events must sort in the order they caused each other.

Skip it when

  • Data with one per key: the leader already orders every write.
  • Values where losing a same-time write does not matter, such as a last-seen time.

Check yourself

Why can't two servers' clock times safely order two writes a few milliseconds apart?

Judgement

Trade-offs and how it fails

What you give up

Safety
A vector clock shows when two writes clash, so the store can keep both. Last-writer-wins keeps one and drops the other without telling anyone.
Complexity
Vector clocks grow with the number of nodes, and the app must merge siblings.

Failure modes

  • A correction lost to a clock 100 ms behind: the newer edit carries an older stamp.
  • Same-time edits on two nodes: last-writer-wins keeps one and drops the other without a word.
  • Siblings never merged: the app keeps showing two versions of the same field.

In the real world

  • Amazon's Dynamo paper used vector clocks and returned siblings for the shopping cart to merge.
  • Cassandra resolves conflicts with last-writer-wins on timestamps, so its nodes need closely synced clocks.

Check yourself

Two copies accept writes to the same key at the same moment. Last-write-wins by timestamp keeps one. What is the cost?

Interview

What interviewers look for

  • When a design says the newest write wins, ask whose clock decides what newest means.
  • Name the trade: last-writer-wins is simple but can lose writes; vector clocks keep them but need merges.

Say it out loud: B's clock runs 100 ms behind. A writes at 12:00:00.000; B sees it and writes 30 ms after A's write. Which write wins under last-writer-wins?

What can vector clocks tell you that Lamport clocks cannot?

Expected depth by interview level

Levels describe the depth interviewers probe, not years of experience. Choose a level on the learning path to highlight it.

JuniorBeyond this level
Not usually expected.
MidUseful
Why two machines' clocks disagree, and why last-writer-wins can lose data.
SeniorCore
Lamport against vector clocks, siblings and merges, and when wall clocks are safe.
StaffExpected
Choosing conflict handling per data type: last-writer-wins, CRDTs or app merges.

Practice

Apply it

Three harder questions

The first two use the idea on a new case. The last asks you to judge a design.

Apply it

Node A's Lamport counter is at 5. It receives a message stamped 9, then makes a write. What stamp does the write get?

Apply it

Write X carries the vector clock [2,1,0]. Write Y carries [1,2,0]. What is true?

Judge a design

A shared note store in two regions keeps the write with the later wall-clock time. Users report edits that vanish. Which change keeps both clashing edits for the app to merge?

Next: consensus, where nodes agree on one order of events by voting.

Go deeper

Sources and further reading

First-hand sources to check this page against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.