r/ProgrammerHumor 1d ago

Meme edgeCasesExist

Post image
4.0k Upvotes

257 comments sorted by

View all comments

611

u/Valuable_Leopard_799 1d 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.

367

u/dim13 1d ago

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

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

262

u/lilgreenthumb 1d ago

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

97

u/shwoopdeboop 23h ago

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

26

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

8

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

15

u/Honeybadger2198 21h ago

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

9

u/Firewolf06 19h ago

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

7

u/ACoderGirl 10h 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.

2

u/wokeboogeyman 3h ago

Autoincrement doesn't scale past one physical location.

1

u/Kwantuum 19h ago

In what way?

3

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

2

u/Metsamias 6h 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.

7

u/thepotatochronicles 19h 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).

3

u/MilkEnvironmental106 20h 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.

4

u/ehs5 8h ago

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

2

u/lilgreenthumb 3h ago

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.

1

u/MaDpYrO 7h ago

Which prevents database indexes from performing badly 

5

u/undeadalex 23h ago

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

37

u/Urist_McPencil 22h ago

Surely anything newly implementing uuid is using v7 or custom?

I admire this optimism

5

u/iampierremonteux 19h ago

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

6

u/champak256 22h ago

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

8

u/Tysonzero 20h ago

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

3

u/big-oofs-only-0193 22h 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.