The Multi-Master Problem
Two database instances in US and India, both accepting writes. How do you handle conflicts when the same key is written on two continents at the same time? The answer reveals the fundamental tradeoff in distributed systems.
The Problem in One Example
Client A → US node: writes name = "hat" at t=101
Client B → India node: writes name = "sat" at t=100Both nodes accepted the write. Both will try to send their write to the other. What is the final value of name? There is no obvious answer — the two writes happened at the same time on different continents.
How Logs Actually Sync
Each node keeps a Write-Ahead Log (WAL). Replication works by shipping these log entries to the other node over the network.
The Clock Problem
US and India each keep their own physical clock (hardware crystal, corrected periodically by NTP). These clocks drift — they never agree exactly, typically off by single-digit to tens of milliseconds across regions. If you order writes purely by "what does my clock say right now," you can end up with a write that caused another write getting stamped with a higher timestamp than the write it caused — simply because the two clocks disagree about what time it is. That breaks the one rule ordering actually needs: a cause must always get a smaller number than its effect.
Hybrid Logical Clocks (HLC) — The Goal
HLC exists to guarantee exactly that one thing: if event A caused event B, A's timestamp is always smaller than B's — no matter how the physical clocks drift. It does this by never trusting a raw clock reading alone. Every node remembers the highest timestamp it has ever seen — its own clock, or one it just received in a message — and refuses to hand out anything smaller than that.
Every HLC timestamp has two parts: (time, counter, node_id).
- time — does the heavy lifting. Normally it just follows the real clock forward.
- counter — the patch for moments the real clock can't supply a new, bigger number: two events landing in the same millisecond, or a node's own clock being behind a timestamp it just received.
The rule, plainly: never hand out a number smaller than the biggest one you've seen. If your clock has moved past that number, use the clock and reset the counter to 0. If it hasn't, bump the counter by 1 instead.
Walking Through It
US clock reads 105ms. India's clock reads 101ms (running 4ms slow).
1. US writes something → stamped (105, 0, US). Ships to India.
2. India receives it. India's own clock still says 101 — smaller than 105.
India is not allowed to timestamp anything below 105 anymore,
because it just learned about something numbered 105.
→ India adopts 105 as its new floor: (105, 0, IN)
3. India does another local write right after. Its real clock is
*still* stuck at 101 (hasn't ticked forward past 105 yet).
Since the clock can't offer a bigger number, India bumps the counter:
→ (105, 1, IN)
Every new event is still strictly bigger than the last — even though
India's actual clock never caught up in real time.The same trick handles the tie case from the original example: if US and India both write at the exact same millisecond (101ms), time ties, so counter ties too (both 0) — the tie is broken deterministically by node_id.
When the Counter Actually Climbs Past 1
In the walkthrough above the counter only ever hit 0 or 1 — easy to miss why it exists. Stretch the same story: India's clock is slow and creeps forward one millisecond at a time (101 → 101 → 101 → 102...), while India keeps doing local writes.
| # | Event | India's real clock | HLC assigned | Why |
|---|---|---|---|---|
| 1 | India local write | 101 | (101, 0, IN) | clock moved past old floor (0) → use it, counter resets |
| 2 | India receives US's (105, 0, US) | 101 | (105, 0, IN) | 105 > 101, so adopt 105 as new floor, counter resets |
| 3 | India local write | 101 (still slow) | (105, 1, IN) | clock (101) can't beat floor (105) → counter bumps |
| 4 | India local write | 101 (still slow) | (105, 2, IN) | clock still hasn't caught up → counter bumps again |
| 5 | India local write | 102 (still slow) | (105, 3, IN) | 102 still < 105 → counter keeps climbing |
| 6 | India local write | 106 (finally caught up) | (106, 0, IN) | clock (106) finally beats floor (105) → use it, counter resets to 0 |
Every row is strictly bigger than the last: (101,0) < (105,0) < (105,1) < (105,2) < (105,3) < (106,0). India's physical clock was stuck at 101 for three straight writes, but the counter kept manufacturing distinct, increasing numbers so nothing collided or went backward. Once the real clock finally overtakes the floor, the counter drops back to 0 because time itself is doing the job again.
HLC only solves ordering — it gives every write a deterministic timestamp you can compare across nodes. It does not decide whether a conflicting write is allowed to happen in the first place, or how the system converges once one has. That's a separate, higher-level decision: whether nodes agree before committing (see Synchronous consensus) or commit locally and reconcile after (see Async replication). HLC timestamps are used by both.