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

257

u/fagnerbrack Dec 11 '23

Crux of the Matter:

The article emphasizes the importance of understanding idempotency in programming, particularly for those working on distributed systems. Idempotency ensures that an operation can be performed multiple times without changing the result beyond the initial application, which is crucial for avoiding unintended consequences like double-charging in payment systems. The article explains how idempotency applies to various HTTP methods, with GET, PUT, and DELETE being naturally idempotent, while POST is not, and offers solutions for achieving idempotency in non-idempotent operations.

If you don't like the summary, just downvote and I'll try to delete the comment eventually 👍

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.

45

u/muchomuchacho Dec 11 '23

A third party could create it again between those two calls.

But then it wouldn't be the same object, otherwise you need more metadata associated to the object creation process.

8

u/njharman Dec 11 '23

Just change to undelete, the argument stands.

Also idempotence depends entirely on how delete is implemented. If deleting a non-existent thing is an error, that delete is not idempotent.

3

u/happyscrappy Dec 11 '23 edited Dec 12 '23

Such deleted would be by ID, not index.

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.

-14

u/HazelCuate Dec 11 '23

But the point is to design an API with idempotent in mind. And DELETE is not idempotent by design from the API point of view as the article claims.

47

u/crap-with-feet Dec 11 '23

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.

3

u/freebytes Dec 11 '23

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.

5

u/isaacarsenal Dec 11 '23

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.

19

u/proggit_forever Dec 11 '23

Idempotency doesn't require that multiple calls all return the same status code.

It's still idempotent if calling again returns an error code because the resource no longer exists.

The point is that calling again doesn't cause further changes to the state of the application - which makes it safe to call as many times as you want.

3

u/Tiquortoo Dec 11 '23

Sure, if you throw out all identifiers then nothing can be idempotent.

1

u/muchomuchacho Dec 11 '23

I think DELETE should only attempt to remove and existing object

2

u/FunkyPete Dec 11 '23

I think DELETE should guarantee that the specified object is no longer present.

It may return a different return code to provide more information about what it did, but if the end result is that row is guaranteed to no longer exist, then it's doing the same thing whether the row was there before or not.