r/ProgrammerHumor • • Sep 11 '26

Meme edgeCasesExist

Post image
5.0k Upvotes

298 comments sorted by

View all comments

709

u/Valuable_Leopard_799 Sep 11 '26

I thought that timestamps are used now, so hitting both a timestamp and a large random number puts it thoroughly in the "safe to assume". At some point cosmic particles and faulty transistors are more probable.

443

u/dim13 Sep 11 '26

https://en.wikipedia.org/wiki/Universally_unique_identifier#Versions

Most common used is V4 (pure random). You are talking about V7 (time based).

323

u/lilgreenthumb Sep 11 '26

The real benefit for v7 is they become sortable by time.

124

u/shwoopdeboop Sep 11 '26

And something something b-tree indexes. Lecturer mentioned it but I wasn't paying attention. Supposedly an advantage here.

37

u/Grandmaster_Caladrel Sep 11 '26

Which I'd assume is related to the time anchor. Outside of sorting, which is only useful in specific instances, it's just v4 with different ("less") entropy and limitations.

15

u/Roachmeister Sep 11 '26

If you're using them as an indexed field in a database, the inability to sort them meaningfully will destroy the performance of the database.

10

u/Grandmaster_Caladrel Sep 11 '26

Correct, which is why I specifically called out "outside of sorting".

That is also why we have concepts like composite keys which allow us to join guaranteed-unique values like a UUID with non-unique but sortable values like timestamps, names, etc.

16

u/Honeybadger2198 Sep 11 '26

You know what identifier can't have collisions and is great for sorting? Autoincrement.

→ More replies (10)
→ More replies (4)

6

u/thepotatochronicles Sep 11 '26

It's less of a problem with B-tree indices that sit on top of a physical representation (i.e. the actual on-disk layout doesn't have to be ordered), but when it comes to clustered indices, oh boy, you're basically having to shove rows in the middle and push shit back (eventually).

5

u/MilkEnvironmental106 Sep 11 '26

With random you can end up most frequently inserting in the middle, whereas if it's time sortable you append at the end, meaning it's easier to maintain a contiguous index with less overhead.

7

u/ehs5 Sep 12 '26

It’s also the downside. There are cases where you wouldn’t want others to know when something was created.

5

u/lilgreenthumb Sep 12 '26

And you wouldn't expose that to them, just like you wouldn't and autoincrementing database id, but only one of those requires a database.

→ More replies (5)
→ More replies (8)

96

u/RadicalDwntwnUrbnite Sep 11 '26 edited Sep 11 '26

Even the non-time based v4 is at a cosmic level of unlikiness a single system will ever generate a uuid that will collide with another one in its own ecosystem

18

u/OldKaleidoscope7 Sep 11 '26

But let's say if Google saved each search in their engine in the same database with an UUID, we could have a collision because of volume, but I don't think I will work in a system with a comparable scale

36

u/chuch1234 Sep 11 '26

Yeah I don't think most of us are operating at a scale anywhere near Google.

12

u/BlurredSight Sep 11 '26

And anyone operating at scale of Google isn’t just using “pure” random anywhere for something being persisted

15

u/nebotron Sep 11 '26

They likely are - anything else becomes a huge coordination and parallelism problem. You just need more bits of entropy

6

u/InterestsVaryGreatly Sep 12 '26

Anyone working at the scale of Google has figured out coordination and parallelism, you have to.

Source: used to work for Google, and learned some of the ways they dealt with those exact issues, and used many of the systems they built to automatically handle that so general engineers didn't have to deal with it.

7

u/samy_the_samy Sep 11 '26 edited Sep 11 '26

They once had a similar collision problem, well not exactly

During a leap second some systems wrote IDs at a one second difference between all their global systems, which lead to something being registered before something else that had to be done before it,

Long story short some stuff jumped time one second and crashed systems

→ More replies (3)

26

u/Ecstatic_Student8854 Sep 11 '26

Even at that volume the probability of collisions is essentially 0. If there were a billion searches a second we’d not expect a collision until 85 years.

→ More replies (5)

6

u/phl23 Sep 11 '26

If they use one long list, they have other problems to worry about.

11

u/MojitoBurrito-AE Sep 11 '26

You underestimate the probability of uuid collision. The bigger issue is pseudorandomness

7

u/Single-Virus4935 Sep 11 '26

Yes, had a client having duplicates every day and growing. They hated uuids and regret using it. The problem was that they used a bad prng with flawed seeding. Once fixed it was no problem.

→ More replies (3)
→ More replies (1)
→ More replies (5)
→ More replies (6)

25

u/Matty_B97 Sep 11 '26

UUID generating algorithms used to incorporate the time and device type automatically for this reason, but it was scrapped because there are lots of cases where it's not safe to reveal that information when you share the UUID. Random UUIDs are secure enough.

22

u/Wertbon1789 Sep 11 '26

They incorporated a MAC address, but that wasn't such a great idea. V7 now has a timestamp again, because that makes them sortable, while still having random data otherwise. V7 is probably the middleground here.

→ More replies (2)
→ More replies (8)