Design a Payment Service
Problem Statement
Design a Payment Service that enables users to make secure, reliable, and fast digital payments for online transactions (e.g., e-commerce orders, subscriptions, or digital content).
The system should handle payment processing, refunds, transaction history, and integration with multiple payment gateways (like Razorpay, Stripe, PayPal, etc.) while maintaining high availability, security, and compliance with financial standards.
🎯 Objective
Build a scalable, fault-tolerant, and secure payment processing platform that can:
✅ Functional Requirements
1. User & Merchant Management
2. Payment Initiation
3. Payment Authorization & Processing
4. Payment Confirmation
5. Refunds & Reconciliation
6. Transaction History
7. Notifications
8. Security & Compliance
⚙️ Non-Functional Requirements
| Attribute | Target |
| ------------------- | ---------------------------------------- |
| **Scalability** | Handle 100K+ TPS during peak load |
| **Availability** | 99.99% uptime |
| **Latency** | < 500 ms for internal operations |
| **Consistency** | Strong consistency for payment states |
| **Security** | PCI-DSS, GDPR, and encryption compliance |
| **Fault Tolerance** | Retry mechanisms and redundant gateways |
| **Observability** | End-to-end tracing for each transaction |
🧩 Core Components
| Component | Responsibility |
| -------------------------------- | --------------------------------------------------- |
| **API Gateway** | Entry point for merchants and apps |
| **Payment Orchestrator Service** | Routes and manages payment workflow |
| **Gateway Adapter Service** | Integrates with multiple external gateways |
| **Transaction Service** | Manages states and metadata of each transaction |
| **Notification Service** | Sends alerts and webhooks |
| **Reconciliation Service** | Verifies payment status with banks |
| **Refund Service** | Handles refund processing and tracking |
| **Ledger Service** | Maintains financial transactions and audit logs |
| **Auth & Tokenization Service** | Manages user tokens, encryption, and authentication |
🧱 High-Level Architecture
1
2 +------------------------+
3 | Merchant / User App |
4 +-----------+------------+
5 |
6 v
7 +---------+---------+
8 | API Gateway |
9 +---------+----------+
10 |
11 +-----------------------------------+
12 | Payment Backend |
13 |-----------------------------------|
14 | |
15 | +-----------------------------+ |
16 | | Payment Orchestrator | |
17 | +-----------------------------+ |
18 | | | |
19 | | | |
20 | +---v---+ +---v---+ |
21 | | Gateway| | Ledger| |
22 | | Adapter| | Service| |
23 | +---+----+ +---+----+ |
24 | | | |
25 | +---v----+ +---v----+ |
26 | | Refund | | Reconcile| |
27 | +---+-----+ +---+-----+ |
28 +------|-------------|----------------+
29 | |
30 v v
31 +--------------+ +-------------+
32 | Payment APIs | | Bank / PSPs |
33 | (e.g., Stripe,| | UPI, Razorpay|
34 | PayPal) | | Gateways) |
35 +--------------+ +-------------+
36 |
37 v
38 +----------------------+
39 | Storage & Data Layer |
40 | (Postgres, Redis, |
41 | Kafka, S3, Vault) |
42 +----------------------+🧾 Data Model (Simplified)
Transaction Table
| Field | Description |
| ------------ | -------------------------------------- |
| txn_id | Unique transaction ID |
| user_id | Payer |
| merchant_id | Merchant |
| amount | Transaction amount |
| currency | ISO code (INR, USD, etc.) |
| status | INITIATED / SUCCESS / FAILED / PENDING |
| payment_mode | CARD / UPI / WALLET |
| gateway_id | External gateway |
| created_at | Initiation timestamp |
| updated_at | Status update timestamp |
Refund Table
| Field | Description |
| ------------ | ------------------------------ |
| refund_id | Unique refund ID |
| txn_id | Linked transaction |
| amount | Refund amount |
| status | INITIATED / COMPLETED / FAILED |
| initiated_at | Timestamp |
| completed_at | Timestamp |
Ledger Table
| Field | Description |
| ------------- | --------------------- |
| entry_id | Unique ID |
| txn_id | Transaction reference |
| type | Debit / Credit |
| amount | Transaction amount |
| balance_after | Current balance |
| created_at | Timestamp |
⚡ Scalability & Performance Considerations
| Challenge | Solution |
| ----------------------- | ------------------------------------------- |
| **Gateway downtime** | Multi-gateway failover and fallback routing |
| **Duplicate requests** | Idempotency tokens per transaction |
| **Concurrency control** | Optimistic locking for transactions |
| **Throughput spikes** | Kafka-based async processing and retries |
| **Read scalability** | Redis caching for transaction lookups |
| **Data partitioning** | Shard transactions by merchant or region |
🔐 Security & Compliance
📊 Observability & Monitoring
⚙️ Tech Stack (Example)
| Layer | Technology |
| --------------------------- | ------------------------------- |
| **Frontend / Merchant SDK** | React, Flutter SDKs |
| **Backend** | Java Spring Boot / Go / Node.js |
| **Database** | PostgreSQL / Aurora / DynamoDB |
| **Queue** | Kafka / RabbitMQ |
| **Cache** | Redis |
| **API Gateway** | Kong / NGINX / AWS API Gateway |
| **Storage** | S3 (for receipts, reports) |
| **Security** | Vault, JWT, OAuth2 |
| **Monitoring** | Prometheus + Grafana |
🚨 Failure & Recovery
| Failure | Handling |
| ------------------------ | ------------------------------ |
| Gateway timeout | Retry with exponential backoff |
| Duplicate transaction | Idempotency key check |
| Refund mismatch | Periodic reconciliation |
| Network partition | Queue retry mechanism |
| Merchant callback failed | Async webhook retry |
💡 Possible Extensions
✅ End Goal
Design a highly available, fault-tolerant, and secure Payment Service that processes millions of transactions daily, supports multiple payment modes, integrates with external gateways, and ensures financial consistency and compliance across the entire payment lifecycle.