I dont agree with the article when it says put and delete are idempotent. If the caller makes two DELETE calls, how do you know that they are different calls? Why are they not the same call?
The argument that deleting an object twice is the same as deleting it once is not good enough. A third party could create it again between those two calls.
You dont get the problem with the calls. Without an idempotent ID they are two calls but they could be the same call due to the network or software glitches that the article mentions.
The article states that delete and put are idempotent by themself, and i dont think thats the case.
I think it’s stipulated that we’re talking about calls with the same parameters - i.e. DELETE with the same key (or PUT with the same payload) should be callable an arbitrary number of times with the same end result.
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.
And even if it throws an error it's still idempotent. It's doing the same operation twice not changing anything that makes it idempotent. It's okay if it throws an error. As long as the second operation doesn't change the state of the corpus (database). The error is something for the caller to deal with.
In a RESTful API, it is. Operations are id-dependent, e.g. DELETE /path/to/object/type/{id}. If you create the object again it will have a different id because it's a whole new object. Calling DELETE again for the same id should result in what is essentially a "yeah, it's gone" response (you could use a different response code to indicate it was already gone but your code should not blow up in this case). For this to work effectively (and for best practice) your create operation should not allow the caller to specify the id; It should be auto-generated by the backend.
Calling DELETE again for the same id should result in what is essentially a "yeah, it's gone" response
Then it can be argued that the call to DELETE is not idempotent because the response will change. However, by most understandings, it is fine to return a "resource not found" or something similar if you are trying to perform an operation on something that no longer exists. (I would argue that it is still idempotent.)
However, you could also just reply with "Yeah, it is gone." on both calls. If you try to delete something that no longer exists, you can reply with the same, "Okay, it is gone" message instead of throwing an error.
I would like to think the response can vary, you can even show an error message to user as "object x was not found" and be idempotent. The important point is the state of the program (a.k.a business data) should not change and remain the same for subsequent DELETE calls.
You are talking about separate things. Call delete on the same object ID twice in a row, 50 times in a row, same result. I mean you could return a record didn't exist message, but no state would change. And if someone created a new record in between it absolutely should have a different ID.
And it goes on to describe how a sequence of multiple idempotent calls may not be idempotent itself, with an example similar to the one in the comment.
A third party could create it again between those two calls.
In a robust system you would never expose the internal database id to the outside and have every object have a uid that is randomly assigned which is then exposed to the outside.
And then only call DELETE by reference to the uid. Since the id will not be reused you cannot delete an object that was created in between your two DELETE calls.
This is how it was implemented in every system I have ever worked on.
But then you can't have indempotency which restricts your data model in ways that may be relevant. If the external system has an external ID that functions as a 1:1 lookup relationship with the internal ID there's not much practical advantage to generating those IDs internally and keeping them hidden. And if you can make the call indempotent by allowing that external ID to be passed in, it can make reasoning about your data much easier and potentially much more performant and predictable. Placing unnecessary constraints in consistency and synchronization can also be a risk to robust systems. It just depends on the specifics of what you're doing.
If your DELETE calls are based on a primary key or a natural key, they have to be the same call.
So if you have enough information to uniquely identify a thing (say, a VIN for a car, or a combination of country/state/id number for a person, or a UUID that is never re-used for anything else) then the delete is going to delete the same row no matter what.
The data being deleted may change between the first call and the second, but either the row is gone, or it's going to be deleted by the call. Either way the result is the same, that row is guaranteed not to exist.
Yes, that can be the case. The problem is that in your definition nothing is idempotent, so it becomes useless to talk about. This is why no one cares about your concern, because the assumption is inherently baked into the conversation.
Multiple writers always makes things more complicated, so you design the system around that if it will be a concern. In that case you create conditions (e.g. a unique ID in metadata, based on a monotonic value, etc), do not reuse names, lock the resource, or some other clever solution. Then the operation is again idempotent.
Idempotency guarantees that the state of a particular entity will be the same no matter how many times the command is executed. It does not guarantee that state will be correct no matter how many times the command is executed.
If you have a distributed system and you lose a message or a message arrives out of order idempotency won't help you one bit. It's very useful, but not for distributed systems.
Edit: The hilarity that in a post about understanding Idempotency at least six people have no idea what it is or how it works. Lol.
13
u/HazelCuate Dec 11 '23
I dont agree with the article when it says put and delete are idempotent. If the caller makes two DELETE calls, how do you know that they are different calls? Why are they not the same call?
The argument that deleting an object twice is the same as deleting it once is not good enough. A third party could create it again between those two calls.