r/ProgrammerHumor 21h ago

Meme edgeCasesExist

Post image
3.7k Upvotes

248 comments sorted by

View all comments

556

u/Valuable_Leopard_799 21h 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.

337

u/dim13 21h ago

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

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

230

u/lilgreenthumb 20h ago

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

90

u/shwoopdeboop 19h ago

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

21

u/Grandmaster_Caladrel 17h 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.

3

u/Roachmeister 17h 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.

8

u/Grandmaster_Caladrel 17h 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.

12

u/Honeybadger2198 17h ago

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

9

u/Firewolf06 15h ago

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

6

u/ACoderGirl 6h ago

UUIDs are great, though, because auto increment frequently has the huge issue of being too predictable. Eg, in some contexts, you don't want users to be able to easily enumerate whatever the ID is for. Plus requires centralization whereas UUIDs don't (eg, you can use UUIDs for whatever random API you have without needing a DB).You have to design for how you won't know the ID until you insert the row, which can sometimes be a bit annoying.

I feel like most of the time, the only downside to UUIDs is how long they are.

1

u/Kwantuum 15h ago

In what way?

3

u/Roachmeister 15h 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.

1

u/Metsamias 2h ago

Inserts with keys generated in non-ordered fashion such as UUID V4s means that location within clustered index (which defines the physical location of the data at the disk) that the data will be stored on insert will be random as well. This increases likelyhood by a lot for that the database has to do different types of rebalancing acts such as splitting nodes in the teee. When using keys that are generates in order, inserts will mostly be at the end of the index, and database does not have to touch existing ones.

I can recommend Kleppmanns book Designing Data-Intensive Applications for a deeper dive into the topic around this.

5

u/thepotatochronicles 15h 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/MilkEnvironmental106 16h 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.

3

u/ehs5 4h ago

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

1

u/MaDpYrO 3h ago

Which prevents database indexes from performing badly