KatInterview

Architecting a Fault-Tolerant Payment Gateway Service

System DesignModel answer
MediumModel answerSystem Designsystem-designdistributed-systemsidempotency

Last updated

Design an enterprise-grade payment gateway that guarantees at-most-once execution, handles ambiguous network timeouts, and maintains an auditable, immutable ledger.

Architecting a Fault-Tolerant Payment Gateway Service

Design a backend payment processing infrastructure for a high-volume merchant platform. Your system receives payment requests from merchant applications, coordinates transaction execution with downstream third-party payment service providers (PSPs), handles asynchronous status webhooks, and provides exact financial status visibility.

The primary engineering goal is absolute safety: the system must never double-charge a user, must remain consistent despite arbitrary network drops or crashes, and must maintain an immutable audit trail of every state transition.

Core Architecture Requirements

  1. API & Data Layer: Define key API contracts, idempotency handling, exact money representations, and a data model capable of tracking state history.
  2. Execution & Failure Handling: Detail how requests interact with external PSPs under failure scenarios (e.g., connection drops mid-request). Address out-of-order callbacks and asynchronous status webhooks.
  3. Data Integrity & Reconciliation: Explain the out-of-band mechanisms used to resolve ambiguous payment states and verify platform balances against external PSP records.
  4. Operational Controls & Resilience: Outline partitioning strategy, rate limiting/circuit breaking, and testing strategies for distributed invariants.

Key Considerations

  • Network calls to PSPs are inherently unreliable and non-atomic. A timeout does not mean the transaction was rejected.
  • Clients will retry aggressively on failure.
  • Webhooks from third parties may arrive out of order, be duplicated, or fail entirely.

Hints

Hint 1

Think carefully about how to represent monetary amounts to prevent floating-point inaccuracies.

Hint 2

Distinguish between payment intent, client idempotency keys, and downstream PSP idempotency keys.

Hint 3

What pattern prevents dual-write issues between your database and your messaging queue?