Idempotent Handler For Out Of Order Payment Gateway Callbacks
Last updated: November 29, 2026
Quick Overview
Payment gateway webhooks for an order can arrive duplicated and out of order (for example CAPTURED before a late AUTHORIZED). Implement a handler that applies each event at most once and never moves an order backwards in its payment state machine.
Meesho
November 29, 20260
0
0 solved
Payment gateway webhooks for an order can arrive duplicated and out of order (for example CAPTURED before a late AUTHORIZED). Implement a handler that applies each event at most once and never moves an order backwards in its payment state machine.
Meesho's prepaid and UPI orders depend on asynchronous gateway callbacks, and a duplicate or late event that flips a captured order back to authorized creates support tickets and wrong payouts. The interviewer is checking that you know the difference between deduplication and state ordering, and how each maps to a database constraint.
What the Interviewer Expects
- Deduplicates on a gateway event id, not on order id
- Encodes allowed transitions explicitly
- Ignores stale transitions instead of erroring
- Explains how to make dedupe and update atomic in a real database
- Returns a success status to the gateway for duplicates so it stops retrying
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- Two workers receive the same event at the same time. What breaks and how do you fix it in SQL?
- The gateway does not send event ids. What do you dedupe on?
- How would you reconcile orders whose callback never arrived?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
handle(event_id, order_id, new_status) is called for every webhook. Duplicates (same event_id) must be no-ops. Allowed transitions: PENDING -> AUTHORIZED | CAPTURED | FAILED, `AUTHORIZED -> CAPT...
High-Level Design
Two separate guards:
- Dedupe on
event_id, because the gateway retries until it gets a 2xx. - Monotonic state machine, because distinct events can arrive in any order. A late
AUTHORIZED...