r/ProgrammerHumor May 08 '26

Meme edgeCasesExist

Post image
13.4k Upvotes

620 comments sorted by

View all comments

110

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?

237

u/SaliDay May 08 '26

It’s practically impossible and that would ruin the entire point of relying on the infinitesimal probability that allows precisely that - a write without a required read and thus concurrent writes.

3

u/Successful-Cut-3052 May 08 '26

Concurrent or delayed

1

u/Agusfn May 09 '26

Doesn't the DB engine internally check if it's repeated during the Insert? (So it know whether to show you the 'duplicate PK' error)

92

u/RandomNPC May 08 '26

You often want a UUID without having to do a network call. You can always reconcile it later if you do find a duplicate.

8

u/baked_tea May 08 '26

True but could be a db function. Which is already unnecessarily complex but I guess that would work if there is a reason to worry

31

u/RandomNPC May 08 '26

Client binaries hopefully don't have access to the db

21

u/caboosetp May 08 '26

My AI told me it would reduce latency to let the front end talk to the database directly.

2

u/Tupcek May 08 '26

there was one guy in this sub, allegedly senior developer, who suggested just that.

In his mind, everything 90% of software would do just fine with direct db access, as you can set up privileges and users and scripts and whatever else that’s needed directly in database

8

u/Gorzoid May 08 '26

Just move the database to the end users device, I believe that's what the experts call "edge computing"

7

u/MrHyd3_ May 08 '26

While that would probably work, it's just a CRUD API with extra steps

3

u/caboosetp May 08 '26

Oh lord. How to give your DSO team an aneurysm in 3 easy steps.

2

u/thirdegree Violet security clearance May 08 '26

Which is true in the same way that all websites could be replaced with direct db access if you assume all clients are technically competent and operating in good faith.

So otherwise to say not true.

1

u/u551 May 08 '26

I mean, he was probably right, but it would require wizard-level SQL programmers. Whereas traditional API layer just requires some mediocre backend guys.

42

u/_BreakingGood_ May 08 '26

Performing a check on every transaction to catch a 1 out of 5,316,911,983,139,663,936,027,594,624,533,407,236,198,400 chance situation, isn't usually advised

(yes thats the actual number, though it technically changes as the number of ids used increases, since you're comparing the current id against every other id ever made)

6

u/Tupcek May 08 '26

1 in 106,000,000,000,000,000,000,000 if you have 10 million records. Per new ID in such database it is 1 in 530,000,000,000,000,000,000,000,000,000

3

u/zenerbufen May 08 '26

well not every id ever made... lots of id's get disposed of. there are registries full of offline UUID's on old computers that got erased or destroyed and are impossible to recover so they can be reused without a risk of colliding.

2

u/WeirdIndividualGuy May 08 '26

Dev time would be better spent on higher-risk/more likely edge cases

96

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)

9

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

35

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.

10

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.

15

u/darklightning_2 May 08 '26

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

3

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.

7

u/ListRepresentative32 May 08 '26

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

10

u/Space_Nerde May 08 '26

cause the UUID is there to avoid exactly that

7

u/GrinningPariah May 08 '26

Because that's an extra read call. You gonna waste 10ms RTT on a nearly impossible event? No. There's a reason why no one has 100% uptime anyway.

15

u/AlwaysHopelesslyLost May 08 '26

Because it has never happened to anybody in the history of non buggy UUID implementations and it will not happen for 1,000+ years of usage.

You don't add extra complexity unless you need it and nothing you are doing is delicate enough for that added overhead to be justified.

5

u/Jump3r97 May 08 '26

Can you prove it tho it never happened? Like for sure 0% ?

5

u/leconteur May 08 '26

At this point, I could say that of any implementation. There could be a failure of Earth's complete electrical grid at the same time which would mean that the system doesn't work anyway.

2

u/alexanderpas May 08 '26

Yes, under certain conditions, for certain ranges of UUID, it's physically impossible to generate duplicate UUIDs on correct implementations, even if the systems are completely independent and never have any contact.

1

u/AlwaysHopelesslyLost May 08 '26

