| Users | Transactions per Second | Data Size (Accounts) | Latency Expectation | System Changes |
|---|---|---|---|---|
| 100 | 10-50 | 1000 accounts | <100ms | Single server, simple DB queries |
| 10,000 | 1,000-5,000 | 100K accounts | <200ms | DB indexing, caching balance results |
| 1,000,000 | 50,000-100,000 | 10M accounts | <500ms | DB sharding, read replicas, distributed cache |
| 100,000,000 | 5M+ | 1B+ accounts | <1s | Microservices, event sourcing, CQRS, multi-region deployment |
Balance calculation algorithm in LLD - Scalability & System Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
At low scale, the database query speed limits balance calculation because each calculation requires reading transaction history or account states.
As users grow, the database CPU and I/O become overwhelmed by concurrent queries.
At very large scale, network bandwidth and data consistency across shards become bottlenecks.
- Caching: Store frequently requested balances in fast cache (e.g., Redis) to reduce DB load.
- Read Replicas: Use database replicas to distribute read queries.
- Sharding: Split accounts across multiple database shards by user ID or account ID.
- Event Sourcing & CQRS: Separate write and read models to optimize balance queries.
- Horizontal Scaling: Add more application servers behind load balancers.
- Batch Processing: Precompute balances periodically instead of real-time calculation.
At 1M users with 100K TPS, assuming each balance query is 1 KB data:
- Data throughput: 100,000 requests/sec * 1 KB = ~100 MB/s network bandwidth.
- Storage: 10M accounts * 1 KB/account = ~10 GB for account data.
- DB QPS: 100K QPS likely exceeds single DB capacity (5K-10K QPS), so sharding needed.
- Cache ops: Redis can handle 100K-500K ops/sec, suitable for caching balance queries.
Start by clarifying the scale and latency requirements.
Identify the main data flow: how balances are calculated and stored.
Discuss bottlenecks at each scale and propose targeted solutions like caching and sharding.
Explain trade-offs between real-time calculation and precomputed balances.
Your database handles 1000 QPS. Traffic grows 10x to 10,000 QPS. What do you do first?
Answer: Add read replicas and implement caching to reduce load on the primary database before considering sharding or more complex solutions.
Practice
balance calculation algorithm in a financial system?Solution
Step 1: Understand the role of balance calculation
The balance calculation algorithm updates the current balance by adding credits and subtracting debits.Step 2: Compare with other options
Sorting transactions, encrypting data, and generating IDs are unrelated to balance calculation.Final Answer:
To add credits and subtract debits from an initial amount -> Option AQuick Check:
Balance calculation = add credits - subtract debits [OK]
- Confusing balance calculation with sorting or encryption
- Thinking balance calculation generates IDs
- Ignoring subtraction of debits
Solution
Step 1: Identify correct assignment syntax
In most programming languages,=assigns a value. Sobalance = 0sets balance to zero.Step 2: Check other options for errors
:=is not standard in many languages,==is a comparison, and assigning a string 'zero' is incorrect for numeric balance.Final Answer:
balance = 0 -> Option BQuick Check:
Use = for assignment, not == or := [OK]
- Using == instead of = for assignment
- Using := which is not common in many languages
- Assigning string instead of numeric zero
balance = 100
transactions = [20, -10, 30, -5]
for t in transactions:
balance += t
print(balance)What will be the printed balance?
Solution
Step 1: Calculate sum of transactions
Sum = 20 + (-10) + 30 + (-5) = 20 - 10 + 30 - 5 = 35Step 2: Add sum to initial balance
Initial balance 100 + 35 = 135Final Answer:
135 -> Option AQuick Check:
100 + (20 -10 +30 -5) = 135 [OK]
- Adding absolute values instead of signed values
- Forgetting to add initial balance
- Miscalculating sum of transactions
balance = 50
transactions = [10, -20, 15]
for t in transactions:
balance = t
print(balance)Solution
Step 1: Analyze the loop operation
Inside the loop,balance = toverwrites balance each time instead of adding.Step 2: Understand correct update
It should bebalance += tto add each transaction to balance.Final Answer:
The balance is overwritten instead of updated -> Option CQuick Check:
Use += to update balance, not = [OK]
- Using = instead of += inside loop
- Assuming transactions list is empty
- Ignoring initial balance
Solution
Step 1: Consider scalability needs
Millions of transactions require efficient processing; single updates per transaction cause bottlenecks.Step 2: Evaluate batch processing benefits
Batching with distributed processing reduces load and maintains accuracy by processing groups of transactions.Step 3: Reject other options
Single DB transactions cause slowdowns, in-memory recalculation is memory-heavy and slow, ignoring debits causes incorrect balances.Final Answer:
Batch transactions and update balance periodically with distributed processing -> Option DQuick Check:
Batch + distributed processing = scalable & accurate [OK]
- Updating balance per transaction causing bottlenecks
- Recalculating balance on every request wasting memory
- Ignoring debits leading to wrong balances
