Datasets:
DIST3: Payment Service Idempotency
Goal
Make 4 payment operations safe to retry by implementing idempotency key deduplication.
Requirements
- Charge creation must be idempotent: same
idempotency_keyreturns same charge result - Refund processing must be idempotent: same key returns same refund result
- Webhook delivery must be idempotent: same key sends webhook exactly once
- Ledger entry creation must be idempotent: same key creates entry exactly once
- The balance query endpoint must NOT be modified (it is already safe to retry)
- Implement the idempotency store in
payments/idempotency.py - All tests must pass:
pytest tests/
Supporting Documents
payments/charges.py— Charge creation (not idempotent)payments/refunds.py— Refund processing (not idempotent)payments/webhooks.py— Webhook delivery (not idempotent)payments/ledger.py— Ledger entries (not idempotent)payments/balance.py— Balance query (ALREADY IDEMPOTENT — do not change)payments/idempotency.py— Empty stub — implement this
Idempotency Key Pattern
Each operation receives an idempotency_key: str parameter. Before executing:
- Check if
idempotency_keyexists in the dedup table - If yes: return the cached result from the first execution
- If no: execute the operation, store the result with the key, return result
The idempotency key is set by the caller. Operations must never execute twice for the same key.