KatInterview

Design a Checkout-to-PSP Payment Gateway

System DesignModel answer
MediumModel answerSystem Designdistributed-systemsidempotencyfault-tolerance

Last updated

Design a fault-tolerant intermediary service that processes charge requests from sellers, coordinates with an external payment provider, and maintains a consistent transaction ledger.

Context

You're building the payment backbone for a multi-merchant e-commerce platform. When a buyer clicks "Pay", the platform needs to forward the charge to an external Payment Service Provider (PSP), track what happened, and make sure the seller's ledger stays accurate — even when the network misbehaves or your service crashes mid-request.

What You're Designing

A payment gateway service that sits between merchant backends and a third-party PSP. It is responsible for accepting charge requests, orchestrating calls to the PSP, and persisting a reliable record of every transaction's lifecycle.

Functional Requirements

  1. Charge initiation: Accept authorize and capture requests for a given amount and currency from merchant systems.
  2. No double charges: If a merchant retries a request because of a timeout, the buyer must never be charged twice.
  3. Crash safety: If the service dies after forwarding a charge to the PSP but before writing the result locally, the system must recover to a correct state on its own.

Non-Functional Requirements

  • Strong consistency for financial records — no dirty reads, no lost updates.
  • Graceful degradation when the PSP is slow or temporarily unreachable.
  • Throughput target: sustain 1,000 transactions per second (TPS), with headroom for 3–5× spikes.

Deliverables

Work through all four of these:

  1. API surface — what endpoints do merchants call, and what do the requests and responses look like?
  2. Data model — what tables or collections do you need to track transaction state, guard against duplicate processing, and maintain an append-only ledger?
  3. Failure handling — walk through what happens when the PSP times out or returns a 5xx. How does the system recover without human intervention?
  4. Reconciliation — if your local records and the PSP's records diverge (e.g., after a prolonged outage), how do you detect and resolve the gap?

A strong answer discusses trade-offs, names specific failure modes, and explains why each design choice prevents a specific class of bug.

Hints

Hint 1

Before calling the PSP, write your intent to the database first. This gives you something to recover from if the service crashes mid-flight — think about what a background worker needs to find in order to figure out what happened and finish the job.

Hint 2

The PSP is an external side effect you cannot roll back. The key insight is that every call to the PSP should carry a stable, deterministic idempotency key derived from your internal attempt record — so retrying the same call is always safe, whether your service or the PSP is the one retrying.

Hint 3

A transaction's outcome is not always known immediately. 'PENDING' is a valid and honest state to return to the merchant. Design your state machine so a late webhook or recovery worker can only move a payment forward, never backward into a state it already left.