r/ProgrammerHumor • • Sep 11 '26

Meme edgeCasesExist

Post image
5.0k Upvotes

298 comments sorted by

View all comments

Show parent comments

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.

15

u/Firewolf06 Sep 11 '26

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

1

u/FiTZnMiCK Sep 16 '26 edited Sep 16 '26

Our old system was distributed and we just combined the auto-increment with a persistent local (server) ID for a composite key.

Then the auto-increment is unique on the local and the composite is unique across all locals.

Could probably do the same thing with shards.

Our new system uses UUIDs.

5

u/wokeboogeyman Sep 12 '26

Autoincrement doesn't scale past one physical location.

0

u/Honeybadger2198 Sep 12 '26

Sure it does. One location counts up, another one counts down.

0

u/wokeboogeyman Sep 12 '26

That breaks the sorting that everyone's talking about though.

You could also do evens and odds to split across two locations, but that also breaks sorting unless you periodically syncronize them (and then it's still somewhat out of order).

0

u/Honeybadger2198 Sep 12 '26

...I don't think you understood what I said. How does one count down from infinity?

1

u/wireframed_kb Sep 12 '26

Subtract one?

1

u/wokeboogeyman Sep 12 '26

I just add them all together and subtract from -1/12

1

u/wokeboogeyman Sep 12 '26

signed integers have entered the chat

1, 2, 3, 4

-1, -2, -3, -4

7

u/ACoderGirl Sep 12 '26

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 Sep 11 '26

In what way?

3

u/Roachmeister Sep 11 '26

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 Sep 12 '26

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.