Implementing Transactional API Idempotency With Node.js and PostgreSQL

https://hackernoon.imgix.net/images/0rOf1jJlOfYCu6022drLZvnJLlm1-aj93bpv.jpeg

Idempotency keys are supposed to make retries safe: the client sends the same key on every attempt, and the server runs the operation once and replays the saved response after that. Most implementations keep the keys in Redis, next to the API, while the operation itself writes to a database. If the server crashes after the database write and before the key is updated, the two disagree.

In my guide to idempotency in APIs with Node.js, I build an idempotent() wrapper for Express with an in-memory store and then a Redis store, and I mention this gap near the end: if the server dies after the work but before the response is saved, a retry can run the work again. This article picks up from there. I reproduce the double charge for real, and then close the gap by putting the key and the payment in the same PostgreSQL...

Copyright of this story solely belongs to hackernoon.com. To see the full text click HERE

Read more