| Users / Vehicles | Parking Requests | Strategy Complexity | Storage & Data | Response Time |
|---|---|---|---|---|
| 100 vehicles | ~10 requests/min | Simple: single strategy, few rules | Small in-memory data | Fast, negligible delay |
| 10,000 vehicles | ~1,000 requests/min | Multiple strategies, moderate rules | Database for parking spots, vehicle info | Needs caching for speed |
| 1,000,000 vehicles | ~100,000 requests/min | Complex strategies, dynamic rules | Distributed DB, real-time updates | Load balancing, caching essential |
| 100,000,000 vehicles | ~10,000,000 requests/min | Highly complex, AI-assisted strategies | Sharded DB, geo-partitioning | Multi-region load balancing, CDN for static data |
Parking strategy pattern in LLD - Scalability & System Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
At small scale, the database becomes the first bottleneck because it handles all parking spot availability and vehicle data queries. As requests grow, the DB query load increases beyond a single instance's capacity.
- Read Replicas: Use read replicas to distribute read queries for parking spot availability.
- Caching: Cache frequently accessed data like parking spot status to reduce DB load.
- Horizontal Scaling: Add more application servers behind a load balancer to handle increased requests.
- Sharding: Partition the database by geographic zones or parking lot IDs to reduce single DB load.
- Asynchronous Processing: Use message queues for non-critical updates to smooth spikes.
- Geo-Distributed Systems: Deploy services closer to users to reduce latency.
- At 10,000 vehicles: ~1,000 requests/min = ~17 requests/sec.
- At 1,000,000 vehicles: ~100,000 requests/min = ~1,666 requests/sec.
- Storage: Each parking spot record ~1KB; 1M spots = ~1GB storage.
- Bandwidth: Assuming 1KB per request/response, 1,666 req/sec = ~1.6MB/s bandwidth.
- Network: 1 Gbps network can handle up to ~125MB/s, sufficient for this scale.
Start by explaining the basic parking strategy pattern and its components. Then discuss how load grows with users and what breaks first. Propose clear, stepwise scaling solutions tied to bottlenecks. Use real numbers and simple analogies like parking lots filling up and needing more attendants or zones.
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 direct DB load before scaling vertically or sharding.
Practice
Parking Strategy Pattern in a parking lot system?Solution
Step 1: Understand the role of strategy pattern
The strategy pattern lets you swap different algorithms easily without changing the main code.Step 2: Apply this to parking system context
In parking, it means you can change how spots are found without rewriting the whole system.Final Answer:
To allow different algorithms for finding parking spots without changing the main system -> Option CQuick Check:
Strategy pattern = flexible parking spot finding [OK]
- Confusing strategy pattern with data storage
- Thinking it controls hardware like gates
- Mixing it with payment processing logic
Solution
Step 1: Identify interface syntax in OOP
Interfaces define method signatures without implementation, e.g.,interface ParkingStrategy { findSpot(vehicle): Spot; }.Step 2: Compare options
interface ParkingStrategy { findSpot(vehicle): Spot; } correctly uses interface with method signature. Others are class, function, or object, not interface.Final Answer:
interface ParkingStrategy { findSpot(vehicle): Spot; } -> Option DQuick Check:
Interface = method signature only [OK]
- Using class instead of interface for strategy
- Defining functions outside interface context
- Confusing object creation with interface definition
findSpot(vehicle) is called using NearestParkingStrategy?
class NearestParkingStrategy {
findSpot(vehicle) {
return "Nearest spot found";
}
}
class RandomParkingStrategy {
findSpot(vehicle) {
return "Random spot found";
}
}
const strategy = new NearestParkingStrategy();
console.log(strategy.findSpot('car'));Solution
Step 1: Identify which strategy instance is used
The code creates an instance ofNearestParkingStrategyand callsfindSpot.Step 2: Check method output for that class
NearestParkingStrategy.findSpotreturns "Nearest spot found".Final Answer:
"Nearest spot found" -> Option AQuick Check:
Instance method output = "Nearest spot found" [OK]
- Confusing which strategy instance is created
- Assuming random strategy is used
- Expecting undefined or error without reason
ParkingLot from using different parking strategies correctly?
class ParkingLot {
constructor() {
this.strategy = null;
}
setStrategy(strategy) {
this.strategy = strategy;
}
park(vehicle) {
return this.strategy.findSpot(vehicle);
}
}
const lot = new ParkingLot();
lot.park('car');Solution
Step 1: Analyze object initialization and method calls
TheParkingLotis created withstrategy = null. No strategy is set before callingpark.Step 2: Understand consequence of null strategy
Callingthis.strategy.findSpot(vehicle)whenstrategyis null causes an error.Final Answer:
No strategy is set before calling park, causing an error -> Option AQuick Check:
Null strategy causes error on method call [OK]
- Thinking park method logic is wrong
- Assuming strategy should be a list
- Ignoring null initialization problem
Solution
Step 1: Identify need for flexibility and scalability
Supporting multiple vehicle types and strategies requires flexible design without code duplication.Step 2: Apply strategy pattern with runtime selection
Using one ParkingLot class with strategy interface allows swapping strategies dynamically based on vehicle type.Step 3: Evaluate other options
Create separate parking lot classes for each vehicle type and hardcode the strategy inside each duplicates code, C ignores strategy pattern, D uses poor global state management.Final Answer:
Use a single ParkingLot class with a strategy interface; implement different strategies and select based on vehicle type at runtime -> Option BQuick Check:
Strategy pattern + runtime selection = best design [OK]
- Duplicating classes per vehicle type
- Ignoring strategy pattern benefits
- Using global variables instead of OOP
