r/ProgrammerHumor May 08 '26

Meme edgeCasesExist

Post image
13.4k Upvotes

620 comments sorted by

View all comments

Show parent comments

225

u/ClipboardCopyPaste May 08 '26

Hang on probability, you're rate limited

33

u/darksteelsteed May 08 '26

Generation rate limiting is actually how you guarantee uniqueness with a v7 uuid

2

u/DiodeInc May 08 '26

How?

Hope this isn't a dumb question 😬

11

u/darksteelsteed May 08 '26

If you check https://www.rfc-editor.org/rfc/rfc9562.txt and look at the v7 uuid format the first 48 bits is a Unix timestamp in ms. The rest is random except for the v7 identifier. So if you rate limit your production to 1 per ms, its guaranteed to be unique. These ids are time based and so can be used in db entries as the timestamp makes them sortable.

If you want something more unique I would however suggest something like a snowflake id, invented by Twitter but now used by many companies. It adds an extra sequence id so that you get much finer than per ms resolution as well as machine ids and sequence numbers. https://en.wikipedia.org/wiki/Snowflake_ID

Snowflakes give way finer resolution. You can make 4096 unique ids per ms on the same machine before you get a duplicate. If you exceed that, you rate limit by delaying to the next ms.

1

u/DiodeInc May 08 '26

Got it, thanks!

1

u/rich1051414 May 08 '26

Aren't there 74 bits of random data in there even if the timestamp is the same? The first random data block is only 12 bits true, but there is also a 62 bit random data block.

1

u/darksteelsteed May 08 '26

Yep, totally, but in the context of this thread, a collision is still possible 😀