KatInterview

Building a Two-Phase Transaction Processing Platform

System DesignModel answer
HardModel answerSystem Designpaymentsdistributed-systemsconsistency

Last updated

Design a service that processes both instant payments and deferred-settlement transactions with reserved funds, ensuring correctness under failures.

Building a Two-Phase Transaction Processing Platform

You need to architect a transaction processing service supporting two payment patterns: instant transactions and two-phase reserved-then-settled transactions.

The service manages an internal credit system where merchants can reserve customer funds first, then finalize those reservations later—either individually or in bulk at day's end. Reservations that aren't finalized must be cancelable.

Core Operations

Your API must provide:

  • charge(user_id, amount): Execute an immediate fund transfer
  • authorize_hold(user_id, amount): Lock funds without transferring them yet
  • capture_hold(hold_id): Complete a previously reserved transaction
  • release_hold(hold_id): Cancel a reservation and return funds

System Properties

  • Idempotency: Duplicate requests must not create multiple charges or reservations
  • Consistency: User balances must accurately distinguish between spendable and locked funds
  • Reliability: Every financial operation must be traceable; the system must recover correctly from mid-operation crashes
  • Throughput: Handle 1,000 operations per second with daily batch jobs processing millions of items

What to Design

  1. Storage Schema: Tables and fields for tracking transactions, reservations, and state progressions
  2. Service Architecture: Component responsibilities and interaction patterns (synchronous vs. asynchronous)
  3. Batch Processing: The mechanism for end-of-day settlement ensuring every item is processed exactly once
  4. Uncertainty Handling: Your approach when an external payment provider call times out with unknown outcome

Hints

Hint 1

Think about how to prevent the same reservation from being both captured and released simultaneously

Hint 2

Consider what happens if your settlement batch crashes halfway through—how do you resume without double-processing?

Hint 3

A timeout from an external service means you don't know if the operation succeeded or failed; treating it as either could lose money