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 A — healthy — clock 12:00:00.000
- Node B — healthy — clock 11:59:59.900
- Two clocks. There are two copies of user:42, and node B's clock runs 100 ms behind node A's.
- A saves a name. Node A saves the name "Ana", stamped 12:00:00.000, and copies it to B.
- B corrects it. B sees "Ana" and corrects it to "Anna" 80 ms after A's edit, stamped 11:59:59.980.
- The copy arrives. The correction reaches A stamped 11:59:59.980, which looks 20 ms older than "Ana".
- The correction is lost. Last-writer-wins keeps "Ana" on both nodes, so B's correction disappears without a word.
- 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
- No two machines agree exactly on the time.
- A slow clock makes a later edit look older.
- Last-writer-wins then keeps the older edit without a word.
- Lamport and vector clocks order edits by what each node had seen.
In plain words
Every machine has its own clock, and no two agree exactly. (One machine, or one running copy of a program, in a group that works together.) 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 (A conflict rule that keeps whichever write has the newer timestamp and discards the other one.) 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
Mechanics
How it works
Two nodes, A and B, edit one document title, and (A copy of a database kept on another machine.) C keeps one value. The pictures show what each kind of stamp lets C decide: which edit came later, and which edits happened apart.
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.
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.
Order what can be ordered
Pick a case to follow belowWith this case: By wall clock, "Final" looked 80 ms older than "Draft", so last-writer-wins dropped the newer edit.
Node A, clock right Node A clock right Node B, clock 100 ms behind, NOW Node B clock 100 ms behind saw "Draft" NOW Replica C, keeps one title Replica C keeps 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 3Step 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 A10:00:00.050A's clock"Final" from B–B's clockReplica C keeps–not decided yetStep 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.
Spot same-time writes
Pick a case to follow belowWith this case: The Lamport stamps tied at 1, so a fixed rule kept "Red" and silently dropped "Blue".
Node A, clock right Node A clock right "Red" · 1 Node B, clock 100 ms behind Node B clock 100 ms behind "Blue" · 1 Replica C, keeps one title, NOW Replica C keeps one title NOW - 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 2Step 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 A1"Blue" from B1Replica C keeps–not decided yetStep 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 (The one copy that takes every write for its data. The other copies, the followers, copy from it. Kafka has one leader for each partition.) 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
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
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?
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
Apply it
Judge a design
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.
- Research paperTime, Clocks, and the Ordering of Events in a Distributed System (1978)Leslie Lamport, Communications of the ACMThe happened-before relation and logical clocks, the base of most later work.
- BookDesigning Data-Intensive ApplicationsMartin Kleppmann (O’Reilly)Chapter 8: why clocks on different machines disagree, and what goes wrong if you trust them.