Himanshu Kukreja
0%

Bonus Problem 1: India's UPI

The World's Largest Real-Time Payment System


๐Ÿ‡ฎ๐Ÿ‡ณ A Revolution That Changed a Nation

In 2016, India launched an experiment. Could a country of 1.4 billion people, many unbanked, leapfrog decades of payment infrastructure and go directly to real-time digital payments?

Eight years later, the answer is extraordinary.

THE NUMBERS THAT DEFINE UPI (2025)

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                                                                          โ”‚
โ”‚   DAILY TRANSACTIONS            MONTHLY TRANSACTIONS                     โ”‚
โ”‚   โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€          โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                     โ”‚
โ”‚   640+ Million                  20+ Billion                              โ”‚
โ”‚                                                                          โ”‚
โ”‚   ANNUAL TRANSACTIONS           ANNUAL VALUE                             โ”‚
โ”‚   โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€           โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                             โ”‚
โ”‚   250+ Billion                  $3.4+ Trillion (โ‚น247 Lakh Crore)         โ”‚
โ”‚                                                                          โ”‚
โ”‚   PARTICIPATING BANKS           ACTIVE USERS                             โ”‚
โ”‚   โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€            โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                             โ”‚
โ”‚   680+                          500+ Million                             โ”‚
โ”‚                                                                          โ”‚
โ”‚   AVERAGE LATENCY               SUCCESS RATE                             โ”‚
โ”‚   โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€               โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                             โ”‚
โ”‚   ~270 milliseconds             99.2%                                    โ”‚
โ”‚                                                                          โ”‚
โ”‚   GLOBAL SHARE                  COUNTRIES ACCEPTING UPI                  โ”‚
โ”‚   โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                    โ”‚
โ”‚   50% of world's                8+ (Singapore, UAE, France,              โ”‚
โ”‚   digital transactions          Nepal, Bhutan, Sri Lanka...)             โ”‚
โ”‚                                                                          โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

For context:
โ€ข UPI processes MORE transactions than Visa and Mastercard COMBINED in India
โ€ข A tea vendor in rural India uses the same system as a Fortune 500 company
โ€ข Transactions as small as โ‚น1 (1 cent) work with the same reliability as โ‚น1 Crore
โ€ข The system operates 24/7/365 with no maintenance windows

This is the system we'll design today.


The Interview Begins

You're interviewing at a fintech company. The principal architect draws on the whiteboard:

Interviewer: "India's UPI is considered one of the greatest achievements in financial technology. Countries around the world are trying to replicate it. Today, I want you to design a system like UPI from scratch."

โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—
โ•‘                                                                          โ•‘
โ•‘            Design a Real-Time Inter-Bank Payment System                  โ•‘
โ•‘                                                                          โ•‘
โ•‘   Build the infrastructure that enables instant money transfers          โ•‘
โ•‘   between ANY two bank accounts in a country using just a phone.         โ•‘
โ•‘                                                                          โ•‘
โ•‘   Requirements:                                                          โ•‘
โ•‘   โ€ข Support 500+ banks with different legacy systems                     โ•‘
โ•‘   โ€ข Handle 600+ million transactions per day                             โ•‘
โ•‘   โ€ข Complete each transaction in < 2 seconds end-to-end                  โ•‘
โ•‘   โ€ข 99.9% availability (< 9 hours downtime/year)                         โ•‘
โ•‘   โ€ข Zero tolerance for money loss (atomic transactions)                  โ•‘
โ•‘   โ€ข Work on basic smartphones with 2G/3G connectivity                    โ•‘
โ•‘   โ€ข Support both P2P (person-to-person) and P2M (person-to-merchant)     โ•‘
โ•‘                                                                          โ•‘
โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

Interviewer: "This is arguably one of the hardest system design problems โ€” you're building financial infrastructure for a nation. Take your time."


Phase 1: Requirements Clarification

You: "Before I start, let me understand the constraints better."

Your Questions

You: "First, what's the relationship between banks and this central system? Do banks connect directly, or through intermediaries?"

Interviewer: "Banks connect through the central switch โ€” NPCI in India's case. They don't talk to each other directly. The central system orchestrates everything."

You: "What about the mobile apps? Can any app connect to the system?"

Interviewer: "Apps must be approved and must partner with a bank. We call these Payment Service Providers or PSPs. PhonePe partners with Yes Bank, Google Pay with multiple banks. The app itself doesn't hold money โ€” it's just an interface."

You: "How do users identify each other? Bank account numbers are long and error-prone."

Interviewer: "Great observation. UPI solved this with Virtual Payment Addresses โ€” like an email for money. username@bankname. The system maps this to actual bank accounts."

You: "What happens if a transaction fails mid-way? Say money is debited but not credited?"

Interviewer: "This is critical. The system MUST be atomic. Either the full transaction succeeds, or it's completely rolled back. Users cannot lose money due to technical failures."

You: "What's the peak traffic pattern? Is it bursty?"

Interviewer: "Very bursty. Evening hours see 2-3x average traffic. Festival seasons like Diwali can see 5x normal load. The system must handle these gracefully."

Requirements Summary

Functional Requirements:

1. USER MANAGEMENT
   โ€ข Register users via mobile number + bank account
   โ€ข Create and manage Virtual Payment Addresses (VPAs)
   โ€ข Link multiple bank accounts to one app
   โ€ข Two-factor authentication (device binding + PIN)

2. PAYMENT OPERATIONS
   โ€ข Push payments (I send money to you)
   โ€ข Pull payments / Collect requests (I request money from you)
   โ€ข QR code payments (scan and pay)
   โ€ข Recurring payments (autopay/mandates)

3. TRANSACTION PROCESSING
   โ€ข Real-time debit from sender's bank
   โ€ข Real-time credit to receiver's bank
   โ€ข Transaction status tracking
   โ€ข Refund/reversal handling

4. BANK INTEGRATION
   โ€ข Standardized APIs for all banks
   โ€ข VPA to account resolution
   โ€ข Balance inquiry (with consent)
   โ€ข Account validation

5. SETTLEMENT
   โ€ข Net settlement between banks (periodic)
   โ€ข Reconciliation and dispute handling
   โ€ข Audit trail for compliance

Non-Functional Requirements:

SCALE
โ€ข 600+ million transactions/day
โ€ข 20+ billion transactions/month
โ€ข 500+ million active users
โ€ข 680+ participating banks

LATENCY
โ€ข End-to-end: < 2 seconds (p99)
โ€ข NPCI switch processing: < 300ms
โ€ข Bank response time: < 1 second

AVAILABILITY
โ€ข 99.9% uptime (8.7 hours downtime/year max)
โ€ข 24/7/365 operation
โ€ข No scheduled maintenance windows

