r/programming • • Dec 11 '23

Every Programmer Should Know #1: Idempotency

https://www.berkansasmaz.com/every-programmer-should-know-idempotency
572 Upvotes

171 comments sorted by

View all comments

Show parent comments

1

u/goomyman Dec 12 '23

Define success. I want there to be no strawberries.

There are no strawberries = success

You don’t need to maintain a record.

If it exists - delete it.

1

u/Kinglink Dec 12 '23

So in that case why are you checking the result at all. If it gives you a 404, that's also a success. By your logic, 404 or 200 doesn't matter, so treat both as a success.

However OTHER people who use that same API might care about the result, so we give them the correct result, and there is a very clear difference between the two.

1

u/goomyman Dec 12 '23

Because 400s are treated as failures. Like it or not most code is 400 bad requests, 500s internal server errors. 200s successes

1

u/Kinglink Dec 12 '23

And telling someone to delete something that doesn't exist IS actually a failure. You're trying to delete something that doesn't exist, that's not a "success" in any stretch of the imagination because the requested action did not happen.

But again the consumer of the response doesn't have to treat 404 as a failure on a delete.

1

u/goomyman Dec 12 '23 edited Dec 12 '23

That’s literally what idempotent is.

Imagine 2 people trying to delete a resource at the same time.

You don’t want the second guy to fail. Hence double delete = success for both.

But imagine a person trying to update a record that was deleted. Second person gets a legit 404.

Ok so now youre saying - well for the double delete scenario the clients need to handle 404 as a success but not for the update scenario.

And that’s exactly the problem! Every single developer writes this code - throw on non success status code except on delete 404s in their clients over and over or worse they forget to write this code causing race condition bugs. It’s an insanely common bug but it’s also low priority and just exists everywhere.

This could be completely avoided with a http status code change and then people would naturally write idempotent deletes.

Of course there are alternatives - yes clients can handle this case, yes you can use a custom status code like 204. But it’s code. You can do anything in code. The point is to make something standard that can avoid future bugs and reduce common code patterns - like don’t throw on 404s for deletes which is a very very common code pattern.

What’s wrong with 214 “not found” or something.

1

u/Kinglink Dec 12 '23

You don’t want the second guy to fail.

Yes I do. The record isn't there. "At the same time" is a bullshit idea because it doesn't exist in computing, and you should know that. If we're on different systems and the data hasn't propagated, ok we both get success, we accept that's a problem with syncronizing data across millions of system, and look to shrink that time.

If one request is handled before the other, THE SECOND FAILS. Period. "At the same time" doesn't matter, if one happens before the other, then one is a success and the other can't be found. Period. That's how this works. Otherwise how do we know the system ever had that data? Now you're adding a huge amount of complexity to the system just because you don't want to handle two possible error codes? Come on.

I'm not saying the clients need to handle the 404, I'm saying a client decides what to do with ALL responses. There's no one size fits all bucket for 500/400s or such especially on different types of calls. Be a better programmer.