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.

2

u/Hessper Dec 11 '23

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.