Scrivio Interview library
ConceptFoundations6 min read · updated 13 November 2026

Logical clocks order events by cause and effect rather than by physical time. Lamport clocks and vector clocks are the two classic designs.

How do Lamport clocks and vector clocks work, and what can each of them tell you about two events?

A Lamport clock is one counter per process: it increments on every event, travels with messages, and on receipt becomes max(local, received) + 1, so if a happened before b then L(a) < L(b), but a smaller timestamp does not prove that a happened first, and it cannot detect concurrency. A vector clock keeps one counter per process and merges element-wise maximums, so comparing two vectors tells us exactly whether one event happened before the other or whether they were concurrent, which is what conflict detection needs. The price is size that grows with the number of participants.

Why this question is asked

Physical clocks cannot order events across machines reliably, so distributed systems order them by cause and effect instead. Logical clocks are how they do it, and the two classic designs appear everywhere: Lamport clocks in total ordering and mutual exclusion, vector clocks and their relatives in Dynamo-style stores and CRDTs. Interviewers ask this question to check that we know the rules and, more importantly, what each clock can and cannot tell us about two events. In this answer, we will recap the happened-before relation, run a Lamport clock by hand, see its limit, then run a vector clock and use it to detect concurrent updates.

1A Quick Recap: Happened-Before

Leslie Lamport defined an order on events that does not need physical time. Event a happened before event b, written a -> b, if a and b are in the same process and a came first, or if a is the sending of a message and b is its receipt, or if there is a chain of such steps from a to b. If neither a -> b nor b -> a, the events are concurrent: neither could have influenced the other.

This is exactly the order that matters for correctness. If a user's second update was made after seeing the first, it happened after it. If two users updated a record without seeing each other's change, those updates are concurrent, and something must merge or choose between them.

2Lamport Clocks Step by Step

Each process keeps one integer counter. Before every event, including sending a message, it increments the counter and stamps the event. Every message carries the sender's counter. On receipt, the receiver sets its counter to max(local, received) + 1 and stamps the receive event with that value.

Java
final class LamportClock {
private long counter;
synchronized long tick() { return ++counter; } // local or send event
synchronized long onReceive(long received) {
counter = Math.max(counter, received) + 1; // jump past the sender
return counter;
}
}
// P1: tick() -> 1 (a), tick() -> 2 (send m with 2)
// P2: tick() -> 1 (c), onReceive(2) -> 3 (d), tick() -> 4 (send n with 4)
// P1: onReceive(4) -> 5 (e)
Step throughLamport timestamps across two processes
P1P2
a: L=1
c: L=1
send m with L=2
d: receive m, max(1, 2) + 1 = 3
send n with L=4
e: receive n, max(2, 4) + 1 = 5

6 messages in order. Play them, or step through them one at a time.

c (L=1) and a (L=1) are concurrent; the numbers alone cannot say so.

The rule guarantees one direction: if a -> b, then L(a) < L(b). The receive always lands above the send, and each process counts upward, so any causal chain has increasing timestamps. If we break ties by process id, comparing (counter, processId) pairs, we get a total order of all events that is consistent with causality, which is what algorithms such as total-order broadcast or Lamport's mutual exclusion need.

3What a Lamport Timestamp Cannot Tell Us

The converse does not hold. L(a) < L(b) does not mean a happened before b. In the example, event c on P2 has L=1, and P1's send of m has L=2, yet c and the send are concurrent: neither process knew about the other's event. Looking only at the numbers 1 and 2, we cannot tell whether they are causally related or merely ordered by chance.

So a Lamport clock gives us an order that never contradicts causality, but it cannot detect concurrency. For a key-value store that must notice two conflicting writes, that is not enough: it would happily pick the one with the larger counter and lose the other.

4Vector Clocks Step by Step

A vector clock gives every process its own entry. With three processes, each clock is a vector like [P1:2, P2:0, P3:1]. Before every event, a process increments its own entry. Messages carry the whole vector. On receipt, the receiver takes the element-wise maximum of its vector and the received one, then increments its own entry.

Comparing two vectors gives the full answer. Vector a is before b if every entry of a is less than or equal to the matching entry of b and at least one is strictly smaller. If neither vector is before the other, the events are concurrent.

