Scrivio Interview library
CodingIntermediate6 min read · updated 13 November 2026

A hybrid logical clock (HLC) combines physical time with a logical counter, so timestamps stay close to wall-clock time while still respecting causality between nodes. Several distributed databases use one.

Implement an HLC in Java with now() for local and send events and update(remote) for receive events, and explain how it behaves when the local physical clock is behind.

An HLC keeps two values: l, the largest physical time seen so far, and c, a logical counter; timestamps compare by l, then c. now() takes the physical time pt and either moves l up to pt and resets c, or keeps l and increments c; update(remote) sets l to the maximum of l, remote.l and pt and chooses c by which of them won. When the local clock is behind, l stays at the larger time it has seen and only c grows, so causality still holds, and once the physical clock catches up, timestamps follow physical time again.

Why this question is asked

Hybrid logical clocks are the practical middle ground between wall clocks, which are readable but can disagree, and Lamport clocks, which respect causality but drift far from real time. Databases such as CockroachDB and YugabyteDB use them to stamp transactions. Interviewers ask us to implement one because the update rule has a few cases that are easy to get wrong, and because the interesting behaviour is what happens when one node's clock is behind. In this answer, we will recap the idea, write the clock in Java, trace it through a node with a slow clock, and make it safe for threads and for bad clocks.

1A Quick Recap: Why Combine Two Clocks

A Lamport clock gives every event a counter that increases along every chain of cause and effect, but the counter has nothing to do with real time, so we cannot ask for 'all events after 10:05'. A wall clock gives real time but can be behind or ahead on any node, so a reply can carry an earlier timestamp than the request that caused it.

An HLC takes a physical timestamp and adds a small counter to it. The physical part, l, is the largest physical time this node has ever seen, either from its own clock or from a message. The counter, c, breaks ties and keeps timestamps increasing when the physical part cannot move. The result stays within the clock offset of real time, yet respects causality like a Lamport clock.

2Implementing the Clock in Java

The timestamp is a pair compared by l first and c second, which a record with a Comparable implementation expresses neatly. The clock takes a time source as a parameter so tests can control physical time.

Java
public record Hlc(long l, int c) implements Comparable<Hlc> {
public int compareTo(Hlc o) {
int byL = Long.compare(l, o.l);
return byL != 0 ? byL : Integer.compare(c, o.c);
}
}
public final class HybridClock {
private final LongSupplier physical; // e.g. System::currentTimeMillis
private final long maxOffsetMs; // reject remote times this far ahead
private long l;
private int c;
public HybridClock(LongSupplier physical, long maxOffsetMs) {
this.physical = physical; this.maxOffsetMs = maxOffsetMs;
}
public synchronized Hlc now() { // local event or send
long pt = physical.getAsLong();
if (pt > l) { l = pt; c = 0; } else { c = c + 1; }
return new Hlc(l, c);
}
public synchronized Hlc update(Hlc remote) { // receive event
long pt = physical.getAsLong();
if (remote.l() - pt > maxOffsetMs) {
throw new IllegalStateException("remote clock " + (remote.l() - pt) + " ms ahead");
}
long newL = Math.max(Math.max(l, remote.l()), pt);
if (newL == l && newL == remote.l()) c = Math.max(c, remote.c()) + 1;
else if (newL == l) c = c + 1;
else if (newL == remote.l()) c = remote.c() + 1;
else c = 0; // physical time won
l = newL;
return new Hlc(l, c);
}
}

Notice how update() chooses c. If l stays the larger value and the remote has the same l, we must exceed both counters. If only our l won, we exceed our own counter; if only the remote's l won, we exceed the remote's counter; and if our physical clock is ahead of both, the counter resets to 0, because the new physical time alone is enough to order this event after everything we have seen.

3Tracing a Node Whose Clock Is Behind

Let node B's clock be 50 ms behind node A's. A sends a message stamped (1000, 0). B receives it when its own physical clock reads 950.