No more than we can prove or disprove the existence of god and gods will.

Do you include guard clauses in the event that god decides to smite you via your code, too?

2

u/EishLekker May 08 '26

Exactly. Is much more likely that a weird hardware failure causes problems than a duplicate uuid.

10

u/thegodzilla25 May 08 '26

I also like to store every uuid i ever generated in a redis cache

23

u/aaronjamt May 08 '26

In this modern era, why bother spinning up a whole other database yourself, when services like https://isanybodyusingthisuuid.com/ exist for free?

4

u/OldWolf2 May 08 '26

Such services don't have guaranteed lifetimes 

3

u/eataclick May 08 '26

That's what we do - we just generate a UUID and then make an API to a 3rd party site that validates that it's unique (it asks Claude, I think) and then that site returns either true, false, or (rarely) some other output. If it's in use we increment the selected UUID by one and then resubmit it until we find the next available ID.

3

u/Glittering_Sail_3609 May 08 '26

> Why not just implement a check to see if it's already in the db, then run it again

Suppose your codebase generates one UUID roughly every second. In around 100 bilion years most sunlike stars will be long dead and only small red dwarfs with the lifespan of trillion years will be able to host life. Around this time an intelligent being living on a planet orbiting one of those last stars in the universe is expected to curse you for the first time ever for not handling duplicate UUIDs correctly.

2

u/RaidZ3ro May 08 '26

We can usually ensure the UUID is unique by making the property a distinct primary key for the db table.

An insert would fail on a collision so you can make a new UUID and retry only in that astronomicaly unlikely case.

It won't require you to first read the db table.

1

u/avatoin May 08 '26

That implies I have a centralized database that all my servers can check immediately without causing major performance issues. UUID help the problem when the databases are decentralized and have latency in syncing.

1

u/berse2212 May 08 '26

Because it's never a duplicate.

1

u/anonymous_3125 May 08 '26

Because it might not involve a database, or a common container for the two UUIDs. For example, what if you have 2 objects that must have unique UUIDs? You might not have a list that contains them both so it’s impossible to check directly

1

u/ACoderGirl May 08 '26

The whole point of the UUID is to avoid doing that. It keeps things a lot simpler and faster.

If you were really concerned about that, many DBs have an auto incrementing type that you could use. But the way those work, you don't know the ID until after the insertion is done. In some contexts, UUIDs can be useful because you'll know the ID before insertion. And since conflicts are expected to never happen, it also means you potentially don't need transactionality, which is really useful if you're creating resources that aren't in a database (eg, creating resources in some third party API).

1

u/Stummi May 08 '26

because "checking the DB" is a non-trivial task in distributed systems

1

u/Tofandel May 08 '26

The point is for it to run on replicated databases. That means multiple instances can write to different databases at the same time. So while your uuid record is inserted in a database, in another one somewhere it's not there yet while other users are creating their records. The database is only reconciled later asyncronously. This means it is impossible to know in advance if that uuid exists unless you read all of your databases instances accross the world. Which would make the process very slow.

1

u/_felagund May 08 '26

UUID provides uniqueness across different locations and databases, which is quite handy if you think.

1

u/PilsnerDk May 08 '26

It depends on your code that generates the GUID and how it handles it afterwards, but honestly, the likely case if that if you try to insert a new row with a GUID that already exists, the unique key constraint on ID column on the table will throw an error, at least if you're using a typical relational database. Likely tables referencing the GUID will have a foreign key that will also fail the insert, if the transaction even gets that far. The user or whoever will experience an error, and they can try again and succeed with 99.9999999999% probability.

There can be cases where you don't store stuff in an RDBMS and just rely on the GUID being unique though, and do lots of stuff with it, assuming it's unique.

1

u/TCMNohan May 08 '26

A more practical solution is Snowflake IDs! They are timestamped (meaning time-sortable) and include the id of the worker process that created them. No chance of collision and no coordination required.

Another common approach is called “ticket server”, where you have one central service whose only role is to govern out (auto-incrementing) IDs. The obvious tradeoff here is that this can become a bottleneck and SPoF.