| Scale | Users | Orders per Second | Menu Items | Data Size | System Changes |
|---|---|---|---|---|---|
| Small | 100 users | 10 orders/sec | 100 items | MBs | Single server, simple DB, no caching |
| Medium | 10,000 users | 1,000 orders/sec | 1,000 items | GBs | DB read replicas, caching menu, load balancer |
| Large | 1,000,000 users | 50,000 orders/sec | 10,000 items | TBs | Sharded DB, distributed cache, multiple app servers |
| Very Large | 100,000,000 users | 5,000,000 orders/sec | 100,000+ items | Petabytes | Microservices, global CDN, event-driven architecture, data partitioning |
Restaurant, Menu, Order classes 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 order writes and menu reads. As users grow, the DB CPU and disk I/O limit throughput. The application server can handle more connections, but DB queries slow down.
- Database Read Replicas: Use replicas to serve menu reads and reduce load on primary DB.
- Caching: Cache menu data in memory (e.g., Redis) to reduce DB hits.
- Horizontal Scaling: Add more app servers behind a load balancer to handle more users.
- Sharding: Partition orders by restaurant or region to distribute DB load.
- Event-Driven Architecture: Use message queues to process orders asynchronously at large scale.
- CDN: Use CDN to serve static menu images and reduce bandwidth.
- At 10,000 orders/sec, DB needs to handle ~10,000 writes/sec plus reads.
- Menu data size grows with items; caching reduces DB storage cost.
- Network bandwidth must support order data and menu images; estimate 1 Gbps for medium scale.
- Storage for orders grows linearly; archiving old orders reduces DB size.
Start by describing the system components and their roles. Then discuss expected traffic and data growth. Identify the first bottleneck logically (usually DB). Propose scaling solutions step-by-step, explaining why each helps. Mention trade-offs and cost considerations. Keep answers structured and clear.
Your database handles 1000 QPS. Traffic grows 10x to 10,000 QPS. What do you do first?
Answer: Add read replicas and implement caching for menu data to reduce DB load. Also, consider connection pooling and optimize queries before scaling vertically.
Practice
Solution
Step 1: Understand the role of Menu class
The Menu class is designed to store food items and their prices, acting as the restaurant's catalog.Step 2: Differentiate from other classes
Order tracks customer requests, Restaurant manages overall operations, Customer represents the diner. Only Menu holds items and prices.Final Answer:
Menu -> Option BQuick Check:
Menu = items and prices [OK]
- Confusing Order with Menu
- Thinking Restaurant holds item prices
- Assuming Customer stores menu data
Solution
Step 1: Identify instance method usage
Adding an item to a Menu instance uses the instance method, so calling menu.addItem(...) is correct.Step 2: Eliminate incorrect syntax
Menu.add(...) suggests a static method which is unlikely; insertItem is not standard; addItem(menu, ...) is procedural, not OOP style.Final Answer:
menu.addItem('Pizza', 12.99) -> Option AQuick Check:
Instance method call = menu.addItem(...) [OK]
- Using static method call instead of instance method
- Confusing method names
- Calling functions outside class context
menu = Menu()
menu.addItem('Burger', 5.0)
menu.addItem('Fries', 2.5)
order = Order(menu)
order.addItem('Burger', 2)
order.addItem('Fries', 3)
total = order.calculateTotal()Solution
Step 1: Calculate cost for each item
Burger price is 5.0, quantity 2 -> 5.0 * 2 = 10.0; Fries price is 2.5, quantity 3 -> 2.5 * 3 = 7.5.Step 2: Sum the costs
Total cost = 10.0 + 7.5 = 17.5.Final Answer:
17.5 -> Option AQuick Check:
(5*2)+(2.5*3) = 17.5 [OK]
- Adding quantities instead of multiplying by price
- Forgetting to multiply price by quantity
- Mixing up item prices
Solution
Step 1: Understand the role of validation
Order should verify items exist in Menu to avoid invalid orders.Step 2: Identify consequence of missing check
Without checking, Order can contain items not on Menu, causing errors or confusion.Final Answer:
Order may include items not available in the Menu -> Option CQuick Check:
Missing validation = invalid items in Order [OK]
- Assuming automatic price updates
- Thinking Order rejects items by default
- Confusing system behavior with unrelated effects
Solution
Step 1: Identify need for unique order tracking
Each order must have a unique ID to distinguish between multiple customers' orders.Step 2: Ensure scalability and data integrity
Storing orders in a centralized database with concurrency control allows multiple orders simultaneously without conflicts.Final Answer:
Use unique order IDs and store orders in a centralized database with concurrency control -> Option DQuick Check:
Unique IDs + concurrency = scalable order handling [OK]
- Ignoring concurrency issues
- Using single list causing data overwrite
- Not persisting orders leads to data loss