Java
// B's physical clock reads 950; remote from A is (1000, 0)
Hlc r = clockB.update(new Hlc(1000, 0)); // newL = max(l=940, 1000, 950) = 1000 -> (1000, 1)
Hlc e1 = clockB.now(); // pt=951 < l=1000 -> (1000, 2)
Hlc e2 = clockB.now(); // pt=960 < l=1000 -> (1000, 3)
// ... B's physical clock passes 1000
Hlc e3 = clockB.now(); // pt=1003 > l=1000 -> (1003, 0)
Step throughHLC on a node whose clock is 50 ms behind
Node ANode B
message stamped (1000, 0)
pt=950: l stays 1000, c=1 -> (1000, 1)
local event, pt=951 -> (1000, 2)
local event, pt=960 -> (1000, 3)
pt=1003 passes l -> (1003, 0)
reply stamped (1003, 0), after A's message

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

The counter carries order while the physical clock catches up.

While B's physical clock is behind, l stays at 1000, the larger time B has seen, and only c grows. Every event B produces is therefore stamped after A's message, so causality holds even though B's clock alone would have put them before it. As soon as B's physical clock passes 1000, the next event takes the physical time and resets c, and B's timestamps track real time again.

4Thread Safety and Bad Clocks

Both methods read and write l and c together, so they must be atomic. The simplest way is synchronized, as above, which is fast enough for most services. For very hot paths we can pack l and c into one long, for example 48 bits of milliseconds and 16 bits of counter, and update it with compare-and-set in a loop on an AtomicLong, so no lock is held.

The maximum offset check protects the cluster from one bad clock. Without it, a node whose clock is an hour ahead would send timestamps an hour in the future, every receiver would adopt that l, and the whole cluster's timestamps would jump forward an hour and stay detached from real time. Rejecting remote timestamps more than maxOffsetMs ahead of the local clock, for example 500 ms, contains the damage, and the node that sees such messages should alert or shut itself down rather than silently continue.

5Follow-up questions

How large can the logical counter grow, and what limits it?

c grows only while l is ahead of the local physical clock, and it resets to 0 as soon as physical time passes l. Since l can be ahead of real time by at most about the maximum clock offset, c can only grow for roughly that long, so it is bounded by the number of events in that window. With a 16-bit field and a millisecond clock, that is ample in practice, but code should check for overflow and wait or fail rather than wrap.

Why not use System.nanoTime() as the physical source?

Because nanoTime has no relation between machines, and the whole point of the l component is to be comparable across nodes and close to real time. We use the wall clock and rely on the HLC rules to repair its jumps and skew.

Does an HLC detect concurrent events like a vector clock?

No. Like a Lamport clock, it guarantees that causally later events get larger timestamps, but two concurrent events simply get some order. Systems that use HLCs detect conflicts by other means, such as uncertainty intervals or write intents.

6Conclusion

In this answer, we built a hybrid logical clock in Java: a pair (l, c) compared by l then c, where now() moves l up to physical time or increments c, and update(remote) takes the maximum of local l, remote l and physical time and picks c according to which won. Tracing a node with a slow clock showed l holding at the larger time seen while c grows, preserving causality until physical time catches up. We made it thread-safe with synchronized or a packed AtomicLong, and protected the cluster with a maximum offset check.

What to say in the interviewAbout 90 seconds aloud

Open with

An HLC keeps two pieces of state: l, the largest physical time I've seen, in milliseconds, and c, a logical counter. Timestamps compare by l first and then c.

Then, if they want more

For now, used for local and send events, I read the physical time pt. If pt is greater than l, I set l to pt and reset c to zero; otherwise I keep l and increment c. For update, on receiving a remote timestamp, I compute newL as the maximum of my l, the remote l and pt.

If newL equals both my l and the remote l, c becomes the max of the two counters plus one; if it equals only my l, I increment my c; if it equals only the remote l, c is the remote c plus one; and if physical time won, c resets to zero. Then l becomes newL.

When my local clock is behind, l stays at the larger time I've seen and only c grows, so everything I stamp still sorts after what caused it; once my physical clock passes l, timestamps follow physical time again. I'd make both methods synchronized, or pack l and c into one long and update it with compare-and-set.

And I'd reject remote timestamps that are more than a maximum offset ahead of my clock, so one node with a bad clock can't drag every node's timestamps into the future.

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 "Implement an HLC in Java with now() for local and send events and update(remote) for...": https://scrivio-interview-library.netlify.app/architecture/distributed-systems/f07cf9fb91/