KatInterview

Building a Fault-Tolerant Payment System with Zero Duplicates

System DesignModel answer
MediumModel answerSystem Designdistributed-systemspaymentsidempotency

Last updated

Design a payment platform that prevents duplicate charges through idempotency, handles gateway timeouts safely, and maintains financial correctness.

Problem Context

You're building the core transaction engine for a SaaS billing platform. This engine receives payment requests from multiple sources—checkout flows, subscription renewals, invoice processors—and executes them against third-party payment processors like Stripe or PayPal. The system must record every financial event in an immutable ledger.

The central challenge: guarantee that network failures, retries, and async notifications never cause a customer to be charged twice.

Your Task

Design a system that enforces strict idempotency while handling real-world failure modes: requests that time out mid-flight, webhooks that arrive out of order, and scenarios where you cannot determine if a charge succeeded.

Functional Goals

  • Duplicate Prevention: When a client retries the same payment request, return the original outcome without re-executing the charge
  • Timeout Resolution: When a gateway call times out, safely discover the actual outcome without risking a duplicate charge
  • Concurrent Event Handling: Process both synchronous API responses and asynchronous webhook notifications without creating race conditions
  • Audit Trail: Keep an append-only record of all state transitions that supports forensic analysis
  • Recovery Mechanisms: Support automated reconciliation against external settlement files and provide guarded manual intervention for edge cases

Performance & Reliability Targets

  • Correctness First: No duplicate charges under any combination of failures—network partitions, process crashes, database failovers
  • Availability: 99.99% uptime for accepting new payment requests
  • Throughput: Handle 100 million daily transactions (roughly 1,200 TPS average, 5,000 TPS peak)
  • Monetary Precision: Support exact decimal arithmetic across multiple currencies without rounding errors

Design Scope

Your design should address:

  1. Request Interface & Storage Schema: How clients submit payments with idempotency keys, how you fingerprint requests, what payment states exist, and how the ledger is structured
  2. Concurrency Strategy: How you prevent two concurrent requests with the same key from both executing, and how state transitions are protected
  3. External Gateway Protocol: How you isolate timeout scenarios, implement safe retry logic, and query for transaction status
  4. Webhook Integration: How you handle payment status updates arriving through async callbacks, possibly before or after your synchronous call returns
  5. Reconciliation & Manual Operations: How you verify correctness against daily settlement reports and what safeguards exist around operator-initiated fixes

Focus on the mechanisms that guarantee correctness under failure. You can assume standard infrastructure (load balancers, databases, message queues) exists.

Hints

Hint 1

Think about what makes a payment request unique and how to enforce 'execute once' semantics at the database level

Hint 2

Consider what happens when your gateway HTTP call times out: did the charge go through or not? How do you find out safely?

Hint 3

Webhooks and API responses might report different outcomes for the same payment. Which source of truth wins, and how do you handle conflicts?