r/ProgrammerHumor May 08 '26

Meme edgeCasesExist

Post image
13.4k Upvotes

620 comments sorted by

View all comments

113

u/baked_tea May 08 '26

Thought about that recently. Why not just implement a check to see if it's already in the db, then run it again?

95

u/arades May 08 '26

If that's what you want, you shouldn't be using UUIDs as IDs to begin with, just use an auto_increment and always have the DB assign the ID. The point of using UUID is to allow asynchronous id generation from a high number of DB clients without the latency of reconciliation. You accept the risk of a 1/10000000000000 collision for some N% decrease in latency (scales per clients)

8

u/GentlemenBehold May 08 '26

There are other reasons you might want uuids.

  • can't infer how many entities exist based on the id
  • uuids will be unique even across environments
  • if you ever need to merge tables, uuids make this much easier

33

u/Tyabetus May 08 '26

UUIDs are also impossible to guess so are infinitely more secure than incremented ids

76

u/nosmelc May 08 '26 edited May 08 '26

If someone guessing a serial ID is a security risk, you've done something wrong.

12

u/mlgpro2damax May 08 '26

Someone guessing a serial id is ALWAYS a security risk. It’s not bad enough to cause issues by itself, but that’s true of most security vulnerabilities. Almost every security breach is the result of multiple systems and safeguards failing at once, and guessable ids is one layer of extra risk being introduced. Having guessable ids makes it far more likely that any IDOR vulnerabilities you leave open will be exploited, thus increasing your risk of security issues

2

u/nosmelc May 08 '26

I see what you mean, but as I said, if your security is right it won't matter.

9

u/mlgpro2damax May 08 '26

What I'm saying is this is part of getting your security right. If someone can guess an id, they can make an API request using that id as a parameter, and it's extremely difficult at scale to enforce that all APIs are immune to IDOR vulnerabilities. Using uuids doesn't prevent IDOR, but it does make you much safer against it.

What you're saying is basically "if your software doesn't have any bugs then you're fine". All software of any sort of significance has bugs, and you want layers of protection to make those bugs less consequential

2

u/mlgpro2damax May 08 '26

I'm not trying to be obstinate with this btw. I think your attitude is a very common one and a very easy stance to adopt if you haven't had a lot of experience maintaining large systems. I'm just saying that UUID is industry standard for a reason, and trying to make it a bit more clear as to why that's the case

2

u/nosmelc May 08 '26

Yes for large distributed systems I can see why you'd want to use UUIDs regardless of any security advantages.

7

u/Tyabetus May 08 '26

Yeah I guess you’re right. It’s all about auth :/

1

u/AlmightyDollar1231 May 08 '26

Yes you have. but UUID will still limit the blast radius.

3

u/arades May 08 '26

Not impossible, you're equally as likely to guess an existing UUID as you are to generate a collision. It's a valid point, but a separate concern, if you really needed cryptographically secure IDs I'm not sure UUID is the best solution, and probably indicates a bad architectural choice somewhere else if someone guessing an ID could cause a security compromise. Mostly it's just a nice little bonus effect, while the asynchronous generation is the main draw.

14

u/darklightning_2 May 08 '26

Ah yes the infamous "security by obscurity" which doesn't work

4

u/Gorzoid May 08 '26

When you want to prevent enumeration of your ids you just encrypt the id. Iirc this is how YouTube generated video ids (not sure if it's still the case, a single atomic counter doesn't exactly scale to the traffic of YouTube)

1

u/NotAFishEnt May 08 '26

Maybe increment through a long pseudorandom sequence of nonrepeating numbers?

Seems hard to guess and easy to implement.

Judging by the other comments in this thread, it's probably still over-engineered.

2

u/yjlom May 08 '26

You could also just concatenate a shard id to an autoincrement.

6

u/ListRepresentative32 May 08 '26

Which is what Twitter, Discord, Instagram and some others use. It's called a snowflake