Design a Payment Processing System

System Design
Hard
Uber
148.7K views

Design a service to handle transactional payments (e.g., Stripe's core API). Focus on atomicity (ACID properties), two-phase commits, and idempotency.

Why Interviewers Ask This

Interviewers at Uber ask this to evaluate your ability to design distributed systems where financial integrity is non-negotiable. They specifically assess your understanding of ACID properties, how to prevent double-spending through idempotency, and your strategy for handling partial failures using two-phase commits. The goal is to see if you can balance high availability with strict consistency in a real-world, high-throughput environment.

How to Answer This Question

1. Clarify requirements: Define scope (e.g., rider pays driver), latency needs, and consistency levels. Uber values high availability, but money transactions require strong consistency. 2. High-level architecture: Propose a client-server model with an API gateway, payment service, and ledger database. Mention load balancing and caching strategies. 3. Core data modeling: Design tables for Transactions and Ledgers, emphasizing unique keys for idempotency. 4. Deep dive into atomicity: Explain the Two-Phase Commit (2PC) protocol or Saga pattern for distributed transactions across services like the wallet and external gateways. Discuss how you ensure all-or-nothing execution. 5. Idempotency implementation: Detail how to use unique request IDs stored in a database to prevent duplicate charges if retries occur due to network glitches. 6. Failure scenarios: Describe handling timeouts, deadlocks, and reconciliation processes for failed payments.

Key Points to Cover

  • Explicitly define how idempotency keys prevent duplicate charges in retry scenarios
  • Demonstrate clear understanding of Two-Phase Commit mechanics for distributed atomicity
  • Explain the trade-off between consistency and availability when handling financial data
  • Propose a concrete data schema that enforces uniqueness constraints on transaction IDs
  • Describe a fallback mechanism or reconciliation process for handling long-running failures

Sample Answer

To design a robust payment system similar to what Uber handles, I would start by defining the core constraint: money cannot be lost or created out of thin air. First, I'd establish a high-level architecture where the Payment Service acts as the central orchestrator. It receives requests via an API Gateway, which validates input and generates a unique idempotency key for every transaction attempt. For data consistency, I would implement a distributed ledger using a relational database that supports ACID transactions. When a ride completes, the system must debit the rider's wallet and credit the driver's wallet atomically. Since these might reside in different logical partitions, I would employ a Two-Phase Commit protocol. In Phase One, the coordinator prepares the transaction on both sides; only if both acknowledge readiness does it proceed to Phase Two to commit. If either fails, the entire operation rolls back instantly. Idempotency is critical here because network retries are inevitable. I would store the idempotency key in a unique index within the database. If a duplicate request arrives with the same key, the system checks the status. If the transaction was already processed successfully, it returns the original response without executing logic again. This prevents double-charging riders. Finally, I would add a background reconciliation job to scan for any stuck transactions and resolve them automatically, ensuring the system remains consistent even during partial outages.

Common Mistakes to Avoid

  • Ignoring idempotency entirely, which leads to potential double-charging issues during network retries
  • Suggesting eventual consistency for the core transaction step instead of immediate strong consistency
  • Failing to mention how to handle distributed deadlocks or coordinator failures during the commit phase
  • Overlooking the need for audit logs or a reconciliation service to detect and fix state drift

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 150 System Design questionsBrowse all 57 Uber questions