Idempotency Is Easy Until the Second Request Is Different

(blog.dochia.dev)

62 points | by ludovicianul 3 days ago ago

17 comments

  • mmillin an hour ago

    This is an excellent article, I’ve seen almost all of the issues it calls out in production for various APIs. I’ll be saving this to share with my team.

    I’ve seen two separate engineers implement a ā€œgeneric idempotent operationā€ library which used separate transactions to store the idempotency details without realizing the issues it had. That was in an organization of less than 100 engineers less than 5 years apart.

    One other thing I would augment this with is Antithesis’ Definite vs Indefinite error definition (https://antithesis.com/docs/resources/reliability_glossary/#...). It helps to classify your failures in this way when considering replay behavior.

  • raffael_de 3 minutes ago

    Idempotency means f(x) = f(f(x)).

    Here x is interpreted as state and f an action acting on the state.

    State is in practice always subjected to side effects and concurrency. That's why if x is state then f can never be purely idempotent and the term has to be interpreted in a hand-wavy fashion which leads to confusions regarding attempts to handle that mismatch which again leads to rather meandering and confusing and way too long blog posts as the one we are seeing here.

  • shiandow an hour ago

    This seems to assume retrying a command should result in the same response, but I am not sure I agree.

    Idempotency is about state, not communication. Send the same payment twice and one of them should respond "payment already exists".

    • raffael_de a few seconds ago

      > Send the same payment twice and one of them should respond "payment already exists".

      You are hiding the relevant complexity in the term "same". What is here the same? I mean, if accidentally buy only 1 instead of two items of a product and then buy afterwards again 1 item. How is this then the same or not the same payment?

    • CodesInChaos a few seconds ago

      That's not idempotency. But when combined with a GET request, it's a perfectly fine way of achieving the same result: no double payment, and both systems are in a consistent state.

    • Jolter 28 minutes ago

      I don’t know if we’re reading the same article? The linked one states very plainly:

      ā€Idempotency is about the effect

      An operation is idempotent if applying it once or many times has the same intended effect.ā€

      • shiandow 8 minutes ago

        I do not disagree with their definition of idempotency, but they silently assume resending the same result is the default. They discus this later on in the article but they do not seem to question why that might not be a good idea in the first place.

    • cocoto an hour ago

      In your example, idempotency means same request + same state = same response. State becomes part of the request, that’s why it is hard.

      • shiandow an hour ago

        That's just deterministic behaviour.

        For idempotency you literally just want f(state) = f(f(state)). Whether you achieve this by just doing the same thing twice (no external effects) or doing the thing exactly once (if you do have side effects) is not important.

        But if you have side effects and need something to happen exactly once it seems a lot more useful to communicate this, rather than pretending you did the thing.

        • adrianmsmith 12 minutes ago

          > But if you have side effects and need something to happen exactly once it seems a lot more useful to communicate this, rather than pretending you did the thing.

          I think it depends on whether the sender needs to know whether the thing was done during the request, or just needs to know that the thing was done at all. If the API is to make a purchase then maybe all the caller really needs to know is "the purchase has been done", no matter whether it was done this time or a previous time.

          And in terms of a caller implementing retry logic, it's easier for the caller to just retry and accept the success response the second time (no matter if it was done the second time, or actually done the first time but the response got lost triggering the retry).

  • WilcoKruijer 2 minutes ago

    I really hate the POST verb for RESTish APIs because it cannot be idempotent without implementing an idempotency layer. Other verbs are naturally idempotent. Has anyone tried foregoing POST routes entirely? Theoretically you can let the client generate an ID and have it request a PUT route to create new entities. This would give you a tiny amount of extra complexity on the client, but make the server simpler as a trade-off.

  • mrkeen 12 minutes ago

    The point of idempotency is safe retries. Systems are completely fallible, all the way down to the network cables.

    The user wants something + the system might fail = the user must be able to try again.

    If the system does not try again, but instead parrots the text of the previous failure, why bother? You didn't build reliability into the system, you built a deliberately stale cache.

    • mrkeen 3 minutes ago

      "Idempotency" feels like "encapsulation" all over again.

      Take a good principle like 'modules should keep their inner workings secret so the caller can't use it wrong', run it through the best-practise-machine, and end up with 'I hand-write getters and setters on all my classes because encapsulation'.

  • zinkem an hour ago

    Idempotency is easy if you don't use mutable state in your middleware.

    Auth, logging, and atomicity are all isolated concerns that should not affect the domain specific user contract with your API.

    How you handle unique keys is going to vary by domain and tolerance-- and its probably not going to be the same in every table.

    It's important to design a database schema that can work independently of your middleware layer.

  • chaz6 an hour ago

    You keep the hash of the request so that you can reject a subsequent request with a different body. This has helped me surface bugs and data issues in other systems.

  • stavros 2 hours ago

    Half of the mentioned issues are issues of atomicity, not idempotency. If I make a request, and the server crashes midway and doesn't send some crucial events, that's an issue whether or not I send a second request.

    From a cursory read, only the part up to "what if the second request comes while the first is running" is an idempotency problem, in which case all subsequent responses need to wait until the first one is generated.

    Everything else is an atomicity issue, which is fine, let's just call it what it is.

  • villgax 16 minutes ago

    skill issue lol, it's not idempotent anymore, same key for different requests? Heard of a nonce?