Idempotency Workflow
How a POST /payments with an Idempotency-Key charges at most once, even with retries, a different body or concurrent requests.
The path of one request
The dashed box is a database transaction. Every IdempotencyReserver method opens its own (REQUIRES_NEW), so each change is committed before the facade moves on.
PaymentService is reached only by the request that reserved the key. The counter behind GET /payments/executions shows how many real charges happened.How createPayment decides
Every request ends in exactly one of five answers. Only the top row calls the provider.
States of a row in idempotency_record
release() is the only way back to “no row”, and only when the provider failed.The five scenarios
| Scenario | Record found | HTTP | Real charges |
|---|---|---|---|
| A · First requestNew key pago-001 | none | 201 Created | 1 |
| B · Retry, same keySame body again | COMPLETED, same hash | 200 OK | 0 |
| C · Different bodySame key, amount 999.00 | hash differs | 422 | 0 |
| D · Concurrent requests2+ concurrent, same new key | one INSERT wins; the rest see PROCESSING | 201 409 | 1 |
| E · Provider failureProvider throws | row deleted by release() | 500 → 201 | 1 (on retry) |
2 chargesafter the five curl steps of the README (pago-001 and pago-002)
Sequence diagram per scenario
Each scenario step by step, with every call between classes and the database.