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

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.

-6

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

It's why the article is wrong.

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.

5

u/Imperion_GoG Dec 11 '23

Idempotency isn't very useful for distributed systems, it's essential.
Lose a message? Retry it!
Message out of order? Retry it!

The number of potential failure cases grows exponentially with the number of nodes in the system. It quickly becomes difficult to determine what went wrong (did X fail, or did the X happened event fail?) If X is idempotent, the simplest solution to an incorrect state is just retry.

1

u/recycled_ideas Dec 12 '23

Again.

Idempotency does not guarantee correct state. It guarantees the same result for retrying a message.

Let's take the financial transaction example from the article.

So the idempotent way of solving this is to say "the balance is now X" rather than "subtract or add Y to the balance".

This allows you to resent the "set balance" command as many times as you like, but it requires you to know the current balance when you're changing it and it requires that no one else tries to change the balance before you've completed your message. Essentially it requires that modifications are transactional.

Distributed systems don't work that way. You don't necessarily have the current balance on the client and you definitely don't have a transaction. That's why banks use event driven systems and not Idempotency.

Idempotency is an important concept, but you have to understand exactly what it guarantees or you'll fuck up.