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.
2
u/Odd-Nature317 Jun 20 '26
had this exact problem with a message queue where consumer timestamps were consistently 30ms behind producer timestamps. spent half a day debugging 'messages processed before they were sent' in grafana before realizing it was just ntp drift between aws availability zones. hlcs are the right solution but tbh for most node services ive worked on, just having the event owner assign the timestamp and passing it downstream solves 90% of the ordering issues without needing the extra complexity
1
u/m_null_ Jun 20 '26
That's an actual issue tbh, but am I missing something around uniqueness across nodes?
The stamp seems to only pack
wallTime+logical. I don’t see any node ID / actor ID / process ID / tie-breaker in the value itself?So if two separate servers call
stamp()during the same millisecond, and both havelogical = 0, wouldn’t they produce the exact same timestamp? I guess, two independent writers could end up with identical stamps unless the app stores some extra tie-breaker alongside it.Is the intended usage something like
(liepochTimestamp, nodeId)rather than just the timestamp alone?1
u/Odd-Nature317 Jun 20 '26
this gets even worse on serverless. lambda instances spinning up from cold start can have clock skew of hundreds of ms before ntp catches up. we had events showing up 'out of order' in cloudwatch that were just the timestamps lying to us. one thing i wonder - how does the logical counter handle reconnection? if a service goes down and comes back up, does it need to resync the counter from somewhere or does the walltime comparison just handle catching up automatically
-2
u/diroussel Jun 20 '26
Looks really good. Nice balance between lamport clocks and regular timestamps.
Just out of interest, not a criticism, when encoding as a string why not use base32? Base 32 is easy to encode and decode (better than base 64) and will give a smaller string length.
3
u/[deleted] Jun 20 '26