KatInterview

Design a Payment Authorization, Capture, and Settlement Engine

System DesignModel answer
MediumModel answerSystem Designsystem-designdistributed-systemsfinancial-systems

Last updated

Architect a resilient payment platform handling two-phase auth/capture flows, double-entry financial ledgering, and batch settlement reconciliation with downstream gateways.

Scenario

Design the core backend services for a payment processing gateway. The platform must handle pay-as-you-go and subscription transactions. This involves reserving funds synchronously (authorizations), processing deferred claims (captures), recording all balance movements in an immutable financial ledger, and assembling daily batch files for settlement and external bank reconciliation.

Requirements

Functional Requirements

  1. Authorization API (/v1/payments/authorize): Synchronously validate card details and place a temporary hold on funds for up to 7 days.
  2. Capture API (/v1/payments/capture): Claim previously authorized funds. Must support full captures, partial captures, and voids (cancellations).
  3. Batch Settlement Generation: Aggregate captured funds into deterministic, bounded settlement files (e.g., ISO 20022 or processor-specific flat files) sent to acquiring institutions.
  4. Bank Reconciliation: Ingest daily clearing reports from acquirers, matching external settled entries against internal ledger postings and highlighting discrepancies.
  5. Immutable Double-Entry Ledger: Every balance movement must maintain debits-equal-credits consistency and store a complete audit history.

Non-Functional Requirements

  1. Strict Idempotency: Duplicate API requests, retries, or worker crashes must never double-charge or double-capture funds.
  2. Latency: p99 latency under 250ms for authorizations (excluding external processor round-trip time).
  3. Availability & Consistency: 99.999% availability for payment ingestion; strict serializability / strong consistency for balance and ledger states.
  4. Fault Recovery: Robust handling of ambiguous states (e.g., network drop after sending a request to the payment network before receiving a response).

Capacity & Constraints

  • Peak authorization throughput: 10,000 transactions per second.
  • Daily volume: 50 million total operations (authorizations + captures).
  • Settlement window: Daily settlement generation must complete within a 2-hour window, with each file capped at 500,000 records.

Key Focus Areas

  • Data Model & Ledger Schemas: Show how transaction states, idempotency keys, and debits/credits are modeled.
  • State Machine: Map out the exact payment states (AUTH_PENDING, AUTHORIZED, CAPTURE_PENDING, CAPTURED, VOIDED, SETTLED, DISCREPANCY).
  • Failure Handling: Explain how network timeouts and partial failure responses are safely resolved without duplicate charges or lost updates.
  • Settlement & Reconciliation Architecture: Describe the batch creation pipeline and report matching logic.

Hints

Hint 1

Ensure idempotency keys are stored in the same database transaction as the initial payment record creation to guarantee shared fate.

Hint 2

Model authorization as a memo hold rather than a cash movement, reserving true double-entry ledger postings for captures and settlements.

Hint 3

Use monotonically increasing sequence numbers per processor to construct deterministic batch settlement files.