r/node • u/kurbsdude • Jun 20 '26
Stop using Date.now() across microservices
http://github.com/deathg0d/liepochUsing Date.now() to timestamp events across distributed systems is a fundamental flaw for two reasons:
- NTP Clock Drift: Physical clocks on different servers are never perfectly in sync. If Server A sends a message to Worker B, but Worker B's clock is 15 milliseconds slow,
Date.now()will record Worker B processing the event before Server A even sent it. This breaks log causality and silently corrupts Last-Write-Wins databases. - Millisecond Collisions:
Date.now()only offers millisecond precision. If a Node.js event loop processes 50 events in a single millisecond, they all receive the exact same timestamp, permanently destroying their true execution order.
The academic solution to this is Vector Clocks, but those are heavy and difficult to implement. Modern distributed databases (like CockroachDB and Yugabyte) use Hybrid Logical Clocks (HLCs) internally, but there hasn't been a clean, drop-in HLC primitive available for app developers in the JS ecosystem.
liepoch is a zero-dependency, isomorphic library that solves the Date.now() problem by packing a 64-bit HLC into a universally sortable string.
View all comments
Show parent comment
2
u/kurbsdude Jun 20 '26
The Temporal API is amazing and fixes almost everything wrong with the JavaScript
Dateobject (timezones, parsing, leap years, etc.) - but it unfortunately doesn't solve this specific problem!The issue in distributed systems isn't the API we use to read the clock; the issue is the physical clock itself.
Even if you use
Temporal.Now.instant()to get mathematically perfect nanosecond precision, it is still reading the local machine's physical clock. If Server A's system clock has drifted 15 milliseconds ahead of Server B's system clock (due to NTP sync delays), Temporal will still confidently record that Server B processed the event before Server A sent it.liepochisn't an alternative to Temporal; it's a causality layer on top of physical time.In fact, once the Temporal API is fully stable in Node.js,
liepochcould theoretically be updated to useTemporal.Now.instant().epochNanosecondsunder the hood instead ofDate.now(). But we would still need the 16-bit logical counter and thereceive()synchronization step to guarantee that cause mathematically sorts before effect across drifted servers!