Java
// two replicas update the same key without seeing each other
V1 = [A:1, B:0] // write x=5 on replica A
V2 = [A:1, B:1] // replica B saw V1, then wrote x=6 -> V1 is before V2: keep x=6
V3 = [A:2, B:0] // replica A wrote x=7 without seeing V2
compare(V2, V3) // A: 1 < 2, B: 1 > 0 -> neither dominates: CONCURRENT
// the store keeps both values (siblings) or merges them; it must not silently pick one
What each clock can tell us about two events
QuestionLamport clockVector clock
If a -> b, is a's stamp smaller?yesyes (dominated)
Does a smaller stamp mean a -> b?noyes
Can it detect concurrent events?noyes: neither dominates
Size per timestampone integerone entry per participant
Typical usetotal order, mutual exclusionconflict detection, CRDTs

Notice what this buys us. V1 before V2 means the write of 6 was made with knowledge of 5, so 6 simply replaces it. V2 and V3 are concurrent, so the store knows there is a real conflict and can keep both versions for the application to merge, which is how Dynamo-style stores handle it.

5Keeping Vector Clocks Small

A vector clock has one entry per participant. If every client that writes gets an entry, the vectors grow without limit in a system with millions of users. Practical systems avoid this by using version vectors with one entry per replica or server, not per client, so the size is bounded by the number of replicas, typically three to a few dozen.

Per-replica entries introduce a subtle problem: two clients writing through the same replica concurrently could look sequential. Dotted version vectors fix this by tagging each value with the exact event, the dot, that created it, which Riak adopted. Some systems also prune old entries, accepting a small risk of false conflicts.

6Follow-up questions

Why can a Lamport clock not detect concurrency, when it sees every message?

Because it collapses all history into one number. Two unrelated processes can count up to the same or nearby values independently, and the number does not record which processes' events it includes. A vector records a count per process, so it keeps exactly that information.

Are hybrid logical clocks a kind of Lamport clock?

Yes, an HLC is a Lamport clock whose counter is kept close to physical time: its timestamp is a physical time plus a logical counter. Like Lamport clocks, it respects causality but does not detect concurrency.

Where do vector-clock-like structures appear in practice?

In Dynamo-style stores such as Riak, which keep sibling values for concurrent writes, in CRDT libraries, and in sync protocols for offline-first apps, where knowing whether two edits were concurrent decides whether to merge them.

7Conclusion

In this answer, we recapped the happened-before relation and then ran both classic logical clocks. A Lamport clock is one counter per process that increments on every event and jumps past received values, so causally later events always get larger stamps, and tie-breaking by process id gives a total order, but it cannot show concurrency. A vector clock keeps a counter per process, merges by element-wise maximum, and tells us exactly whether one event preceded another or whether they were concurrent, at the cost of size, which version vectors and dotted version vectors keep bounded.

What to say in the interviewAbout 90 seconds aloud

Open with

Both order events by cause and effect rather than physical time. A Lamport clock is a single counter per process: I increment it on every event, attach it to messages I send, and when I receive a message I set my counter to the maximum of mine and the received value, plus one.

Then, if they want more

That guarantees that if a happened before b, a's timestamp is smaller. But the reverse isn't true: a smaller timestamp doesn't prove causality, so a Lamport clock can't tell me whether two events were concurrent. If I break ties by process id, I get a total order that's consistent with causality, which is useful for things like total-order broadcast.

A vector clock keeps one counter per process. Each process increments its own entry on every event, sends the whole vector, and on receipt takes the element-wise maximum and then increments its own entry.

Now a happened before b exactly when a's vector is less than or equal to b's in every entry and smaller in at least one, and if neither dominates, the events were concurrent. That's what conflict detection needs: the store can keep both versions instead of silently dropping one.

The cost is that vectors grow with the number of participants, so real systems use version vectors per replica rather than per client, or dotted version vectors, to keep them bounded.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How do Lamport clocks and vector clocks work, and what can each of them tell you about...": https://scrivio-interview-library.netlify.app/architecture/distributed-systems/503237dede/