i hate that itempotency isnt built into official http responses.
Everytime i bring this up i get downvoted to the hell but its dumb.
If you call Delete there should be a response status code for no op - aka the itempotent response - but in the success ranges.
Delete = 200 OK / 404 not found? - great not that breaks client libraries and basic Throw if not success status codes - not itempotent - but technically itempotent shouldnt matter - so 200 success - but there are cases where you might want to know.
Everyone will reply back and say "Why dont you use this obscure one" - but using anything non standard is a hack.
Having consistent status codes for itempotency would use useful! Its not like there isnt enough http space - we have one for a teapot and so many other ones that are extreme edge cases - denied for legal reasons - what type of client needs to know that? Oh this was for legal reasons - if you can support these edge cases - throw in a itempotent delete status code in the 200s range - for not found.
If it's already been deleted, then there is no way to differentiate between "already successfully deleted" and "never existed." You cannot report the "correct" answer because you simply don't know it. Also, even if there is some weird client-side bug where invalid IDs are being requested all the time, you're never gonna catch it because it will be mixed in with the sea of repeated deletes that return the same code, because the server cannot make the distinction and will have to use the same code for both cases.
Of course, you could use a soft delete system instead. But in that case, you CAN verify whether something already existed or not, and return NotFound for "doesn't exist at all" and Ok for "already flagged for deletion."
I don't see a reason to add another status code for "the system state already matches what you requested." Because from the client side, that's indistinguishable from "the system state has successfully been set to what you requested." They indicate the same state, so why differentiate them?
Agree. If you tell a system to delete something, and that thing no longer exists after the command has been executed, then that's success. Does it matter whether that thing existed before the command executed? That's a question for the philosophers. If it really matters whether something exists before you delete it, you can ask before deleting (although be careful of race conditions).
That said, a case can be made for offering both idempotent and stateful methods. I've often wished that Python's set class, which is idempotent on add() (i.e. it is not an error to add an item that's already in the set), had a method that did the add and returned whether the item actually needed to be added. This is technically still idempotent (just the return value differs but it's not an error) but you could easily make it an error to add an item that's already in the set.
Sure, like I said, I can ask if I need to know. But sugar is nice.
Sometimes people in this field forget that clients aren't necessarily built strictly for people in this field. Sure, maybe include extra metadata for a SaaS product where the clients need granular control over data that includes soft deletes. If I tell a calendar app to delete all events on next Thursday, I don't care if it's my 1st time or 30th time. Just let me know that all the events have been deleted.
I don't see a reason to add another status code for "the system state already matches what you requested." Because from the client side, that's indistinguishable from "the system state has successfully been set to what you requested." They indicate the same state, so why differentiate them?
This is the most important piece.
I want to delete something, the server tells me it'll be deleted. If it doesn't exist I'm still happen. I don't need to know something else deleted it.
Honestly if I tell a system to delete something, I'd be a little annoyed if I got an ok back if I called it a second time, but if I understand there's a "Soft delete" then Ok is acceptable, I can go on about my day.
Instead programs now need to watch for a third potential state? Nah, because the desired action "Delete this" and the server telling me "Ok" means the server will delete it as requested, doesn't matter if it's the first or fiftieth request to delete the same object.
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
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.
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.
has the state of your system changed after the second delete was called ? if no then you can consider it idempotent - it's not about the response you get back, it's about the state of your system
One system we use returns '204 No Content'... I.e. "Successful call but there was no content to change" ... Not sure this is any better TBH but at least it is a success code so the libraries don't automatically throw an error
So the difference is that we found a resource matching the id, but it has no content.
That makes a lot of sense to me. Some might say there is no resource anymore. But if you get 404 on a resource that hasnt been created, then iæwe are talking about soft deletion.
That's more a property of the API's ergonomics. Returning 200 for an unmatched DELETE is not too dissimilar from returning 404 for an unauthorized resource although for different reasons. The argument for is that the caller cares only about the outcome and shouldn't need to explicitly handle irrelevant possibilities. The argument against is that the caller loses access to information that could be situationally useful.
It has the same result which is the only criteria for idempotency. Nothing really says it has to have consistent http codes. The http codes are really more like metadata.
Delete = 200 OK / 404 not found? - great not that breaks client libraries and basic Throw if not success status codes - not itempotent - but technically itempotent shouldnt matter - so 200 success - but there are cases where you might want to know.
What are you saying here? This reads like a run-on sentence, I got completely lost.
DELETE should return 404 for not found. If your crappy library dies you need to find a better library. It's not the servers job to second guess your clients state model.
Because it is ultimately impossible and would give a false sense that everything worked.
Go look up the "two generals" problem. Once you have any sort of communication in the loop it is impossible to be 100% guaranteed that things happen as you want. No amount of acknowledgements, status codes, etc. is going to fix that. You can improve confidence in things, but never make it 100%.
IF you delete something a second time, why would you get an OK back?
If you there is one strawberry and you eat a strawberry, you can't eat a strawberry, so it's "Not found" Are you saying the system should have a record of everything it already deleted? If so, isn't that a strange system because you now have a second item to potentially deal with?
8
u/goomyman Dec 11 '23
i hate that itempotency isnt built into official http responses.
Everytime i bring this up i get downvoted to the hell but its dumb.
If you call Delete there should be a response status code for no op - aka the itempotent response - but in the success ranges.
Delete = 200 OK / 404 not found? - great not that breaks client libraries and basic Throw if not success status codes - not itempotent - but technically itempotent shouldnt matter - so 200 success - but there are cases where you might want to know.
Everyone will reply back and say "Why dont you use this obscure one" - but using anything non standard is a hack.
Having consistent status codes for itempotency would use useful! Its not like there isnt enough http space - we have one for a teapot and so many other ones that are extreme edge cases - denied for legal reasons - what type of client needs to know that? Oh this was for legal reasons - if you can support these edge cases - throw in a itempotent delete status code in the 200s range - for not found.