Live data from Hacker News

Nanosecond timestamp collisions are common

evanjones.ca

71–80 of 291 posts

Re: Nanosecond timestamp collisions are common

#71

Earlier quoted context omitted.

> So why aren't we doing this? isn't IPv6 is basically that? just restricted to internet addresses

It's sort of this. Although it would've been nice if the size of the IP wasn't restricted, so one day we can add an optional segment on top and connect the whole Milky Way, or something.

Fun fact: a ping from the center to the extreme of the milky way would overflow for 64 bit millisecond timestamps.

Re: Nanosecond timestamp collisions are common

#72

Earlier quoted context omitted.

Isn’t it simpler to use sequence keys then?

Yes, but only on single machines, UUID and co are intended for distributed systems. Although now I wonder if / how UUID v7 can do sequential keys on distributed systems. Mind you, on those systems "close enough" will probably be good enough, and sorting will be done by date instead of incremental ID.

You could also keep a global incremental index for that, assuming there's some authoritative server that has to be polled sequentially to get them.

Probably too much overhead when conflicting UUIDs are a few orders of magnitude less likely than the clients crashing from some random bug though.

Re: Nanosecond timestamp collisions are common

#73

Despite the resolution being nanoseconds, what is the actual precision of computer clocks? I can't imagine it is actually nanoseconds. Takes me back to teaching physics labs where I had to hound students to remember that the accuracy of their measuring device is not identical to the smallest number it displays...

For devices clocked above 1Ghz it's perfectly possible for the clock to increment every ns, although that doesn't make it accurate to that level, and multi core systems may have clocks that are not synchronised to that level.

ARMv8 guarantees that it's clock increments at at least 1Ghz, for intel and earlier ARM it's more complicated

Re: Nanosecond timestamp collisions are common

#74
post #43

Earlier quoted context omitted.

If you're going to do that then you might as well just use UUID, since you effectively reintroduce the negative aspects of that (infinitesimally miniscule chance of collisions, computation involved in the calculation, etc.)

The difference is that you can still use sequential IDs internally, while exposing hashed IDs to the outside. This protects your database from collisions under all circumstances, while in the absolute worst case, a single user might experience bugs because two external IDs collide.

If you're hashing for security reasons, I think you should still maintain a salt.

Re: Nanosecond timestamp collisions are common

#75
post #37
post #32

Earlier quoted context omitted.

v7 looks nicer since it solves v4's locality issue and you're still a gazillion times more likely to win the lottery than generate a collision.

Unfortunately, many libraries don't implement it yet.

It is still in draft and I've watched the spec changed at least once.

Re: Nanosecond timestamp collisions are common

#76
post #48

This is why you should use ids that combine both a time component and a sequence. Eg UUIDv7 has a milliseconds time component and then a field that increments for each event in the same millisecond, and then enough random bits to make collisions between ids generated on different machines astronomically unlikely. Of course there are only so many bits so you might generate too many events in the same time slice so the…

Why do you need the time component anyway? It's just eating up bits in your UUID without contributing much entropy.

When you sort them they will be ordered by generation time. This is useful in UIs, and greatly improves performance in databases (how much depends on DB and use case).

Re: Nanosecond timestamp collisions are common

#77
post #48

Earlier quoted context omitted.

Why do you need the time component anyway? It's just eating up bits in your UUID without contributing much entropy.

You need it to make database indices perform better. If you don't need that, but just need a random UUID, UUIDv4 is better.

I feel like v7 is almost a strictly better v4. Assuming you can generate a v7 (you have a time source), what are the disadvantages?

Re: Nanosecond timestamp collisions are common

#78

Timestamps should probably never be used as a "unique" id.

The problem is achieving locality in cohesion/meaning, which usually involves locality in time, which provokes using timestamps as part of the id at the very least. But it's a chain of very lazy thinking IMHO and it's like a peek at the house of cards some large systems are built like.

Would a Snowflake ID work for this? https://en.wikipedia.org/wiki/Snowflake_ID

Re: Nanosecond timestamp collisions are common

#79
post #64

Earlier quoted context omitted.

You need it to make database indices perform better. If you don't need that, but just need a random UUID, UUIDv4 is better.

But shouldn't that be a separate field then?

But then you have 2 identifiers which complicates everything.

Re: Nanosecond timestamp collisions are common

#80
post #48

This is why you should use ids that combine both a time component and a sequence. Eg UUIDv7 has a milliseconds time component and then a field that increments for each event in the same millisecond, and then enough random bits to make collisions between ids generated on different machines astronomically unlikely. Of course there are only so many bits so you might generate too many events in the same time slice so the…

Why do you need the time component anyway? It's just eating up bits in your UUID without contributing much entropy.

Iirc you dont want to spend entropy that you don't need. The advantage of using the time means you need to spend less bits on expensive randomness and thus generation is cheaper.
Post reply on HN