Live data from Hacker News

Idempotency

berkansasmaz.com

11–20 of 69 posts

Re: Idempotency

#11
post #5

Come here to learn more details and maybe find answers to my questions. Unfortunately, the article is very modest explaining how to work with idempotency. Missing key answers like what about two requests in flight, how to store and query requests status reliably. The article mention requests should be ACID, but the diagram has caching storage. Would redis/dynamodb work here?

Redis has SETEX that will set if not existing. Can be used as a lock.

Re: Idempotency

#12
Idempotentancy on POST isn't just about preventing double posting. For POST the RFC says you can return 303 when the call to create a resource would have resulted in generating an existing resource. You need a Location header for allowing the client to redirect to the corresponding GET. This allows clients to POST multiple times (over time) for the same resources and not have to handle 400 errors.

Re: Idempotency

#13
post #7
post #4

I know it's just an introduction so perhaps it shouldn't be taken too literally, but the sequence diagram of how to make an order idempotent feels like instructions on how to create race conditions. You can't check for existence to guard against duplicate creation without either locking or another approach to making the Exists-or-Create step atomic. If you were to put in the sequence as described, you'd have somethin…

Presumably unique constraints could also solve for this, in a properly written transaction.

That depends how your API is structured, and again comes back to the atomicity and idempotency.

If your API is for example: "POST /orders/create {products:[{bananas:1}], shippingAddress:{},...}", then it's not clear how you would check uniqueness, because you'd want the same person to make the same order at different times by design.

To prevent double-submit, and make it idempotent, you need to add extra information.

One way to address this is generating transaction/sequence/correlation IDs at the very start of the process. So for example when you first request the order form, a transaction / correlation ID (called in the article an Idempotency key) is generated.

Then you would include that transaction ID in the order, and could prevent duplicate use of an order ID at multiple levels of the system including the database.

One design is to have a single-source-of-truth, and have at the (internal) API layer a state machine for a transaction. Once an order has been processed then it's impossible to make another order because there's no "make order" step from the state that an order is in having been processed. Again you need some kind of correlation ID to track which state machine needs loading.

That however is less scalable than other solutions including things like event-sourced architecture, which would also prevent duplicate orders through eventual consistency although there's less (or no) guarantee in many such systems that the order submitted "first" would win the race and not be superseded by the second. Such behaviour is fine if documented.

Re: Idempotency

#14

One of the things I think I did right in my career is working on payment and billing systems early on. It does teach you how to build at least semi-reliable software because people tend to get really mad when you screw with their money. It also is a good introduction to regulation and compliance. Idempotency was is one of the concepts you get taught on day one in that field (as kind of demonstrated by that being the…

Interestingly, I didn’t have to be taught about idempotency early on in my career; I’m pretty sure I didn’t even know what that word meant. I’m a naturally curious person and I was always curious about the failure cases for the software I worked on—e.g., “What happens if this request is successful but times out before returning a response to the client?” I honestly assumed every programmer asked these types of questions, but I learned the hard way over the years that this isn’t the case.

Re: Idempotency

#15
pet peeve: idempotency is badly named. idempotent literally translated is ‘same power.’ two or more things having the same power doesn’t necessarily mean they produce same, single result. if that were the case we wouldn’t need the special handling for idempotent instructions/commands/actions to ensure duplicates are not created.

Re: Idempotency

#16
post #4

I know it's just an introduction so perhaps it shouldn't be taken too literally, but the sequence diagram of how to make an order idempotent feels like instructions on how to create race conditions. You can't check for existence to guard against duplicate creation without either locking or another approach to making the Exists-or-Create step atomic. If you were to put in the sequence as described, you'd have somethin…

As I understand it, it's not checking for the existence of the resource but for the existence of the request. Assuming the requests are unique to a user/client and the user has a sequential conversation with the API server, the server would process the received requests in order and would reject the duplicate. However, if the requests of the same key (duplicates) are sent to any of several API servers, you have a pro…

Even on the same server there's still a race condition if checking the key has been used and marking the key as used aren't atomic if more than one request can be handled at once.

Re: Idempotency

#18

pet peeve: idempotency is badly named. idempotent literally translated is ‘same power.’ two or more things having the same power doesn’t necessarily mean they produce same, single result. if that were the case we wouldn’t need the special handling for idempotent instructions/commands/actions to ensure duplicates are not created.

I’m not totally sure, but in math “idempotent” elements are those for which x^2 = x. And so in software engineering it refers to operations for which doing them twice has the same effect as doing them once. Maybe this is what is meant by “same power”?

Re: Idempotency

#20
post #6

Idempotency is one mathematical concept that's pretty useful for software engineers to understand. Heck, might be more useful than BigO. - Useful to know when you work with APIs (as the article outlines). - Very useful when working with background jobs. You're not gonna have a good time if those aren't idempotent. - Good to know for interviews. I was asked to explain idempotency a handful of times, weird as that is.

I want to see an Up Goer Five⁰ explanation of all the ways "you will not have a good time today" with distributed systems.

0: https://xkcd.com/1133/

Post reply on HN