Making retries safe.
WHY IT MATTERS MORE HERE THAN ANYWHERE
A repeated request can charge someone twice.
WHAT AN IDEMPOTENCY KEY IS
A unique value supplied with a request, identifying the intent.
WHAT THE SERVER DOES WITH IT
Records it, and returns the original result if the same key arrives again.
WHAT THAT ACHIEVES
A retry after an unknown outcome returns the original result rather than repeating the action.
WHAT TO GENERATE IT FROM
The business intent, not a random value per attempt.
WHY
A new key per attempt defeats the purpose entirely.
WHAT TO USE
An order identifier, or a deterministic combination.
HOW LONG TO RETAIN KEYS
Long enough to cover any plausible retry.
WHAT TO DO WHEN THE SAME KEY ARRIVES WITH DIFFERENT PARAMETERS
Reject it, since it indicates a fault.
WHAT ELSE MUST BE IDEMPOTENT
Notification handling Refunds Payouts Ledger entries
WHAT TO ENFORCE AT THE DATABASE
A uniqueness constraint on the reference.
WHY THERE
It is the only place that cannot be bypassed by a race between processes.
WHAT TO TEST
Sending the same request concurrently, several times.