Problem Statement
Design an end-to-end payment processing platform that handles transactions at scale. The system should be reliable, consistent, and fault-tolerant.
Think of building the backend for a service like Stripe or Razorpay.
Requirements
Functional:
- Accept payment requests (amount, currency, source, destination)
- Process payments through various payment gateways
- Handle payment status tracking (initiated, processing, completed, failed, refunded)
- Support retries for failed payments
- Provide transaction history and receipts
Non-Functional:
- Consistency: A payment must never be charged twice (idempotency)
- Reliability: No data loss even during system failures
- Latency: Sub-second response for payment initiation
- Scale: Handle 10K+ transactions per second
- Auditability: Full audit trail for compliance
What the Interviewer Expects
- Request flow: Client → API Gateway → Payment Service → Gateway Adapter → External Payment Provider → Callback/Webhook
- Idempotency: Every request carries an idempotency key. Store in DB before processing. Duplicate requests return cached result.
- State machine: Payment transitions:
INITIATED → PROCESSING → COMPLETED/FAILED. No invalid transitions allowed. - Database choices: Postgres for transactions (ACID), Redis for caching + rate limiting, Kafka for async event processing.
- Failure handling:
- Gateway timeout → retry with exponential backoff
- Ambiguous response → reconciliation job checks with gateway later
- Service crash mid-transaction → recovery from last known state
- Retry mechanisms: Idempotent retries with the same key. Max retry count. Dead letter queue for permanently failed transactions.
- Monitoring: Real-time dashboard for success rates, latency p50/p99, failure alerts.
Follow-ups
- How do you handle partial failures in a multi-step payment (hold → charge → settle)?
- How would you design the refund flow? What are the edge cases?
- How do you handle currency conversion at scale?
- How would you implement fraud detection in the payment pipeline without adding latency?
- How do you ensure compliance (PCI-DSS)? What data can/cannot be stored?