Architecture Intermediate
Idempotency — Safe Operation Retries¶
IdempotencyAPIReliability 3 min read
Implementing idempotent APIs. Idempotency keys and database techniques.
What Is Idempotency?¶
Multiple executions = same result. GET, PUT, DELETE are naturally idempotent. POST is not.
Idempotency Key¶
app.post('/api/payments', async (req, res) => {
const key = req.headers['idempotency-key'];
if (!key) return res.status(400).json({ error: 'Missing key' });
const existing = await redis.get(\`idem:\${key}\`);
if (existing) return res.json(JSON.parse(existing));
const result = await processPayment(req.body);
await redis.setex(\`idem:\${key}\`, 86400, JSON.stringify(result));
res.status(201).json(result);
});
DB Idempotency¶
INSERT INTO orders (external_id, customer_id, total)
VALUES ('ord-123', 'cust-456', 1000)
ON CONFLICT (external_id) DO NOTHING RETURNING *;
Summary¶
Implement idempotency keys for POST/PATCH. Always expect repeated requests.
Need Help with Implementation?¶
Our team has experience designing and implementing modern architectures. We’re happy to help.