KatInterview

Build a Multi-Provider Payment Routing and Ledger Platform

System DesignModel answer
MediumModel answerSystem Designsystem-designpaymentsidempotency

Last updated

Design a merchant payment platform that routes charges across several processors, survives timeouts without double-charging, and keeps refunds, captures and authorizations consistent.

Problem

A large global online merchant sells subscriptions and one-time items. Its checkout services should not talk to card processors directly. Design an internal payment orchestration platform that sits between checkout and several external payment service providers (PSPs), for example a card acquirer, a regional processor and a wallet provider.

The platform must handle the full payment lifecycle (authorize, capture, void, refund), choose among providers dynamically, tolerate flaky networks, and guarantee that a customer is never charged twice or refunded more than was collected.

Functional requirements

  • Expose operations to authorize, capture (full or partial), void and refund (full or partial).
  • Select a provider using configurable rules, and fail over when the preferred provider is unhealthy or timing out.
  • Provide exactly-once effect for client requests, even with concurrent retries.
  • Consume PSP callbacks (webhooks) that can be late, duplicated or out of order.
  • Keep an immutable audit trail and a double-entry ledger so that refunded <= captured <= authorized always holds.

Non-functional requirements

  • Strong consistency for payment state and money totals.
  • 99.999% availability for accepting new authorization requests.
  • p99 latency under 500 ms for the platform's own work (excluding provider time).
  • Never store raw card numbers; use a tokenization service to keep PCI scope small.

Scale

  • About 50 million transactions per day (roughly 600/s average, 3,500/s at peak).
  • Up to 10,000 webhook events per second at peak.
  • Financial records and audit data kept for 7 years.

What to deliver

  1. A high-level architecture from checkout to provider call.
  2. The payment state machine and a data model that enforces the money invariants.
  3. How you handle provider timeouts and failover without duplicate authorizations.
  4. How webhooks are ingested and reconciled with current payment state when they arrive out of order.

Hints

Hint 1

A timeout is not a failure. Think about what the platform knows after a timeout, and what must happen before a second provider is tried.

Hint 2

Separate the client's idempotency key (one logical request) from an attempt (one call to one provider), and give each attempt its own deterministic provider-side idempotency key.

Hint 3

Put the state change, ledger rows, audit/outbox event and idempotent response in a single database transaction, and add CHECK constraints so the invariants hold even if application code is wrong.

Hint 4

For webhooks, acknowledge fast, deduplicate by provider event id, partition by provider reference, and push events through the same state-machine code as synchronous responses, treating them as facts that may be stale.