u/kurbsdude

▲ 0 r/node+1 crossposts

Stop using Date.now() across microservices

Using Date.now() to timestamp events across distributed systems is a fundamental flaw for two reasons:

  1. 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.
  2. 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.

github.com
u/kurbsdude — 17 days ago