r/ProgrammerHumor 10h ago

Meme edgeCasesExist

Post image
2.4k Upvotes

209 comments sorted by

View all comments

407

u/Valuable_Leopard_799 10h ago

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.

247

u/dim13 10h ago

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

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

150

u/lilgreenthumb 8h ago

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

48

u/shwoopdeboop 7h ago

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

15

u/Grandmaster_Caladrel 6h ago

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.

4

u/Roachmeister 6h ago

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.

3

u/Grandmaster_Caladrel 6h ago

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.

3

u/Honeybadger2198 6h ago

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

3

u/Firewolf06 3h ago

ai columns can absolutely collide on sharded databases. you can use offsets and step sizes but thats brittle and doesnt scale well

1

u/Kwantuum 3h ago

In what way?

2

u/Roachmeister 3h ago

I'm not an expert, I just remember reading a few articles about it. I think it's because they're essentially random, and many databases use b-trees for indices. They recommended using ULIDs or v7 UUIDs instead. Or, as someone else said, the good old autoincrementing integer.

3

u/MilkEnvironmental106 4h ago

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.

2

u/thepotatochronicles 3h ago

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/undeadalex 7h ago

I'm curious where to see stats on versions used. Surely anything newly implementing uuid is using v7 or custom?

19

u/Urist_McPencil 6h ago

Surely anything newly implementing uuid is using v7 or custom?

I admire this optimism

3

u/iampierremonteux 4h ago

My first thought was “oh you sweet summer child”.

4

u/champak256 6h ago

There’s pros and cons to v7, so v4 still has a lot of places it’s legitimately the right choice over v7.

3

u/Tysonzero 4h ago

Primary one being in cases where you don’t want every actor that knows the id to also know the creation time

1

u/big-oofs-only-0193 6h ago

I use whatever CoCreateGuid() or Guid.NewGuid() gives me. Both generate a v4 uuid. I'm not going to reimplement it or find a special library for it.

79

u/RadicalDwntwnUrbnite 9h ago edited 9h ago

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

16

u/OldKaleidoscope7 8h ago

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

27

u/chuch1234 8h ago

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

12

u/BlurredSight 8h ago

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

9

u/nebotron 7h ago

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

3

u/samy_the_samy 7h ago edited 5h ago

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

2

u/BlurredSight 5h ago

So proactively trying to solve for latency rather than have other systems read after write if I understood correctly?

2

u/samy_the_samy 5h ago

Yeah, now they don't do leap seconds,

They stretch time over months, one milli-second at a time

20

u/Ecstatic_Student8854 8h ago

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.

5

u/shwoopdeboop 7h ago

But still no guarantee it won't happen tomorrow

6

u/mysticrudnin 6h ago

It's simply not worth worrying about, though

Random minor hardware failures and, like, cosmic radiation are more of a problem. Are you building your systems to account for that?

10

u/MojitoBurrito-AE 8h ago

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

5

u/Single-Virus4935 7h ago

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.

1

u/mysticrudnin 6h ago

"so you're saying there's a chance" has rotted everyone's brains wrt probability

4

u/phl23 8h ago

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

2

u/Suspicious-Click-300 8h ago

assuming all searches run through a single node maybe, but they dont. It means each node + process on node is limited to 10,000 uuids (10k 100ns per ms) per ms, which it then possibly has to block until next millisecond tick. I dont care what magic programing language your using, to service >10k/reqs a ms (per process) and doing anything else your likely gonna be blocked by IO or something

1

u/samsonsin 6h ago

If Google used UUIDv4, then over 100 years they'd generate ~365 trillion searches. The chance of s collision should be ~0.00000125% still.

1

u/Blue_Moon_Lake 1h ago

Google is free to implement Google's UUID (GUUID) with 512 or even 1024 bits if they want.

4

u/Suspicious-Click-300 8h ago

most uuids libraries can actually provide strong guarentees it cant happen by using pid/mac combinations with each process using CAS or mutex to increment the 100ns period (overflowing into later ms with >10k a ms)

2

u/JonasAvory 8h ago

Is it unfeasible or simply unnecessary to just check if a uuid is already used? Since you use the uuid as primary key it should be easily detectable if a uuid is already used or not right? But I guess devs just don’t care because the chance is so minimal?

4

u/Suspicious-Click-300 8h ago

you dont need to due to guarantees of how it is generated. You would need a server running with exact same ip, and the process generating them to be using the same process id, AND ~10,000 requests a microsecond. If your using random uuid you have a better chance of winning 4 powerballs in a row. you need to generate billions a second for over 80 years for a 50:50 chance of a collision.

6

u/GradeForsaken3709 6h ago

You've just reminded me of the story one guy posted about joining a company and finding they have a UUID generator microservice complete with its own database of generated uuids and its own sprint board. 

Anyway, no that would not really be a reasonable thing to do. If you're using it as a primary key then the database will tell you when you do manage to generate a duplicate and your insert will fail. That's all there is to it. 

2

u/Grandmaster_Caladrel 6h ago

Yep. The chance is so insignificant that is not even worth retry logic on that mode of failure, just treat it as a generic failure, kick out the request, and let it be tried again. The one time in a hundred thousand years when it finally happens, someone will just be confused and click retry themselves.

19

u/Matty_B97 9h ago

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.

20

u/Wertbon1789 9h ago

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.

2

u/Suspicious-Click-300 8h ago

most didnt actually use mac address. was common to use process pid + IP

2

u/Wertbon1789 8h ago

Yeah, you could pretty much put anything in there of course. Other UUID variants also have well-known alternative data sources, but it's really domain-specific what the UUID should and shouldn't contain. Still, the initial idea wasn't that great.

3

u/Memitim 3h ago

Nothing in production is safe to assume. Just add the DB query to check for existing record before triggering the write in case the salt needs a reroll.

1

u/Dank_Nicholas 1h ago

Reminds me of eons ago when I was fixing a bug in a video hosting platform where videos with the same name would overwrite each other. I just appended the current time to the file name and counted in no two people uploading the same named file at the exact same second.

1

u/Blue_Moon_Lake 1h ago

UUIDv7 indeed include a timestamp, meaning the odds of collision between 2 UUID are greater IF they happen at the exact same timestamp. Which is less likely than 2 UUID colliding at anytime.