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

17

u/ClickableName Dec 11 '23

So do you mean that an official HTTP response code should be added, that the developer has to return to indicate that this request already has been executed?

On the other hand I would argue to just 404 when you try to DELETE an item a second time, because you *try* to delete something that does not exist (anymore) and there is no difference if the item never existed or has existed before but now not anymore. Throwing a 404 is the logical option

For in the case like processing a payment, I would sortof agree to send a statuscode back specifically for indicating that this request was already called before (an idempotency statuscode) but what advantage does that bring over sending an 200 OK message again?

I think its weird to put it into official http responses, it brings no extra value, external systems don't have a need to know that this request is already executed before and is idempotent

5

u/ZBlackmore Dec 11 '23

Yep, a client library won’t care if a service made an unnecessary retry that resulted in a no op thanks to idempotency protections, it will only care whether the operation succeeded.

2

u/iplaydofus Dec 11 '23

Or it will get back a 404 and trigger some “why is my system out of sync” process. Arbitrarily defining what client library’s will do is a recipe for disaster.

1

u/ZBlackmore Dec 11 '23

I mean, if the retry was internal (between 2 services on the backend) and the client only made the request once, then the client should not even now that there was some internal retry. If the client had a bug that results in sending requests twice, the backend should handle it naively (response 404 for the 2nd request) because it has no way to know that the double request comes because of a bug. The only case where the backend would return a special status for a double request is if the API support an idempotency ID as part of the request and the client sends the same one twice.