| Users | Requests per Second (RPS) | Data Stored | System Changes |
|---|---|---|---|
| 100 users | ~10 RPS | Few KBs (fine rules, user data) | Single server, simple DB, no caching needed |
| 10,000 users | ~1,000 RPS | MBs (fine history, user profiles) | DB indexing, caching layer, load balancer |
| 1,000,000 users | ~50,000 RPS | GBs (fine records, audit logs) | DB read replicas, sharding, distributed cache, multiple app servers |
| 100,000,000 users | ~5,000,000 RPS | TBs (long term storage, analytics) | Microservices, global CDN, data partitioning, asynchronous processing |
Fine calculation in LLD - Scalability & System Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
At small scale, the database is the first bottleneck because it handles all fine calculation queries and updates. As users grow, the DB CPU and I/O get saturated.
At medium scale, application servers CPU and memory become bottlenecks due to complex fine calculation logic and concurrent requests.
At large scale, network bandwidth and data storage become bottlenecks because of heavy data transfer and large fine history storage.
- Database: Use read replicas to spread read load, connection pooling to manage DB connections, and sharding to split data by user region or ID.
- Caching: Cache frequent fine rules and recent fine calculations in Redis or Memcached to reduce DB hits.
- Application Servers: Horizontally scale by adding more servers behind a load balancer.
- Data Storage: Archive old fine records to cheaper storage to reduce DB size.
- Network: Use CDNs for static content and asynchronous processing for heavy calculations to reduce latency.
- At 10,000 users: ~1,000 RPS, DB handles ~1,000 QPS, fits in a single PostgreSQL instance with caching.
- At 1,000,000 users: ~50,000 RPS, need ~5 app servers (each 10,000 RPS), DB read replicas to handle 50,000 QPS.
- Storage: Each fine record ~1 KB, 1M users with 10 fines each = ~10 GB data.
- Bandwidth: 50,000 RPS * 1 KB = ~50 MB/s, fits in 1 Gbps network link.
Start by estimating user growth and request rates. Identify the first bottleneck (usually DB). Discuss scaling DB with replicas and caching. Then cover app server scaling and data partitioning. Finally, mention cost and latency trade-offs.
Your database handles 1000 QPS. Traffic grows 10x to 10,000 QPS. What do you do first and why?
Answer: Add read replicas and implement caching to reduce DB load before scaling app servers, because the DB is the first bottleneck.
Practice
What is the primary purpose of a fine calculation system in low-level design?
Solution
Step 1: Understand the system goal
The fine calculation system is designed to handle rule violations and compute the corresponding charges automatically.Step 2: Identify the main function
Its main function is to calculate fines based on violation details and fixed rates.Final Answer:
To automatically compute charges for rule violations -> Option AQuick Check:
Fine calculation = automatic charge computation [OK]
- Confusing fine calculation with user management
- Thinking it handles authentication
- Assuming it generates performance reports
Which of the following is the correct way to represent a fine rate for a violation type in a configuration file?
violation_fine_rates = {'speeding': 100,'parking': 50,'signal_jump': 150}
Solution
Step 1: Analyze the data structure
The example shows a dictionary mapping violation names to their fine amounts, which is clear and easy to update.Step 2: Compare with other options
Lists or strings do not map violation types to amounts directly, and booleans cannot store fine values.Final Answer:
Using a dictionary with violation types as keys and fine amounts as values -> Option CQuick Check:
Dictionary maps violation to fine [OK]
- Using lists without keys loses violation context
- Using strings cannot store amounts
- Booleans cannot represent fine values
Given the following code snippet, what will be the total fine calculated?
violation_fine_rates = {'speeding': 100, 'parking': 50}violations = ['speeding', 'parking', 'speeding']total_fine = sum(violation_fine_rates[v] for v in violations)print(total_fine)
Solution
Step 1: Calculate fine for each violation
Violations are 'speeding', 'parking', 'speeding'. Their fines are 100, 50, and 100 respectively.Step 2: Sum all fines
Total fine = 100 + 50 + 100 = 250.Final Answer:
250 -> Option DQuick Check:
100 + 50 + 100 = 250 [OK]
- Counting each violation only once
- Adding fines incorrectly
- Ignoring repeated violations
Identify the error in the following fine calculation code snippet:
violation_fine_rates = {'speeding': 100, 'parking': 50}violations = ['speeding', 'parking', 'signal_jump']total_fine = sum(violation_fine_rates[v] for v in violations)print(total_fine)
Solution
Step 1: Check dictionary keys against violations
'signal_jump' is not a key in violation_fine_rates, so accessing it causes a KeyError.Step 2: Understand error type
Attempting to access a missing key in a dictionary raises KeyError in Python.Final Answer:
KeyError occurs because 'signal_jump' is not in the rates dictionary -> Option BQuick Check:
Missing key access = KeyError [OK]
- Assuming missing keys return zero
- Confusing KeyError with SyntaxError
- Ignoring runtime errors
You are designing a fine calculation system that must support multiple violation types, each with different fine rates and possible discounts for repeat offenses. Which design approach is best?
Solution
Step 1: Identify need for flexible fine rates
Different violation types require different base fines, so a mapping structure is needed.Step 2: Incorporate discount logic
Discounts for repeat offenses require additional logic applied on top of base fines.Step 3: Choose design approach
A dictionary for base fines plus discount logic is clear, scalable, and easy to update.Final Answer:
Use a dictionary mapping violation types to base fines and add logic to apply discounts based on offense count -> Option AQuick Check:
Dictionary + discount logic = scalable design [OK]
- Ignoring violation types in fine calculation
- Hardcoding fines without flexibility
- Not handling repeat offense discounts
