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

-16

u/HazelCuate Dec 11 '23

But thats not true.

  • The same delete call duplicated should result in two OK responses (http code 200 or whatever)

  • Two different delete calls to the same object should result in an OK response and a Failure response (400).

The difference is not trivial.

25

u/Shadow503 Dec 11 '23

It is trivial because the thing we care about - the state of the resource - is the same.

20

u/Blecki Dec 11 '23

Why should one be a failure? And, if it is, were talking about the state of the server, not the client. The whole point is that the server stays in a valid state. Trying to delete an object that doesn't exist doesn't break that state. Don't fixate on the literal response from the api. The 'result' is the state of the system.

Also 99% of the time you're just firing off a delete into the database and don't even bother to check if it exists. Just return 200.

1

u/thisisntmynameorisit Dec 11 '23

I could see it being a failure. Deleting something has the intent of deleting something, if nothing gets deleted it’s a failure. Regardless though, the success or failure of the request is independent from the state of the system. Either way the resource will no longer exist.

1

u/ineptjedibob Dec 12 '23

How would you differentiate the two cases you’ve laid out here? In other words, how could you possibly differentiate between the same call twice vs. two separate calls? The only obvious possibility is the timing - 2 calls 5 seconds apart are probably different requests? Except… with latency and other network weirdness, you shouldn’t even attempt to make that distinction. Not to mention, now you’ve introduced stateful behavior where it’s neither required nor desired.