Live data from Hacker News

Nanosecond timestamp collisions are common

evanjones.ca

11–20 of 291 posts

Re: Nanosecond timestamp collisions are common

#11

I was going to post about "use a UUID", but I was surprised to learn that no UUID uses both timestamp + a random component. You can either get fully random with UUID4, or have a time + MAC based UUID with UUID1. Strange, I would have thought there would exist a UUID that uses time + random to minimize collisions like described in the post.

ULID (and I think UUIDv7 draft) has millisecond timestamp + 80b randomness.

Re: Nanosecond timestamp collisions are common

#13
post #6

If you need unique nanosecond, keep track of the previously generated one and increase it if necessary. Would require global lock or atomic stuff, but should be good enough for practical uses.

One benefit of UUID is that you don't need coordination. In coordinated systems, unique IDs are a non-issue.

Is there any other benefit? That's the raison d'être - Universally.

Re: Nanosecond timestamp collisions are common

#14

If you need unique nanosecond, keep track of the previously generated one and increase it if necessary. Would require global lock or atomic stuff, but should be good enough for practical uses.

If you do that, just take some redis server and increment an integer every time you need a new number?

Re: Nanosecond timestamp collisions are common

#15
At some point doesn’t this come down to the ISA? A CPU running at 3GHz gets 3 clock cycles per nanosecond.

I bet there is a fair amount of optimization in the compiler that leads to back to back assembly calls of reading the clock register. If subsequent time.Now() calls happen within 3 clock cycles of each other, can you really fairly expect unique nanosecond precision…

Re: Nanosecond timestamp collisions are common

#16
I guess I'm old, the macOS behaviour is more in line with my expectations.

But this got me thinking, how feasible it would be to tie the various clock systems of a computer to some reference clock, like 10 MHz GPSDO? Obviously it wouldn't improve the granularity, but you could ensure that the timestamps are actually accurate. Because otherwise I doubt that random computer clock would be accurate down to 32ns even with NTP.

Re: Nanosecond timestamp collisions are common

#17

If you need unique nanosecond, keep track of the previously generated one and increase it if necessary. Would require global lock or atomic stuff, but should be good enough for practical uses.

"Logical Physical Clocks" may be of interest. The timestamps are monotonic and don't require atomic clocks like in google spanner.

HLC captures the causality relationship like logical clocks, and enables easy identification of consistent snapshots in distributed systems. Dually, HLC can be used in lieu of physical/NTP clocks since it maintains its logical clock to be always close to the NTP clock. Moreover HLC fits in to 64 bits NTP timestamp format, and is masking tolerant to NTP kinks and uncertainties.

https://cse.buffalo.edu/~demirbas/publications/hlc.pdf

Re: Nanosecond timestamp collisions are common

#19
post #14

If you need unique nanosecond, keep track of the previously generated one and increase it if necessary. Would require global lock or atomic stuff, but should be good enough for practical uses.

If you do that, just take some redis server and increment an integer every time you need a new number?

or y'know, use the thing that was designed specifically for this case: UUID's

Re: Nanosecond timestamp collisions are common

#20

At some point doesn’t this come down to the ISA? A CPU running at 3GHz gets 3 clock cycles per nanosecond. I bet there is a fair amount of optimization in the compiler that leads to back to back assembly calls of reading the clock register. If subsequent time.Now() calls happen within 3 clock cycles of each other, can you really fairly expect unique nanosecond precision…

Linux will (x86_64) use RDTSC and adjust that against a value read from the VDSO, so it can indeed happen very quickly.
Post reply on HN