Idempotency keys: the boring header that saves the queue
Every payment API has it, and almost no internal service does. Idempotency keys are the cheapest way to make a queue safe.
The shape I use in Go:
- the client sends
Idempotency-Keywith the request - a middleware checks a dedupe table keyed by
(key, route) - if it exists, return the stored response
- if not, process, store the result with a TTL, then return
The subtle parts: store the response, not just the status; set the TTL to something longer than your retry window; and make the check-and-insert atomic, or two workers will race and both process.
The win is that at-least-once delivery becomes safe without changing the queue. The cost is one small table and a header.
I'd argue every handler that touches money or email should have one. What's the smallest service where you'd skip it?
原文:https://www.a8q9.cn/en/article/idempotency-keys-the-boring-header-that-saves-the-queue-6d71af.html