KatInterview

Design an Idempotent Payment Processing Service

System DesignModel answer
MediumModel answerSystem Designsystem designpaymentsidempotency

Last updated

Design a payment service that safely retries failed transactions without double-charging customers, handling scale of ~10,000 transactions per second.

Design an Idempotent Payment Processing Service

Background

You are a senior engineer at a fast-growing e-commerce platform. The checkout service calls a downstream Payment Service to charge customers. Due to network timeouts, client retries, and server restarts, the same payment request can arrive multiple times. Customers have complained about duplicate charges appearing on their credit card statements.

Your task is to redesign the Payment Service so that retrying a payment request is always safe — it either succeeds exactly once or fails cleanly.

Scale & Constraints

  • Peak load: ~10,000 payment requests per second
  • Payment amounts: $0.01 – $50,000 per transaction
  • Retry window: clients may retry the same logical payment for up to 24 hours
  • Latency SLO: p99 < 500 ms end-to-end
  • Durability: no payment record may be silently lost
  • Downstream: you call an external payment processor (think Stripe/Adyen) that is NOT idempotent by default — each call creates a new charge

What is Expected

  1. Requirements & clarifying questions — functional and non-functional
  2. API design — how clients communicate idempotency intent
  3. Data model — what you store and why
  4. Architecture — components, flows for the happy path and retry path
  5. Deep dives — at least two: (a) how you prevent double charges at the DB layer, (b) how you handle the case where the external processor responds but your service crashes before saving the result
  6. Trade-offs — discuss at least one alternative you considered and why you chose your approach

Hints

Hint 1

Think about where the idempotency key comes from and who is responsible for generating it — the client or the server?

Hint 2

What database property (e.g., a unique constraint) can act as your last line of defense against duplicate writes, even if application-level checks race?

Hint 3

Consider the window between 'call external processor' and 'write result to DB' — what patterns (e.g., outbox, status machine) help you survive a crash there?