Tip 6.13 Β· The Conscious Node Β· API Design
The retry that charges your customer twice
A retried GET is harmless, because it returns the same data. A retried POST is different. A client submits "create order", the response gets lost on the way back, the client retries, and now there are two orders for the same customer. Every retried POST is a duplicate-charge risk unless the endpoint is idempotent.
app.post('/api/v1/orders', async (req, res) => { const order = await placeOrder(req.body); // the network drops the response, // the client retries, a second order is created res.status(201).json({ data: order });});// client sends a unique Idempotency-Key// per logical operationconst stored = await db.idempotencyKeys .findByKey(key); if (stored) { if (stored.requestHash !== requestHash) { return res.status(409) .json({ error: 'key reused, new body' }); } // replay: same status, same body, // operation never re-executes return res.status(stored.statusCode) .json(stored.response);}Require an Idempotency-Key on every write with side effects. The first request executes, and duplicates replay the stored response, so charging a customer twice becomes structurally impossible. Stripe has run on this pattern for over a decade.







