Idempotency for APIs

Idempotency for APIs is especially important because the likelyhood of an API consumer implementing a “retry” functionality is alot higher than someone working with a UI.

What does a “retry” functionality have to do with Idempotency though you may ask?

As briefly explained in the article about Idempotency Idempotency is ensuring that actions always result in the same one thing. This is hard to do for API endpoints like POSTs because if not handled properly multiple exact same requests will cause multiple entries to be created, which can violate the Data Integrity.

To counteract this, a decently new HTTP header has been developed, called an “Idempotency-key” it is basically an identifier to the request, according to which, an application should decide if it should run the business logic again or simply return the cached response from a previous request.

Heres a Diagram to visualize this:

Request arrives with Idempotency-Key: key-xyzCheck key-xyzin storageExistsNot foundKey foundKey status?completedprocessingStatus: completedRequest hashmatches thestored one?MatchesDoesn't matchReturn cached response(don't reprocess)Return 422Different payloadfor the same keyReturn 409 Conflict(still being processed)Atomic Insert keywith status `processing`(UNIQUE constraint)Insert suceeded?(affected rows = 1)YesNo (duplicate insertExecute business logicSave response +update status to`completed`Return responseReturn 409Concurrent duplicate

And another Diagram to show how and what could go wrong with ERV BE PartnerAPI as an example:

Idempotency demonstration with PartnerAPI exampleUser.ClientServerDatabaseUser/ClientServerDatabaseUser/ClientServerDatabaseWithout Idempotency -- Retry = Saved twicePOST /private/bookings {policy data}Persist Policy DataServer crash / Timeout before sending response!Timeout -- no response recievedPOST /private/bookings (RETRY)Persist Policy Data AGAIN201 CREATEDTwo different Policies with identical data were persisted!With Idempotency -- Retry is SafePOST /private/bookings { Idempotency-Key:key-abc }Persist Policy Data + save key-abc + responseServer crash / timeout!TimeoutPOST /private/bookings { Idempotency-Key:key-abc } (RETRY)Check key-abc, already exists, get cached response201 CREATED (returns cached response, without actual change)Policy data is only saved once while Client still receives success response

But in what situation can a request be sent twice, usually after a request a user won't resend the request.

But an automated API might, incase it recieves no response due to some kind of network or server issue, it might send a retry on a timeout which would cause the request to be recieved and saved twice.

This is only an issue for endpoints with POST or PATCH since the others are naturally Idempotent.

A GET request is safe anyways as it doesn't edit data.

Delete is Idempotent as calling it multiple times will not delete more pages, only the same one over and over. This will lead to a different response to the user. Some kind of 404 Not Found will be thrown however, the effect on the application remains the same which is why its Idempotent.

PUT Is Idempotent as it overwrites one singular resource in the exact same way, which leads to no change in data.

PATCH even tho listed as not Idempotent, can also be Idempotent, it can also only edit things in the exact same way, however it can also edit resources percentually, which would lead to for example a price to be raised by 20% twice on a duplicate request.

This is only if you follow the conventions, since if you wanted to, you could just map a PATCH request to logic that creates a new entry.