← All posts

Idempotency belongs to the whole payout path, not one endpoint

A retry can come from the user, the client, a worker, or a batch job. Every layer needs an answer.

The problem

A cashback payout can be retried in many places: a double click, a client timeout, a worker restarting mid-job, or a batch file regenerated after a failure. An idempotency key on the API alone doesn’t stop money from moving twice.

Principles I lean on

Trade-offs

Option Gain Cost
DB unique constraint Simple, survives crashes Needs careful key design
Distributed lock Flexible across services Lock expiry and failover edge cases

What I’d revisit

Exactly-once is a promise you can’t keep across a bank boundary. Design for at-least-once plus deduplication, and treat daily reconciliation as the real safety net.