CONSISTENCY
โ€ข ACID transactions (money can't be lost)
โ€ข Exactly-once semantics
โ€ข Atomic debit-credit operations

SECURITY
โ€ข End-to-end encryption
โ€ข Device binding
โ€ข Multi-factor authentication
โ€ข Fraud detection in real-time

Phase 2: Back of the Envelope Estimation

You: "Let me work through the numbers..."

Traffic Calculations

TRANSACTIONS PER SECOND

Daily transactions:           640,000,000
Seconds per day:              86,400
Average TPS:                  ~7,400 TPS

Peak multiplier:              3-5x (evenings, festivals)
Peak TPS:                     ~25,000-35,000 TPS

Per transaction, multiple operations:
โ”œโ”€โ”€ VPA resolution:           1 lookup
โ”œโ”€โ”€ Sender bank call:         1 API call
โ”œโ”€โ”€ Receiver bank call:       1 API call
โ”œโ”€โ”€ Audit logging:            1-2 writes
โ””โ”€โ”€ Notifications:            2 pushes

Effective operations/second:  ~150,000+ at peak

Data Volume

STORAGE REQUIREMENTS

Per transaction record:
โ”œโ”€โ”€ Transaction ID:           36 bytes (UUID)
โ”œโ”€โ”€ Sender VPA:               50 bytes
โ”œโ”€โ”€ Receiver VPA:             50 bytes
โ”œโ”€โ”€ Amount:                   8 bytes
โ”œโ”€โ”€ Timestamps:               16 bytes
โ”œโ”€โ”€ Status:                   4 bytes
โ”œโ”€โ”€ Bank references:          100 bytes
โ”œโ”€โ”€ Metadata:                 200 bytes
โ””โ”€โ”€ Total:                    ~500 bytes

Daily storage:
โ”œโ”€โ”€ Transactions:             640M ร— 500B = 320 GB/day
โ”œโ”€โ”€ Audit logs:               ~500 GB/day
โ””โ”€โ”€ Total:                    ~800 GB/day

Annual storage:               ~300 TB/year
7-year retention:             ~2 PB

Infrastructure Estimates

COMPUTE REQUIREMENTS

At 25,000 TPS peak, assuming each server handles 1,000 TPS:
โ”œโ”€โ”€ API servers:              25+ servers (with redundancy: 50+)
โ”œโ”€โ”€ Database:                 Clustered, sharded
โ”œโ”€โ”€ Cache:                    Distributed Redis cluster
โ””โ”€โ”€ Message queues:           High-throughput Kafka cluster

NETWORK
โ”œโ”€โ”€ Connections to 680+ banks
โ”œโ”€โ”€ Each bank: dedicated secure link
โ”œโ”€โ”€ Geographic distribution: Multiple data centers
โ””โ”€โ”€ Bandwidth: Several Gbps

Phase 3: High-Level Architecture

You: "Let me draw how UPI actually works. It's a beautiful layered architecture."

The Three-Layer Cake

UPI ARCHITECTURE: THE THREE-LAYER CAKE

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                                                                         โ”‚
โ”‚                         LAYER 1: USER INTERFACE                         โ”‚
โ”‚                         โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                         โ”‚
โ”‚                                                                         โ”‚
โ”‚    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”‚
โ”‚    โ”‚  PhonePe    โ”‚  โ”‚ Google Pay  โ”‚  โ”‚   Paytm     โ”‚  โ”‚  BHIM       โ”‚   โ”‚
โ”‚    โ”‚   (App)     โ”‚  โ”‚   (App)     โ”‚  โ”‚   (App)     โ”‚  โ”‚  (App)      โ”‚   โ”‚
โ”‚    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ”‚
โ”‚           โ”‚                โ”‚                โ”‚                โ”‚          โ”‚
โ”‚           โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜          โ”‚
โ”‚                            โ”‚                โ”‚                           โ”‚
โ”‚                            โ–ผ                โ–ผ                           โ”‚
โ”‚    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚    โ”‚                    PSP (Payment Service Providers)              โ”‚  โ”‚
โ”‚    โ”‚                                                                 โ”‚  โ”‚
โ”‚    โ”‚   Apps must partner with a bank (PSP Bank) to access UPI        โ”‚  โ”‚
โ”‚    โ”‚   PhonePe โ†’ Yes Bank    Google Pay โ†’ Multiple banks             โ”‚  โ”‚
โ”‚    โ”‚   The PSP handles: User onboarding, VPA creation, UI/UX         โ”‚  โ”‚
โ”‚    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ”‚                                   โ”‚                                     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                    โ”‚
                                    โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                                                                         โ”‚
โ”‚                         LAYER 2: NPCI SWITCH                            โ”‚
โ”‚                         โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                            โ”‚
โ”‚                                                                         โ”‚
โ”‚    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚    โ”‚                      NPCI UPI PLATFORM                          โ”‚  โ”‚
โ”‚    โ”‚                                                                 โ”‚  โ”‚
โ”‚    โ”‚   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”          โ”‚  โ”‚
โ”‚    โ”‚   โ”‚    VPA       โ”‚  โ”‚  Transaction โ”‚  โ”‚   Fraud      โ”‚          โ”‚  โ”‚
โ”‚    โ”‚   โ”‚   Mapper     โ”‚  โ”‚   Router     โ”‚  โ”‚  Detection   โ”‚          โ”‚  โ”‚
โ”‚    โ”‚   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜          โ”‚  โ”‚
โ”‚    โ”‚                                                                 โ”‚  โ”‚
โ”‚    โ”‚   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”          โ”‚  โ”‚
โ”‚    โ”‚   โ”‚  Settlement  โ”‚  โ”‚    Audit     โ”‚  โ”‚   Dispute    โ”‚          โ”‚  โ”‚
โ”‚    โ”‚   โ”‚   Engine     โ”‚  โ”‚    Trail     โ”‚  โ”‚  Resolution  โ”‚          โ”‚  โ”‚
โ”‚    โ”‚   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜          โ”‚  โ”‚
โ”‚    โ”‚                                                                 โ”‚  โ”‚
โ”‚    โ”‚   The brain of UPI: Routes transactions between banks           โ”‚  โ”‚
โ”‚    โ”‚   NPCI NEVER holds money โ€” only routes information              โ”‚  โ”‚
โ”‚    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ”‚                                   โ”‚                                     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                    โ”‚
                                    โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                                                                         โ”‚
โ”‚                         LAYER 3: BANKING LAYER                          โ”‚
โ”‚                         โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                          โ”‚
โ”‚                                                                         โ”‚
โ”‚    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”          โ”‚
โ”‚    โ”‚   SBI   โ”‚ โ”‚  HDFC   โ”‚ โ”‚  ICICI  โ”‚ โ”‚  Axis   โ”‚ โ”‚  680+   โ”‚          โ”‚
โ”‚    โ”‚  Bank   โ”‚ โ”‚  Bank   โ”‚ โ”‚  Bank   โ”‚ โ”‚  Bank   โ”‚ โ”‚  Banks  โ”‚          โ”‚
โ”‚    โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜          โ”‚
โ”‚         โ”‚           โ”‚           โ”‚           โ”‚           โ”‚               โ”‚
โ”‚         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜               โ”‚
โ”‚                               โ”‚                                         โ”‚
โ”‚                               โ–ผ                                         โ”‚
โ”‚    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚    โ”‚                    IMPS (Immediate Payment Service)             โ”‚  โ”‚
โ”‚    โ”‚                                                                 โ”‚  โ”‚
โ”‚    โ”‚   The settlement rail that actually moves money between banks   โ”‚  โ”‚
โ”‚    โ”‚   UPI transactions settle via IMPS under the hood               โ”‚  โ”‚
โ”‚    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ”‚                                                                         โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

KEY INSIGHT:
โ€ข Money flows: Bank โ†’ Bank (through IMPS)
โ€ข Information flows: App โ†’ PSP โ†’ NPCI โ†’ Banks
โ€ข NPCI is the orchestrator, not a money holder

Transaction Flow

You: "Let me trace a โ‚น500 payment from Alice to Bob..."

TRANSACTION FLOW: ALICE PAYS BOB โ‚น500

Alice's Phone                NPCI                    Banks
(PhonePe App)               Switch                  
     โ”‚                        โ”‚                        โ”‚
     โ”‚  โ‘  INITIATE           โ”‚                        โ”‚
     โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ถโ”‚                        โ”‚
     โ”‚  "Pay โ‚น500 to          โ”‚                        โ”‚
     โ”‚   bob@okaxis"          โ”‚                        โ”‚
     โ”‚  + Alice's VPA         โ”‚                        โ”‚
     โ”‚  + Encrypted PIN       โ”‚                        โ”‚
     โ”‚                        โ”‚                        โ”‚
     โ”‚                        โ”‚  โ‘ก RESOLVE VPA        โ”‚
     โ”‚                        โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ถโ”‚
     โ”‚                        โ”‚  "Who is bob@okaxis?"  โ”‚
     โ”‚                        โ”‚                        โ”‚
     โ”‚                        โ”‚  โ‘ข VPA RESPONSE       โ”‚
     โ”‚                        โ”‚โ—€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€  โ”‚
     โ”‚                        โ”‚  "Bob's account at     โ”‚
     โ”‚                        โ”‚   Axis Bank: XXXX1234" โ”‚
     โ”‚                        โ”‚                        โ”‚
     โ”‚                        โ”‚  โ‘ฃ DEBIT REQUEST      โ”‚
     โ”‚                        โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ถโ”‚
     โ”‚                        โ”‚  "Debit โ‚น500 from      โ”‚  HDFC
     โ”‚                        โ”‚   Alice at HDFC"       โ”‚  (Alice's Bank)
     โ”‚                        โ”‚                        โ”‚
     โ”‚                        โ”‚  โ‘ค DEBIT RESPONSE     โ”‚
     โ”‚                        โ”‚โ—€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€  โ”‚
     โ”‚                        โ”‚  "Debited. Ref: ABC123"โ”‚
     โ”‚                        โ”‚                        โ”‚
     โ”‚                        โ”‚  โ‘ฅ CREDIT REQUEST     โ”‚
     โ”‚                        โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ถโ”‚
     โ”‚                        โ”‚  "Credit โ‚น500 to       โ”‚  AXIS
     โ”‚                        โ”‚   Bob at Axis"         โ”‚  (Bob's Bank)
     โ”‚                        โ”‚                        โ”‚
     โ”‚                        โ”‚  โ‘ฆ CREDIT RESPONSE    โ”‚
     โ”‚                        โ”‚โ—€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€  โ”‚
     โ”‚                        โ”‚  "Credited. Ref: XYZ789"โ”‚
     โ”‚                        โ”‚                        โ”‚
     โ”‚  โ‘ง SUCCESS            โ”‚                        โ”‚
     โ”‚โ—€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€  โ”‚                        โ”‚
     โ”‚  "Payment complete!    โ”‚                        โ”‚
     โ”‚   Ref: TXN123456"      โ”‚                        โ”‚
     โ”‚                        โ”‚                        โ”‚
     โ–ผ                        โ–ผ                        โ–ผ

TOTAL TIME: < 2 seconds

PARALLEL ACTIONS:
โ€ข Audit log written at each step
โ€ข Fraud check runs during step โ‘ 
โ€ข Push notifications sent to both Alice and Bob
โ€ข Settlement record created for bank reconciliation

Phase 4: Deep Dives

Deep Dive 1: Virtual Payment Address (VPA) Resolution

Week 1 concepts: Partitioning, lookup optimization. Week 4 concepts: Caching.

You: "VPA resolution is called for EVERY transaction. With 640 million daily transactions, this lookup must be blazing fast."

The Challenge:

VPA RESOLUTION CHALLENGE

500+ million VPAs like:
โ”œโ”€โ”€ alice@okhdfc
โ”œโ”€โ”€ bob@okaxis
โ”œโ”€โ”€ merchant@paytm
โ”œโ”€โ”€ 9876543210@ybl
โ””โ”€โ”€ ...

Each VPA maps to:
โ”œโ”€โ”€ Bank code
โ”œโ”€โ”€ Account number (encrypted)
โ”œโ”€โ”€ Account holder name
โ”œโ”€โ”€ Status (active/blocked)
โ””โ”€โ”€ Metadata

Requirements:
โ”œโ”€โ”€ Lookup latency: < 10ms
โ”œโ”€โ”€ 100% accuracy (wrong mapping = money to wrong person!)
โ”œโ”€โ”€ Real-time updates (user changes bank)
โ””โ”€โ”€ Handle 50,000+ lookups/second at peak

The Solution:

VPA MAPPER ARCHITECTURE

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                                                                         โ”‚
โ”‚                          VPA RESOLUTION FLOW                            โ”‚
โ”‚                                                                         โ”‚
โ”‚                                                                         โ”‚
โ”‚    VPA: bob@okaxis                                                      โ”‚
โ”‚           โ”‚                                                             โ”‚
โ”‚           โ–ผ                                                             โ”‚
โ”‚    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                                                  โ”‚
โ”‚    โ”‚  PARSE HANDLE   โ”‚  Extract: handle="bob", suffix="okaxis"          โ”‚
โ”‚    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                                  โ”‚
โ”‚             โ”‚                                                           โ”‚
โ”‚             โ–ผ                                                           โ”‚
โ”‚    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                          โ”‚
โ”‚    โ”‚  SUFFIX LOOKUP  โ”‚โ”€โ”€โ”€โ”€โ–ถโ”‚  BANK REGISTRY  โ”‚                          โ”‚
โ”‚    โ”‚                 โ”‚     โ”‚                 โ”‚                          โ”‚
โ”‚    โ”‚ "okaxis" โ†’ Axis โ”‚     โ”‚ okhdfc โ†’ HDFC   โ”‚                          โ”‚
โ”‚    โ”‚  Bank Code      โ”‚     โ”‚ okaxis โ†’ Axis   โ”‚                          โ”‚
โ”‚    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚ paytm โ†’ Paytm   โ”‚                          โ”‚
โ”‚             โ”‚              โ”‚ ybl โ†’ Yes Bank  โ”‚                          โ”‚
โ”‚             โ”‚              โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                          โ”‚
โ”‚             โ–ผ                                                           โ”‚
โ”‚    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                                                  โ”‚
โ”‚    โ”‚   CACHE CHECK   โ”‚                                                  โ”‚
โ”‚    โ”‚   (Redis)       โ”‚                                                  โ”‚
โ”‚    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                                  โ”‚
โ”‚             โ”‚                                                           โ”‚
โ”‚      Cache Hit?                                                         โ”‚
โ”‚        โ”‚    โ”‚                                                           โ”‚
โ”‚       Yes   No                                                          โ”‚
โ”‚        โ”‚    โ”‚                                                           โ”‚
โ”‚        โ”‚    โ–ผ                                                           โ”‚
โ”‚        โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                   โ”‚
โ”‚        โ”‚  โ”‚ QUERY BANK      โ”‚โ”€โ”€โ”€โ”€โ–ถโ”‚   Axis Bank     โ”‚                   โ”‚
โ”‚        โ”‚  โ”‚ (Real-time)     โ”‚     โ”‚   VPA Database  โ”‚                   โ”‚
โ”‚        โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                   โ”‚
โ”‚        โ”‚           โ”‚                                                    โ”‚
โ”‚        โ”‚           โ”‚ Update cache                                       โ”‚
โ”‚        โ”‚           โ”‚                                                    โ”‚
โ”‚        โ–ผ           โ–ผ                                                    โ”‚
โ”‚    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                                                  โ”‚
โ”‚    โ”‚ RETURN ACCOUNT  โ”‚                                                  โ”‚
โ”‚    โ”‚ DETAILS         โ”‚                                                  โ”‚
โ”‚    โ”‚                 โ”‚                                                  โ”‚
โ”‚    โ”‚ Bank: Axis      โ”‚                                                  โ”‚
โ”‚    โ”‚ Account: ***234 โ”‚                                                  โ”‚
โ”‚    โ”‚ Name: Bob Kumar โ”‚                                                  โ”‚
โ”‚    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                                  โ”‚
โ”‚                                                                         โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

KEY DESIGN DECISIONS:

1. DISTRIBUTED OWNERSHIP
   Each bank owns their VPA namespace (suffix)
   NPCI doesn't store all VPAs โ€” banks do
   This scales naturally as banks handle their own data

2. CACHING STRATEGY
   Hot VPAs (frequently used) cached at NPCI
   TTL: 15-30 minutes
   Invalidation: Banks push updates for VPA changes

3. HANDLE UNIQUENESS
   handle@suffix is globally unique
   Banks ensure uniqueness within their namespace
   Cross-bank uniqueness handled by suffix differentiation
# vpa/resolver.py

"""
VPA Resolution Service

Maps Virtual Payment Addresses to actual bank accounts.
This is the most critical lookup in the entire system.
"""

from dataclasses import dataclass
from typing import Optional, Tuple
from datetime import datetime, timedelta
import hashlib


@dataclass
class AccountDetails:
    """Resolved account information."""
    bank_code: str
    account_number_masked: str  # Only last 4 digits visible
    account_holder_name: str
    ifsc_code: str
    is_active: bool
    verified_at: datetime


@dataclass
class VPAResolutionResult:
    """Result of VPA resolution."""
    success: bool
    account: Optional[AccountDetails] = None
    error_code: Optional[str] = None
    resolution_time_ms: float = 0
    cache_hit: bool = False


class VPAResolver:
    """
    Resolves VPAs to bank account details.
    
    Design principles:
    - Cache aggressively (VPAs don't change often)
    - Fail fast on invalid formats
    - Banks are the source of truth
    """
    
    def __init__(
        self,
        cache,           # Redis cluster
        bank_registry,   # Bank code โ†’ Bank API mapping
        bank_gateway,    # Gateway to call bank APIs
        metrics
    ):
        self.cache = cache
        self.registry = bank_registry
        self.gateway = bank_gateway
        self.metrics = metrics
        
        # Cache settings
        self.cache_ttl = timedelta(minutes=30)
        self.negative_cache_ttl = timedelta(minutes=5)
    
    async def resolve(self, vpa: str) -> VPAResolutionResult:
        """
        Resolve a VPA to account details.
        
        VPA format: handle@suffix
        Example: alice@okhdfc, 9876543210@ybl
        """
        start_time = datetime.utcnow()
        
        # Step 1: Parse and validate VPA format
        parsed = self._parse_vpa(vpa)
        if not parsed:
            return VPAResolutionResult(
                success=False,
                error_code="INVALID_VPA_FORMAT"
            )
        
        handle, suffix = parsed
        
        # Step 2: Get bank code from suffix
        bank_code = self.registry.get_bank_for_suffix(suffix)
        if not bank_code:
            return VPAResolutionResult(
                success=False,
                error_code="UNKNOWN_VPA_SUFFIX"
            )
        
        # Step 3: Check cache
        cache_key = f"vpa:{vpa.lower()}"
        cached = await self.cache.get(cache_key)
        
        if cached:
            if cached == "NOT_FOUND":
                return VPAResolutionResult(
                    success=False,
                    error_code="VPA_NOT_FOUND",
                    cache_hit=True
                )
            
            account = AccountDetails(**cached)
            return VPAResolutionResult(
                success=True,
                account=account,
                resolution_time_ms=self._elapsed_ms(start_time),
                cache_hit=True
            )
        
        # Step 4: Query the bank
        try:
            account = await self.gateway.resolve_vpa(
                bank_code=bank_code,
                handle=handle,
                suffix=suffix
            )
            
            if account:
                # Cache the result
                await self.cache.set(
                    cache_key,
                    account.__dict__,
                    ttl=self.cache_ttl
                )
                
                return VPAResolutionResult(
                    success=True,
                    account=account,
                    resolution_time_ms=self._elapsed_ms(start_time),
                    cache_hit=False
                )
            else:
                # Cache negative result (VPA doesn't exist)
                await self.cache.set(
                    cache_key,
                    "NOT_FOUND",
                    ttl=self.negative_cache_ttl
                )
                
                return VPAResolutionResult(
                    success=False,
                    error_code="VPA_NOT_FOUND",
                    resolution_time_ms=self._elapsed_ms(start_time)
                )
                
        except BankTimeoutError:
            return VPAResolutionResult(
                success=False,
                error_code="BANK_TIMEOUT"
            )
        except BankUnavailableError:
            return VPAResolutionResult(
                success=False,
                error_code="BANK_UNAVAILABLE"
            )
    
    def _parse_vpa(self, vpa: str) -> Optional[Tuple[str, str]]:
        """Parse VPA into handle and suffix."""
        if not vpa or '@' not in vpa:
            return None
        
        parts = vpa.lower().strip().split('@')
        if len(parts) != 2:
            return None
        
        handle, suffix = parts
        
        # Validate handle (alphanumeric, 3-50 chars)
        if not handle or len(handle) < 3 or len(handle) > 50:
            return None
        
        # Validate suffix (registered bank suffix)
        if not suffix or len(suffix) < 2 or len(suffix) > 20:
            return None
        
        return handle, suffix
    
    def _elapsed_ms(self, start: datetime) -> float:
        return (datetime.utcnow() - start).total_seconds() * 1000

Deep Dive 2: Atomic Transactions โ€” The Heart of Trust

Week 2 concepts: Idempotency, failure handling. Week 5 concepts: Distributed transactions, Saga pattern.

You: "The most critical requirement: money cannot disappear. If I debit Alice but fail to credit Bob, Alice must get her money back. Always."

The Challenge:

THE ATOMICITY CHALLENGE

Happy path:
  โ‘  Debit Alice (HDFC): โ‚น500     โœ“
  โ‘ก Credit Bob (Axis): โ‚น500      โœ“
  โ†’ Success!

Failure scenarios:

SCENARIO A: Credit fails after debit
  โ‘  Debit Alice: โ‚น500            โœ“ (money left Alice's account)
  โ‘ก Credit Bob: TIMEOUT          โœ— (did it go through or not?)
  โ†’ UNCERTAINTY! Alice lost โ‚น500?

SCENARIO B: Network partition
  โ‘  Debit Alice: โœ“
  โ‘ก Credit Bob: Request sent...
  โ‘ข Network dies
  โ‘ฃ We don't know the outcome!
  โ†’ UNCERTAINTY!

SCENARIO C: Duplicate request
  โ‘  User clicks "Pay" twice quickly
  โ‘ก Two debit requests sent
  โ†’ DOUBLE DEBIT! Alice loses โ‚น1000?

These scenarios CANNOT happen in a payment system.

The Solution:

UPI'S TRANSACTION STATE MACHINE

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                                                                         โ”‚
โ”‚                    TRANSACTION STATE MACHINE                            โ”‚
โ”‚                                                                         โ”‚
โ”‚                                                                         โ”‚
โ”‚                         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                                โ”‚
โ”‚                         โ”‚   CREATED    โ”‚                                โ”‚
โ”‚                         โ”‚              โ”‚                                โ”‚
โ”‚                         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                โ”‚
โ”‚                                โ”‚                                        โ”‚
โ”‚                                โ”‚ Validation passed                      โ”‚
โ”‚                                โ–ผ                                        โ”‚
โ”‚                         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                                โ”‚
โ”‚                         โ”‚   PENDING    โ”‚                                โ”‚
โ”‚                         โ”‚              โ”‚                                โ”‚
โ”‚                         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                โ”‚
โ”‚                                โ”‚                                        โ”‚
โ”‚                                โ”‚ Send to remitter bank                  โ”‚
โ”‚                                โ–ผ                                        โ”‚
โ”‚   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”          โ”‚
โ”‚   โ”‚   FAILED     โ”‚โ—€โ”€โ”€โ”€โ”€โ”€โ”‚    DEBIT     โ”‚โ”€โ”€โ”€โ”€โ”€โ–ถโ”‚   DEBITED    โ”‚          โ”‚
โ”‚   โ”‚              โ”‚      โ”‚   INITIATED  โ”‚      โ”‚              โ”‚          โ”‚
โ”‚   โ”‚ (No debit    โ”‚      โ”‚              โ”‚      โ”‚ (Money left  โ”‚          โ”‚
โ”‚   โ”‚  happened)   โ”‚      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ”‚  sender)     โ”‚          โ”‚
โ”‚   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                            โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜          โ”‚
โ”‚                                                      โ”‚                  โ”‚
โ”‚                                                      โ”‚ Send to          โ”‚
โ”‚                                                      โ”‚ beneficiary bank โ”‚
โ”‚                                                      โ–ผ                  โ”‚
โ”‚   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”          โ”‚
โ”‚   โ”‚   REVERSED   โ”‚โ—€โ”€โ”€โ”€โ”€โ”€โ”‚   CREDIT     โ”‚โ”€โ”€โ”€โ”€โ”€โ–ถโ”‚  COMPLETED   โ”‚          โ”‚
โ”‚   โ”‚              โ”‚      โ”‚  INITIATED   โ”‚      โ”‚              โ”‚          โ”‚
โ”‚   โ”‚ (Money back  โ”‚      โ”‚              โ”‚      โ”‚ (Money       โ”‚          โ”‚
โ”‚   โ”‚  to sender)  โ”‚      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ”‚  received)   โ”‚          โ”‚
โ”‚   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜             โ”‚              โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜          โ”‚
โ”‚          โ–ฒ                     โ”‚                                        โ”‚
โ”‚          โ”‚                     โ”‚ Credit timeout/failure                 โ”‚
โ”‚          โ”‚                     โ–ผ                                        โ”‚
โ”‚          โ”‚              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                                โ”‚
โ”‚          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚    DEEMED    โ”‚                                โ”‚
โ”‚            Auto-reverse โ”‚   SUCCESS    โ”‚                                โ”‚
โ”‚            after T+2    โ”‚              โ”‚                                โ”‚
โ”‚                         โ”‚ (Uncertain   โ”‚                                โ”‚
โ”‚                         โ”‚  state)      โ”‚                                โ”‚
โ”‚                         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                โ”‚
โ”‚                                                                         โ”‚
โ”‚  DEEMED SUCCESS: Bank didn't respond in time.                           โ”‚
โ”‚  Settlement happens, if credit actually failed, auto-reversal at T+2    โ”‚
โ”‚                                                                         โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
# transaction/processor.py

"""
Transaction Processing with Atomic Guarantees.

This is the most critical code in the entire system.
Money cannot be lost under any circumstances.
"""

from dataclasses import dataclass
from typing import Optional
from enum import Enum
from datetime import datetime, timedelta
import uuid


class TransactionState(Enum):
    CREATED = "created"
    PENDING = "pending"
    DEBIT_INITIATED = "debit_initiated"
    DEBITED = "debited"
    CREDIT_INITIATED = "credit_initiated"
    COMPLETED = "completed"
    FAILED = "failed"
    DEEMED_SUCCESS = "deemed_success"
    REVERSED = "reversed"


@dataclass
class Transaction:
    """A UPI transaction record."""
    txn_id: str
    sender_vpa: str
    receiver_vpa: str
    amount: int  # In paise (smallest unit)
    
    state: TransactionState
    created_at: datetime
    updated_at: datetime
    
    # Bank references
    sender_bank_ref: Optional[str] = None
    receiver_bank_ref: Optional[str] = None
    
    # For idempotency
    idempotency_key: Optional[str] = None
    
    # Reversal tracking
    reversal_initiated: bool = False
    reversal_completed: bool = False


class TransactionProcessor:
    """
    Processes UPI transactions with atomic guarantees.
    
    Key principles:
    1. IDEMPOTENCY: Same request = same result (no double-debit)
    2. ATOMICITY: Either complete success or complete rollback
    3. DURABILITY: State persisted before any bank call
    4. RECOVERABILITY: Can resume from any failure point
    """
    
    def __init__(
        self,
        db,              # Transaction database
        bank_gateway,    # Bank API gateway
        reversal_queue,  # Queue for failed transactions
        audit_log
    ):
        self.db = db
        self.gateway = bank_gateway
        self.reversal_queue = reversal_queue
        self.audit = audit_log
        
        # Timeouts
        self.debit_timeout = timedelta(seconds=30)
        self.credit_timeout = timedelta(seconds=30)
    
    async def process(
        self,
        sender_vpa: str,
        receiver_vpa: str,
        amount: int,
        idempotency_key: str
    ) -> Transaction:
        """
        Process a payment transaction.
        
        CRITICAL: This method must be idempotent.
        Same idempotency_key = same result, always.
        """
        
        # STEP 0: Check idempotency
        existing = await self.db.get_by_idempotency_key(idempotency_key)
        if existing:
            # Return existing result (no reprocessing)
            await self.audit.log("IDEMPOTENT_RETURN", existing.txn_id)
            return existing
        
        # STEP 1: Create transaction record FIRST
        txn = Transaction(
            txn_id=str(uuid.uuid4()),
            sender_vpa=sender_vpa,
            receiver_vpa=receiver_vpa,
            amount=amount,
            state=TransactionState.CREATED,
            created_at=datetime.utcnow(),
            updated_at=datetime.utcnow(),
            idempotency_key=idempotency_key
        )
        
        # Persist BEFORE any bank call
        await self.db.save(txn)
        await self.audit.log("TXN_CREATED", txn.txn_id)
        
        try:
            # STEP 2: Initiate debit
            txn.state = TransactionState.DEBIT_INITIATED
            txn.updated_at = datetime.utcnow()
            await self.db.save(txn)
            
            debit_result = await self.gateway.debit(
                vpa=sender_vpa,
                amount=amount,
                txn_ref=txn.txn_id,
                timeout=self.debit_timeout
            )
            
            if not debit_result.success:
                # Debit failed cleanly โ€” no money moved
                txn.state = TransactionState.FAILED
                txn.updated_at = datetime.utcnow()
                await self.db.save(txn)
                await self.audit.log("DEBIT_FAILED", txn.txn_id, 
                                    debit_result.error)
                return txn
            
            # STEP 3: Debit succeeded โ€” record it
            txn.state = TransactionState.DEBITED
            txn.sender_bank_ref = debit_result.bank_reference
            txn.updated_at = datetime.utcnow()
            await self.db.save(txn)
            await self.audit.log("DEBIT_SUCCESS", txn.txn_id)
            
            # STEP 4: Initiate credit
            # CRITICAL: From this point, we MUST either complete or reverse
            txn.state = TransactionState.CREDIT_INITIATED
            txn.updated_at = datetime.utcnow()
            await self.db.save(txn)
            
            credit_result = await self.gateway.credit(
                vpa=receiver_vpa,
                amount=amount,
                txn_ref=txn.txn_id,
                timeout=self.credit_timeout
            )
            
            if credit_result.success:
                # SUCCESS! Transaction complete
                txn.state = TransactionState.COMPLETED
                txn.receiver_bank_ref = credit_result.bank_reference
                txn.updated_at = datetime.utcnow()
                await self.db.save(txn)
                await self.audit.log("TXN_COMPLETED", txn.txn_id)
                return txn
            
            elif credit_result.status == "TIMEOUT":
                # UNCERTAINTY: We don't know if credit happened
                # Mark as DEEMED_SUCCESS โ€” settlement will clarify
                txn.state = TransactionState.DEEMED_SUCCESS
                txn.updated_at = datetime.utcnow()
                await self.db.save(txn)
                await self.audit.log("TXN_DEEMED_SUCCESS", txn.txn_id)
                
                # Schedule reconciliation check
                await self.reversal_queue.schedule_check(
                    txn.txn_id,
                    check_at=datetime.utcnow() + timedelta(hours=24)
                )
                return txn
            
            else:
                # Credit FAILED โ€” must reverse the debit
                await self._initiate_reversal(txn)
                return txn
                
        except Exception as e:
            # Unexpected error โ€” check state and recover
            await self.audit.log("TXN_ERROR", txn.txn_id, str(e))
            await self._handle_error(txn, e)
            raise
    
    async def _initiate_reversal(self, txn: Transaction):
        """
        Reverse a failed transaction.
        Credit the debited amount back to sender.
        """
        txn.reversal_initiated = True
        txn.updated_at = datetime.utcnow()
        await self.db.save(txn)
        await self.audit.log("REVERSAL_INITIATED", txn.txn_id)
        
        # Queue for reversal (handled by separate process)
        await self.reversal_queue.enqueue(txn.txn_id)
    
    async def process_reversal(self, txn_id: str):
        """
        Execute reversal โ€” credit money back to sender.
        Called by reversal worker.
        """
        txn = await self.db.get(txn_id)
        
        if txn.reversal_completed:
            return  # Already reversed
        
        # Credit back to sender
        reversal_result = await self.gateway.credit(
            vpa=txn.sender_vpa,
            amount=txn.amount,
            txn_ref=f"REV-{txn.txn_id}",
            timeout=self.credit_timeout
        )
        
        if reversal_result.success:
            txn.state = TransactionState.REVERSED
            txn.reversal_completed = True
            txn.updated_at = datetime.utcnow()
            await self.db.save(txn)
            await self.audit.log("REVERSAL_COMPLETED", txn.txn_id)
        else:
            # Reversal failed โ€” retry later
            # This is a critical alert scenario
            await self.audit.log("REVERSAL_FAILED", txn.txn_id,
                                reversal_result.error)
            await self.reversal_queue.schedule_retry(
                txn.txn_id,
                retry_at=datetime.utcnow() + timedelta(minutes=15)
            )

Interviewer: "What about the 'deemed success' state? That seems risky."

You: "Great catch. Here's how reconciliation handles it..."

DEEMED SUCCESS RECONCILIATION

Scenario: We debited Alice, tried to credit Bob, got TIMEOUT

At NPCI level:
โ”œโ”€โ”€ Transaction marked DEEMED_SUCCESS
โ”œโ”€โ”€ We don't know if Bob got money
โ””โ”€โ”€ Settlement file sent to banks includes this transaction

At Bank level (T+1 reconciliation):
โ”œโ”€โ”€ Bank compares settlement file with actual credits
โ”œโ”€โ”€ If credit happened: Mark as COMPLETED
โ”œโ”€โ”€ If credit NOT happened: Mark as FAILED โ†’ Auto-reversal

Timing:
โ”œโ”€โ”€ T+0: Transaction happens, deemed success
โ”œโ”€โ”€ T+1: Banks reconcile, report actual status
โ”œโ”€โ”€ T+2: NPCI updates final status
โ”œโ”€โ”€ T+2: If failed, reversal initiated automatically

This is why UPI guidelines say:
"If money is debited but not credited, it will be
automatically reversed within 5 business days"

In practice, it's usually resolved within 24-48 hours.

Deep Dive 3: Bank Integration at Scale

Week 2 concepts: Timeouts, circuit breakers. Week 3 concepts: Message queues.

You: "With 680+ banks, each with different legacy systems, bank integration is a massive challenge."

BANK INTEGRATION ARCHITECTURE

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                                                                        โ”‚
โ”‚                         NPCI โ†’ BANK GATEWAY                            โ”‚
โ”‚                                                                        โ”‚
โ”‚   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”‚
โ”‚   โ”‚                    BANK ADAPTER LAYER                          โ”‚   โ”‚
โ”‚   โ”‚                                                                โ”‚   โ”‚
โ”‚   โ”‚   Every bank exposes standard UPI APIs, but internal           โ”‚   โ”‚
โ”‚   โ”‚   implementations vary wildly. The adapter handles this.       โ”‚   โ”‚
โ”‚   โ”‚                                                                โ”‚   โ”‚
โ”‚   โ”‚   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”            โ”‚   โ”‚
โ”‚   โ”‚   โ”‚ SBI Adapter โ”‚  โ”‚ HDFC Adapterโ”‚  โ”‚ Axis Adapterโ”‚  ...       โ”‚   โ”‚
โ”‚   โ”‚   โ”‚             โ”‚  โ”‚             โ”‚  โ”‚             โ”‚            โ”‚   โ”‚
โ”‚   โ”‚   โ”‚ Handles:    โ”‚  โ”‚ Handles:    โ”‚  โ”‚ Handles:    โ”‚            โ”‚   โ”‚
โ”‚   โ”‚   โ”‚ - SBI's     โ”‚  โ”‚ - HDFC's    โ”‚  โ”‚ - Axis's    โ”‚            โ”‚   โ”‚
โ”‚   โ”‚   โ”‚   quirks    โ”‚  โ”‚   quirks    โ”‚  โ”‚   quirks    โ”‚            โ”‚   โ”‚
โ”‚   โ”‚   โ”‚ - Retry     โ”‚  โ”‚ - Retry     โ”‚  โ”‚ - Retry     โ”‚            โ”‚   โ”‚
โ”‚   โ”‚   โ”‚   logic     โ”‚  โ”‚   logic     โ”‚  โ”‚   logic     โ”‚            โ”‚   โ”‚
โ”‚   โ”‚   โ”‚ - Timeouts  โ”‚  โ”‚ - Timeouts  โ”‚  โ”‚ - Timeouts  โ”‚            โ”‚   โ”‚
โ”‚   โ”‚   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜            โ”‚   โ”‚
โ”‚   โ”‚          โ”‚                โ”‚                โ”‚                   โ”‚   โ”‚
โ”‚   โ”‚          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                   โ”‚   โ”‚
โ”‚   โ”‚                           โ”‚                                    โ”‚   โ”‚
โ”‚   โ”‚                           โ–ผ                                    โ”‚   โ”‚
โ”‚   โ”‚   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚   โ”‚
โ”‚   โ”‚   โ”‚               CIRCUIT BREAKER LAYER                     โ”‚  โ”‚   โ”‚
โ”‚   โ”‚   โ”‚                                                         โ”‚  โ”‚   โ”‚
โ”‚   โ”‚   โ”‚   Per-bank circuit breakers prevent cascade failures    โ”‚  โ”‚   โ”‚
โ”‚   โ”‚   โ”‚                                                         โ”‚  โ”‚   โ”‚
โ”‚   โ”‚   โ”‚   SBI:    [CLOSED]  โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–‘โ–‘ (healthy)                โ”‚  โ”‚   โ”‚
โ”‚   โ”‚   โ”‚   HDFC:   [CLOSED]  โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–‘ (healthy)                โ”‚  โ”‚   โ”‚
โ”‚   โ”‚   โ”‚   Axis:   [OPEN]    โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘ (failing, skip for 30s)  โ”‚  โ”‚   โ”‚
โ”‚   โ”‚   โ”‚   ICICI:  [HALF]    โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–‘โ–‘โ–‘โ–‘โ–‘ (testing recovery)       โ”‚  โ”‚   โ”‚
โ”‚   โ”‚   โ”‚                                                         โ”‚  โ”‚   โ”‚
โ”‚   โ”‚   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚   โ”‚
โ”‚   โ”‚                           โ”‚                                    โ”‚   โ”‚
โ”‚   โ”‚                           โ–ผ                                    โ”‚   โ”‚
โ”‚   โ”‚   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚   โ”‚
โ”‚   โ”‚   โ”‚              SECURE COMMUNICATION LAYER                 โ”‚  โ”‚   โ”‚
โ”‚   โ”‚   โ”‚                                                         โ”‚  โ”‚   โ”‚
โ”‚   โ”‚   โ”‚   - HTTPS with mutual TLS                               โ”‚  โ”‚   โ”‚
โ”‚   โ”‚   โ”‚   - Request/Response signing                            โ”‚  โ”‚   โ”‚
โ”‚   โ”‚   โ”‚   - Encryption of sensitive data                        โ”‚  โ”‚   โ”‚
โ”‚   โ”‚   โ”‚   - IP whitelisting                                     โ”‚  โ”‚   โ”‚
โ”‚   โ”‚   โ”‚   - Dedicated leased lines to major banks               โ”‚  โ”‚   โ”‚
โ”‚   โ”‚   โ”‚                                                         โ”‚  โ”‚   โ”‚
โ”‚   โ”‚   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚   โ”‚
โ”‚   โ”‚                                                                โ”‚   โ”‚
โ”‚   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ”‚
โ”‚                                                                        โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
# bank/gateway.py

"""
Bank Gateway: Unified interface to 680+ banks.

Each bank is different. This gateway provides a
consistent interface while handling per-bank quirks.
"""

from dataclasses import dataclass
from typing import Dict, Optional
from datetime import datetime, timedelta
from enum import Enum


class CircuitState(Enum):
    CLOSED = "closed"    # Normal operation
    OPEN = "open"        # Bank failing, don't try
    HALF_OPEN = "half"   # Testing if bank recovered


@dataclass
class BankConfig:
    """Configuration for a bank."""
    bank_code: str
    endpoint: str
    timeout_ms: int = 30000
    
    # Circuit breaker settings
    failure_threshold: int = 5
    recovery_timeout_s: int = 30
    
    # Bank-specific quirks
    requires_padding: bool = False
    amount_in_rupees: bool = False  # Some banks want rupees, not paise
    legacy_xml_format: bool = False


class BankGateway:
    """
    Gateway for all bank operations.
    """
    
    def __init__(
        self,
        http_client,
        bank_configs: Dict[str, BankConfig],
        metrics
    ):
        self.http = http_client
        self.configs = bank_configs
        self.metrics = metrics
        
        # Circuit breakers per bank
        self.circuits: Dict[str, CircuitBreaker] = {
            code: CircuitBreaker(config)
            for code, config in bank_configs.items()
        }
    
    async def debit(
        self,
        bank_code: str,
        account_ref: str,
        amount: int,
        txn_ref: str
    ) -> 'BankResponse':
        """
        Debit an account at a bank.
        
        Amount is in paise (smallest unit).
        """
        config = self.configs.get(bank_code)
        if not config:
            return BankResponse(
                success=False,
                error_code="UNKNOWN_BANK"
            )
        
        # Check circuit breaker
        circuit = self.circuits[bank_code]
        if not circuit.can_execute():
            self.metrics.increment("bank_circuit_open", bank_code)
            return BankResponse(
                success=False,
                error_code="BANK_CIRCUIT_OPEN"
            )
        
        try:
            # Build request (handle bank-specific formats)
            request = self._build_debit_request(
                config, account_ref, amount, txn_ref
            )
            
            # Make the call
            start = datetime.utcnow()
            response = await self.http.post(
                config.endpoint + "/debit",
                json=request,
                timeout=config.timeout_ms / 1000
            )
            latency = (datetime.utcnow() - start).total_seconds() * 1000
            
            # Record metrics
            self.metrics.record_latency("bank_debit", bank_code, latency)
            
            # Parse response
            result = self._parse_response(config, response)
            
            if result.success:
                circuit.record_success()
            else:
                circuit.record_failure()
            
            return result
            
        except TimeoutError:
            circuit.record_failure()
            self.metrics.increment("bank_timeout", bank_code)
            return BankResponse(
                success=False,
                error_code="TIMEOUT",
                status="TIMEOUT"
            )
        except Exception as e:
            circuit.record_failure()
            self.metrics.increment("bank_error", bank_code)
            return BankResponse(
                success=False,
                error_code="BANK_ERROR",
                error_message=str(e)
            )
    
    def _build_debit_request(
        self,
        config: BankConfig,
        account_ref: str,
        amount: int,
        txn_ref: str
    ) -> dict:
        """Build bank-specific request format."""
        
        # Handle amount format (paise vs rupees)
        if config.amount_in_rupees:
            amount_value = amount / 100
        else:
            amount_value = amount
        
        if config.legacy_xml_format:
            # Some old banks still use XML
            return {
                "xml_payload": self._build_xml(
                    account_ref, amount_value, txn_ref
                )
            }
        
        return {
            "account_reference": account_ref,
            "amount": amount_value,
            "transaction_reference": txn_ref,
            "timestamp": datetime.utcnow().isoformat()
        }


class CircuitBreaker:
    """
    Circuit breaker for bank connections.
    
    Prevents cascade failures when a bank is down.
    """
    
    def __init__(self, config: BankConfig):
        self.config = config
        self.state = CircuitState.CLOSED
        self.failure_count = 0
        self.last_failure: Optional[datetime] = None
        self.success_count = 0
    
    def can_execute(self) -> bool:
        """Check if we can make a request to this bank."""
        if self.state == CircuitState.CLOSED:
            return True
        
        if self.state == CircuitState.OPEN:
            # Check if recovery timeout passed
            if self.last_failure:
                elapsed = (datetime.utcnow() - self.last_failure).total_seconds()
                if elapsed > self.config.recovery_timeout_s:
                    self.state = CircuitState.HALF_OPEN
                    self.success_count = 0
                    return True
            return False
        
        if self.state == CircuitState.HALF_OPEN:
            return True
        
        return False
    
    def record_success(self):
        """Record a successful request."""
        if self.state == CircuitState.HALF_OPEN:
            self.success_count += 1
            if self.success_count >= 3:  # 3 successes to close
                self.state = CircuitState.CLOSED
                self.failure_count = 0
        else:
            self.failure_count = 0
    
    def record_failure(self):
        """Record a failed request."""
        self.failure_count += 1
        self.last_failure = datetime.utcnow()
        
        if self.state == CircuitState.HALF_OPEN:
            # Back to open
            self.state = CircuitState.OPEN
            self.success_count = 0
        elif self.failure_count >= self.config.failure_threshold:
            self.state = CircuitState.OPEN

Deep Dive 4: Security โ€” The Trust Foundation

Week 9 concepts: Security, authentication, fraud detection.

You: "UPI handles โ‚น20+ trillion monthly. Security isn't optional โ€” it's existential."

UPI SECURITY ARCHITECTURE

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                                                                         โ”‚
โ”‚                      MULTI-LAYER SECURITY                               โ”‚
โ”‚                                                                         โ”‚
โ”‚  LAYER 1: DEVICE BINDING                                                โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                                              โ”‚
โ”‚  โ€ข UPI PIN is bound to specific device                                  โ”‚
โ”‚  โ€ข Device fingerprint (IMEI, hardware ID)                               โ”‚
โ”‚  โ€ข SIM binding (mobile number verification)                             โ”‚
โ”‚  โ€ข If device changes, re-registration required                          โ”‚
โ”‚                                                                         โ”‚
โ”‚  LAYER 2: TWO-FACTOR AUTHENTICATION                                     โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                                   โ”‚
โ”‚  Factor 1: Something you HAVE                                           โ”‚
โ”‚  โ€ข The registered mobile device                                         โ”‚
โ”‚  โ€ข The SIM card with registered number                                  โ”‚
โ”‚                                                                         โ”‚
โ”‚  Factor 2: Something you KNOW                                           โ”‚
โ”‚  โ€ข 4-6 digit UPI PIN (set by user)                                      โ”‚
โ”‚  โ€ข PIN encrypted on device, never transmitted in clear                  โ”‚
โ”‚                                                                         โ”‚
โ”‚  LAYER 3: ENCRYPTION                                                    โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                                                  โ”‚
โ”‚  โ€ข HTTPS/TLS for all communication                                      โ”‚
โ”‚  โ€ข UPI PIN encrypted using PBKDF2 (600,000 iterations)                  โ”‚
โ”‚  โ€ข PIN verification in bank's HSM (Hardware Security Module)            โ”‚
โ”‚  โ€ข End-to-end encryption for sensitive data                             โ”‚
โ”‚                                                                         โ”‚
โ”‚  LAYER 4: TRANSACTION SIGNING                                           โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                                           โ”‚
โ”‚  โ€ข Each transaction signed with digital signature                       โ”‚
โ”‚  โ€ข Prevents tampering in transit                                        โ”‚
โ”‚  โ€ข Non-repudiation for disputes                                         โ”‚
โ”‚                                                                         โ”‚
โ”‚  LAYER 5: REAL-TIME FRAUD DETECTION                                     โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                                     โ”‚
โ”‚  โ€ข Velocity checks (too many transactions too fast)                     โ”‚
โ”‚  โ€ข Amount anomaly detection                                             โ”‚
โ”‚  โ€ข Geo-location checks (impossible travel)                              โ”‚
โ”‚  โ€ข Behavioral analysis                                                  โ”‚
โ”‚  โ€ข Block list matching                                                  โ”‚
โ”‚                                                                         โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

INFRASTRUCTURE SECURITY

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                                                                         โ”‚
โ”‚  NPCI DATA CENTERS                                                      โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                                                      โ”‚
โ”‚                                                                         โ”‚
โ”‚  โ€ข Tier-IV certified (99.995% uptime)                                   โ”‚
โ”‚  โ€ข Chennai + Hyderabad (geographically separated)                       โ”‚
โ”‚  โ€ข FIPS 140-2 Level 3 certified HSMs                                    โ”‚
โ”‚  โ€ข Active-Active configuration                                          โ”‚
โ”‚  โ€ข N+N redundancy                                                       โ”‚
โ”‚                                                                         โ”‚
โ”‚  โ€ข Physical security:                                                   โ”‚
โ”‚    โ”œโ”€โ”€ Biometric access control                                         โ”‚
โ”‚    โ”œโ”€โ”€ 24/7 security personnel                                          โ”‚
โ”‚    โ”œโ”€โ”€ CCTV surveillance                                                โ”‚
โ”‚    โ””โ”€โ”€ Man-trap entries                                                 โ”‚
โ”‚                                                                         โ”‚
โ”‚  BANK CONNECTIONS                                                       โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                                                       โ”‚
โ”‚  โ€ข Dedicated leased lines (not public internet)                         โ”‚
โ”‚  โ€ข Mutual TLS authentication                                            โ”‚
โ”‚  โ€ข IP whitelisting                                                      โ”‚
โ”‚  โ€ข Regular security audits                                              โ”‚
โ”‚                                                                         โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
# security/fraud_detector.py

"""
Real-Time Fraud Detection for UPI.

Must decide in < 50ms whether to allow a transaction.
"""

from dataclasses import dataclass
from typing import List, Tuple
from datetime import datetime, timedelta


@dataclass
class FraudSignals:
    """Signals used for fraud detection."""
    user_id: str
    device_id: str
    amount: int
    receiver_vpa: str
    
    # Velocity
    txn_count_1hr: int
    txn_count_24hr: int
    total_amount_24hr: int
    
    # Device
    is_new_device: bool
    device_age_days: int
    
    # Behavioral
    is_new_receiver: bool
    typical_amount: int
    typical_time_of_day: List[int]
    current_hour: int
    
    # Location
    device_location: str
    usual_locations: List[str]


class FraudDetector:
    """
    Real-time fraud detection.
    
    Must be FAST (< 50ms) and ACCURATE (low false positives).
    """
    
    def __init__(self, ml_model, rules_engine, blocklist):
        self.model = ml_model
        self.rules = rules_engine
        self.blocklist = blocklist
    
    async def evaluate(
        self,
        signals: FraudSignals
    ) -> Tuple[str, float, List[str]]:
        """
        Evaluate fraud risk.
        
        Returns: (decision, confidence, triggered_rules)
        decision: "ALLOW", "BLOCK", "STEP_UP"
        """
        triggered_rules = []
        
        # RULE 1: Blocklist check (instant)
        if await self.blocklist.is_blocked(signals.user_id):
            return "BLOCK", 1.0, ["USER_BLOCKED"]
        
        if await self.blocklist.is_blocked(signals.device_id):
            return "BLOCK", 1.0, ["DEVICE_BLOCKED"]
        
        # RULE 2: Velocity checks
        if signals.txn_count_1hr > 10:
            triggered_rules.append("HIGH_VELOCITY_1HR")
        
        if signals.txn_count_24hr > 50:
            triggered_rules.append("HIGH_VELOCITY_24HR")
        
        if signals.total_amount_24hr > 100000_00:  # โ‚น1 lakh in paise
            triggered_rules.append("HIGH_AMOUNT_24HR")
        
        # RULE 3: Amount anomaly
        if signals.amount > signals.typical_amount * 10:
            triggered_rules.append("AMOUNT_ANOMALY")
        
        # RULE 4: New device
        if signals.is_new_device:
            triggered_rules.append("NEW_DEVICE")
            if signals.amount > 10000_00:  # > โ‚น10,000 on new device
                triggered_rules.append("HIGH_AMOUNT_NEW_DEVICE")
        
        # RULE 5: Unusual time
        if signals.current_hour not in signals.typical_time_of_day:
            triggered_rules.append("UNUSUAL_TIME")
        
        # RULE 6: Location check
        if signals.device_location not in signals.usual_locations:
            triggered_rules.append("UNUSUAL_LOCATION")
        
        # ML model for complex patterns
        ml_score = await self.model.predict(signals)
        
        # Decision logic
        if ml_score > 0.9 or len(triggered_rules) > 3:
            return "BLOCK", ml_score, triggered_rules
        
        if ml_score > 0.7 or len(triggered_rules) > 1:
            # Step-up: require additional verification
            return "STEP_UP", ml_score, triggered_rules
        
        return "ALLOW", 1 - ml_score, triggered_rules

Phase 5: Scaling and Edge Cases

Interviewer: "What happens during Diwali when everyone is sending money?"

You: "UPI handles 5x spikes during festivals. Here's how..."

Festival Traffic Management

DIWALI SCALE (5X NORMAL TRAFFIC)

Normal day:
โ”œโ”€โ”€ ~640 million transactions
โ”œโ”€โ”€ ~7,400 average TPS
โ”œโ”€โ”€ ~25,000 peak TPS

Diwali:
โ”œโ”€โ”€ ~3 billion transactions
โ”œโ”€โ”€ ~35,000 average TPS
โ”œโ”€โ”€ ~150,000+ peak TPS
โ””โ”€โ”€ Concentrated in evening hours (7 PM - 11 PM)

PREPARATION (Weeks Before):
โ”œโ”€โ”€ Pre-scale infrastructure to 3x capacity
โ”œโ”€โ”€ Warm up caches with popular VPAs
โ”œโ”€โ”€ Notify banks to scale their systems
โ”œโ”€โ”€ Extended support staff on standby
โ”œโ”€โ”€ Runbooks reviewed and tested

DURING THE EVENT:
โ”œโ”€โ”€ Auto-scaling triggers at 60% capacity
โ”œโ”€โ”€ Non-critical features disabled (promotional notifications)
โ”œโ”€โ”€ Enhanced monitoring (5-second alert intervals)
โ”œโ”€โ”€ War room with all bank representatives
โ””โ”€โ”€ Direct escalation paths to bank CTOs

GRACEFUL DEGRADATION:
If overwhelmed:
โ”œโ”€โ”€ Prioritize smaller transactions (more users served)
โ”œโ”€โ”€ Rate limit per-user (max 5 txn/minute)
โ”œโ”€โ”€ Queue non-urgent operations (mandate registrations)
โ””โ”€โ”€ Return "Try again in few minutes" vs hard failure

Critical Edge Cases

EDGE CASE 1: Bank System Down

Problem: SBI (largest bank, 30% market share) goes down
Impact: 30% of transactions fail

Solution:
โ”œโ”€โ”€ Circuit breaker opens for SBI immediately
โ”œโ”€โ”€ Return clear error: "SBI temporarily unavailable"
โ”œโ”€โ”€ Pending transactions queued (if bank supports retry)
โ”œโ”€โ”€ Status page updated
โ”œโ”€โ”€ Auto-retry when circuit closes
โ””โ”€โ”€ Transactions involving SBI gracefully rejected

EDGE CASE 2: NPCI Switch Partial Failure

Problem: One NPCI data center fails
Impact: 50% capacity lost

Solution:
โ”œโ”€โ”€ Active-Active setup in Chennai and Hyderabad
โ”œโ”€โ”€ Traffic automatically routes to healthy DC
โ”œโ”€โ”€ DNS TTL is low (60 seconds) for fast failover
โ”œโ”€โ”€ Data replicated synchronously between DCs
โ””โ”€โ”€ RPO: 0 (no data loss), RTO: < 30 seconds

EDGE CASE 3: Duplicate Transaction Request

Problem: User's app times out, they retry, but first request succeeded
Impact: Double debit

Solution:
โ”œโ”€โ”€ Every transaction has idempotency key
โ”œโ”€โ”€ Generated on client: device_id + timestamp + amount + receiver
โ”œโ”€โ”€ NPCI checks idempotency before processing
โ”œโ”€โ”€ If duplicate: return original result
โ””โ”€โ”€ No double processing possible

EDGE CASE 4: April 2025 Outage (Real Incident)

What happened:
โ”œโ”€โ”€ Banks were calling "Check Transaction Status" API excessively
โ”œโ”€โ”€ Some banks called for old transactions repeatedly
โ”œโ”€โ”€ NPCI didn't enforce rate limits on this API
โ”œโ”€โ”€ API flooded, entire system degraded

Lesson learned:
โ”œโ”€โ”€ Rate limit ALL APIs, not just transaction APIs
โ”œโ”€โ”€ Enforce guidelines at NPCI firewall, not just bank side
โ”œโ”€โ”€ Separate critical path APIs from status check APIs
โ””โ”€โ”€ Circuit breaker for misbehaving banks

Phase 6: Monitoring and Operations

You: "For a system processing โ‚น20 trillion monthly, monitoring isn't optional."

Key Metrics Dashboard

UPI OPERATIONS DASHBOARD

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                                                                         โ”‚
โ”‚  REAL-TIME HEALTH                                                       โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                                                      โ”‚
โ”‚                                                                         โ”‚
โ”‚  Transaction Rate        Success Rate           Latency (p99)           โ”‚
โ”‚  โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–‘       โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–‘            โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–‘โ–‘              โ”‚
โ”‚  7,842 TPS              99.2%                  847 ms                   โ”‚
โ”‚  (Target: 10K)          (Target: 99.0%)        (Target: 1000ms)         โ”‚
โ”‚                                                                         โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€  โ”‚
โ”‚                                                                         โ”‚
โ”‚  BANK HEALTH MATRIX                                                     โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                                                     โ”‚
โ”‚                                                                         โ”‚
โ”‚  Bank          TPS      Success   Latency    Circuit    Issues          โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€          โ”‚
โ”‚  SBI           2,341    98.7%     423ms      CLOSED     Minor lag       โ”‚
โ”‚  HDFC          1,856    99.8%     287ms      CLOSED     Healthy         โ”‚
โ”‚  ICICI         1,234    99.5%     312ms      CLOSED     Healthy         โ”‚
โ”‚  Axis          987      94.2%     892ms      HALF-OPEN  HIGH LATENCY    โ”‚
โ”‚  Kotak         654      99.1%     345ms      CLOSED     Healthy         โ”‚
โ”‚  Yes Bank      543      99.4%     298ms      CLOSED     Healthy         โ”‚
โ”‚  ...                                                                    โ”‚
โ”‚                                                                         โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€  โ”‚
โ”‚                                                                         โ”‚
โ”‚  ALERTS (Last 1 hour)                                                   โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                                                   โ”‚
โ”‚                                                                         โ”‚
โ”‚  ๐Ÿ”ด 14:32 - Axis Bank latency > 800ms (CRITICAL)                        โ”‚
โ”‚  ๐ŸŸก 14:28 - SBI error rate 1.3% (Warning)                               โ”‚
โ”‚  ๐ŸŸข 14:15 - Axis Bank circuit half-open (Info)                          โ”‚
โ”‚  ๐ŸŸข 13:45 - Traffic spike +20% (Auto-scaled)                            โ”‚
โ”‚                                                                         โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

SLOs for UPI

UPI SERVICE LEVEL OBJECTIVES

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                                                                         โ”‚
โ”‚  SLO 1: TRANSACTION SUCCESS RATE                                        โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                                       โ”‚
โ”‚  Target: 99.0% of transactions succeed                                  โ”‚
โ”‚  Measurement: Successful / Total (excluding user errors)                โ”‚
โ”‚  Current: 99.2%                                                         โ”‚
โ”‚                                                                         โ”‚
โ”‚  Exclusions:                                                            โ”‚
โ”‚  โ€ข Insufficient balance (user error)                                    โ”‚
โ”‚  โ€ข Wrong PIN (user error)                                               โ”‚
โ”‚  โ€ข Account blocked (compliance)                                         โ”‚
โ”‚                                                                         โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€  โ”‚
โ”‚                                                                         โ”‚
โ”‚  SLO 2: END-TO-END LATENCY                                              โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                                             โ”‚
โ”‚  Target: 99% of transactions complete in < 2 seconds                    โ”‚
โ”‚  Measurement: Time from request received to response sent               โ”‚
โ”‚  Current: p99 = 1.2 seconds                                             โ”‚
โ”‚                                                                         โ”‚
โ”‚  Breakdown:                                                             โ”‚
โ”‚  โ€ข NPCI processing: < 300ms                                             โ”‚
โ”‚  โ€ข Bank response (each): < 800ms                                        โ”‚
โ”‚  โ€ข Network overhead: < 200ms                                            โ”‚
โ”‚                                                                         โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€  โ”‚
โ”‚                                                                         โ”‚
โ”‚  SLO 3: AVAILABILITY                                                    โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                                                   โ”‚
โ”‚  Target: 99.9% uptime                                                   โ”‚
โ”‚  Measurement: (Total time - Downtime) / Total time                      โ”‚
โ”‚  Allowed downtime: 8.7 hours/year                                       โ”‚
โ”‚                                                                         โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€  โ”‚
โ”‚                                                                         โ”‚
โ”‚  SLO 4: MONEY SAFETY                                                    โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                                                  โ”‚
โ”‚  Target: 100% of debited amounts credited or reversed                   โ”‚
โ”‚  Measurement: No money stuck > 5 business days                          โ”‚
โ”‚  Current: 99.99% resolved within 24 hours                               โ”‚
โ”‚                                                                         โ”‚
โ”‚  This is NON-NEGOTIABLE. Error budget = 0                               โ”‚
โ”‚                                                                         โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Interview Conclusion

Interviewer: "Impressive depth. A few rapid-fire questions:"

Interviewer: "Why didn't India just adopt an existing system like Visa/Mastercard?"

You: "Three reasons:

  1. Cost: Card networks charge 1.5-3% per transaction. UPI is nearly free.
  2. Inclusion: Cards need credit checks, plastic production, POS terminals. UPI needs only a phone.
  3. Control: Critical financial infrastructure shouldn't depend on foreign companies.

The result: UPI enabled the chai vendor to accept digital payments for a โ‚น10 tea."

Interviewer: "What's the biggest technical achievement of UPI?"

You: "Interoperability without centralized money holding. NPCI routes transactions but never touches the money. This means:

  • No counterparty risk (NPCI can't go bankrupt with your money)
  • Banks remain the regulated entities
  • Scales infinitely (NPCI is just a switch)
  • Any app works with any bank

This architecture is why countries worldwide are studying UPI."

Interviewer: "If you were to improve UPI today, what would you change?"

You: "Based on the April 2025 outage:

  1. Stricter rate limiting at NPCI level, not trusting banks to self-regulate
  2. Better isolation between critical transaction APIs and status check APIs
  3. More granular circuit breakers โ€” per-API, not just per-bank
  4. Chaos engineering โ€” regularly test failure scenarios in production"

Summary: Concepts Applied from 10-Week Course

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                                                                         โ”‚
โ”‚              CONCEPTS FROM 10-WEEK COURSE IN UPI DESIGN                 โ”‚
โ”‚                                                                         โ”‚
โ”‚  WEEK 1: DATA AT SCALE                                                  โ”‚
โ”‚  โ”œโ”€โ”€ Partitioning: VPAs partitioned by bank suffix                      โ”‚
โ”‚  โ”œโ”€โ”€ Replication: Multi-DC active-active setup                          โ”‚
โ”‚  โ””โ”€โ”€ Read optimization: VPA caching at NPCI level                       โ”‚
โ”‚                                                                         โ”‚
โ”‚  WEEK 2: FAILURE-FIRST DESIGN                                           โ”‚
โ”‚  โ”œโ”€โ”€ Timeouts: Strict timeouts for bank calls (30s)                     โ”‚
โ”‚  โ”œโ”€โ”€ Circuit breakers: Per-bank failure isolation                       โ”‚
โ”‚  โ”œโ”€โ”€ Idempotency: Transaction idempotency keys                          โ”‚
โ”‚  โ””โ”€โ”€ Retries: Smart retry with exponential backoff                      โ”‚
โ”‚                                                                         โ”‚
โ”‚  WEEK 3: MESSAGING & ASYNC                                              โ”‚
โ”‚  โ”œโ”€โ”€ Transactional outbox: Audit logging                                โ”‚
โ”‚  โ”œโ”€โ”€ Dead letter queues: Failed reversal handling                       โ”‚
โ”‚  โ””โ”€โ”€ Event streaming: Transaction events for reconciliation             โ”‚
โ”‚                                                                         โ”‚
โ”‚  WEEK 4: CACHING                                                        โ”‚
โ”‚  โ”œโ”€โ”€ VPA resolution caching                                             โ”‚
โ”‚  โ”œโ”€โ”€ Bank configuration caching                                         โ”‚
โ”‚  โ””โ”€โ”€ Negative caching: Non-existent VPAs                                โ”‚
โ”‚                                                                         โ”‚
โ”‚  WEEK 5: CONSISTENCY & COORDINATION                                     โ”‚
โ”‚  โ”œโ”€โ”€ Distributed transactions: Debit-then-credit with rollback          โ”‚
โ”‚  โ”œโ”€โ”€ State machine: Transaction lifecycle management                    โ”‚
โ”‚  โ””โ”€โ”€ Exactly-once semantics: Idempotency guarantees                     โ”‚
โ”‚                                                                         โ”‚
โ”‚  WEEK 9: SECURITY & COMPLIANCE                                          โ”‚
โ”‚  โ”œโ”€โ”€ Multi-factor authentication: Device + PIN                          โ”‚
โ”‚  โ”œโ”€โ”€ Encryption: PBKDF2, HSM-based PIN verification                     โ”‚
โ”‚  โ”œโ”€โ”€ Fraud detection: Real-time ML scoring                              โ”‚
โ”‚  โ””โ”€โ”€ Audit trail: Complete transaction logging                          โ”‚
โ”‚                                                                         โ”‚
โ”‚  WEEK 10: PRODUCTION READINESS                                          โ”‚
โ”‚  โ”œโ”€โ”€ SLOs: Success rate, latency, availability targets                  โ”‚
โ”‚  โ”œโ”€โ”€ Observability: Per-bank health dashboards                          โ”‚
โ”‚  โ”œโ”€โ”€ Capacity planning: Festival traffic handling                       โ”‚
โ”‚  โ””โ”€โ”€ Incident management: April 2025 outage learnings                   โ”‚
โ”‚                                                                         โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Why UPI Matters

โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—
โ•‘                                                                         โ•‘
โ•‘                    WHY UPI IS A MARVEL OF ENGINEERING                   โ•‘
โ•‘                                                                         โ•‘
โ•‘  SCALE                                                                  โ•‘
โ•‘  โ”€โ”€โ”€โ”€โ”€                                                                  โ•‘
โ•‘  โ€ข 50% of world's digital transactions                                  โ•‘
โ•‘  โ€ข More than Visa + Mastercard combined (in India)                      โ•‘
โ•‘  โ€ข 640+ million transactions DAILY                                      โ•‘
โ•‘                                                                         โ•‘
โ•‘  INCLUSION                                                              โ•‘
โ•‘  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                                                              โ•‘
โ•‘  โ€ข Works on โ‚น3,000 smartphones                                          โ•‘
โ•‘  โ€ข Works on 2G networks                                                 โ•‘
โ•‘  โ€ข โ‚น1 transactions viable (no minimums)                                 โ•‘
โ•‘  โ€ข Enabled 300 million+ previously unbanked Indians                     โ•‘
โ•‘                                                                         โ•‘
โ•‘  COST                                                                   โ•‘
โ•‘  โ”€โ”€โ”€โ”€                                                                   โ•‘
โ•‘  โ€ข Zero cost to consumers                                               โ•‘
โ•‘  โ€ข Near-zero cost to small merchants                                    โ•‘
โ•‘  โ€ข Saved billions in card network fees                                  โ•‘
โ•‘                                                                         โ•‘
โ•‘  INNOVATION                                                             โ•‘
โ•‘  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                                                             โ•‘
โ•‘  โ€ข VPA system (email for money)                                         โ•‘
โ•‘  โ€ข Interoperable (any app, any bank)                                    โ•‘
โ•‘  โ€ข Open standard (countries can adopt)                                  โ•‘
โ•‘  โ€ข Built on existing bank infrastructure                                โ•‘
โ•‘                                                                         โ•‘
โ•‘  GLOBAL IMPACT                                                          โ•‘
โ•‘  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                                                          โ•‘
โ•‘  โ€ข 8+ countries accepting UPI                                           โ•‘
โ•‘  โ€ข 10+ countries studying UPI for adoption                              โ•‘
โ•‘  โ€ข Model for BIS cross-border payment initiatives                       โ•‘
โ•‘                                                                         โ•‘
โ•‘  โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•   โ•‘
โ•‘                                                                         โ•‘
โ•‘  "UPI proved that with the right architecture, a developing nation      โ•‘
โ•‘   can leapfrog decades of financial infrastructure and build            โ•‘
โ•‘   something the developed world envies."                                โ•‘
โ•‘                                                                         โ•‘
โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

Self-Assessment Checklist

After studying this case study, you should be able to:

Architecture:

  • Explain the three-layer UPI architecture (Apps โ†’ NPCI โ†’ Banks)
  • Design a VPA resolution system with caching
  • Implement atomic transactions with rollback capability

Distributed Systems:

  • Handle partial failures in multi-party transactions
  • Implement circuit breakers for unreliable dependencies
  • Design idempotency for payment systems

Scale:

  • Calculate infrastructure needs for billion-transaction systems
  • Plan for bursty traffic (festivals, events)
  • Implement graceful degradation under load

Security:

  • Design multi-factor authentication for payments
  • Implement real-time fraud detection
  • Understand HSM-based PIN verification

Operations:

  • Define meaningful SLOs for payment systems
  • Monitor multi-party systems (NPCI + 680 banks)
  • Learn from production incidents (April 2025 outage)

Sources

Statistics and Data:

Architecture and Technical Details:

Infrastructure and Security:

Settlement and Operations:

Outage Analysis (April 2025):


Further Reading

Official Documentation:

Engineering Blogs and Technical Deep Dives:

Research Papers and Reports:

  • BIS Papers No. 152: Faster Digital Payments - Global and Regional Perspectives (India Chapter)
  • NPCI White Papers: Available on NPCI website
  • RBI Annual Reports: Digital payments statistics and trends

News and Industry Analysis:

Video Resources:

  • NPCI YouTube Channel: Official explainers and announcements
  • System Design Interview Videos: Search "UPI System Design" on YouTube

Books:

  • "Designing Data-Intensive Applications" by Martin Kleppmann - Foundational concepts
  • "System Design Interview" by Alex Xu - Interview preparation with similar patterns

Related Systems to Study:

  • PIX (Brazil): Similar instant payment system
  • FedNow (USA): US real-time payment system
  • SEPA Instant (Europe): European instant payments

End of Bonus Problem 1: India's UPI

"A payment system that serves a billion people, handles trillions in transactions, and costs nothing to use. This is what engineering at scale looks like."