Tackling System Design Interview Problems
Capacity Estimation
Capacity estimation can guide decisions about scalability, database design, and caching, but only if it contributes meaningfully to your design. Avoid wasting time on unnecessary or overly detailed estimations.
Instead, focus on areas where capacity metrics directly inform decisions. For example:
- Database Scaling: Use capacity estimation to decide the number of shards or replicas needed based on query volume and storage growth.
- Caching Requirements: Estimate traffic volumes and read-to-write ratios to determine cache size and placement.
- Message Queue Configuration: Estimate events per second to configure tools like Kafka or RabbitMQ.
- Load Balancer Sizing: Estimate peak RPS to configure load balancers effectively.
The key is to use capacity estimation strategically, focusing on areas that influence design decisions or address identified bottlenecks. Let’s explore capacity estimation through examples for the Short URL Service and Twitter.
Traffic Assumptions
- 200 RPS for creating short URLs.
- 20,000 RPS for redirecting short URLs to long URLs.
Storage Requirements
Assume each URL mapping includes the following fields:
- Long URL ()
- Short URL ()
- Creation time ()
- Created by user ID ()
- Expiration time ()
Each entry totals bytes, which we round up to for simplicity.
- Daily Storage:
- Five-Year Storage:
Adding buffer for growth, we estimate of total storage over five years.
Insights for Design
- A single relational database like MySQL or PostgreSQL could handle this scale with read replicas for redirect lookups.
- If traffic grows beyond expectations, a distributed NoSQL database like DynamoDB or Cassandra may be required.
- Caching frequently accessed redirects in Redis could significantly reduce database load.
Traffic Assumptions
- 500M DAU (Daily Active Users)
- Each user tweets twice per day, resulting in 1B tweets/day.
- Each user views 100 tweets/day on average.
- At peak, 20% of DAU (~100M users) interact with the system simultaneously.
Storage Requirements
Each tweet includes:
- Tweet ID ()
- Created by (user ID, )
- Posted time ()
- Content (140 bytes, rounded up to with metadata)
- Media link (optional, average )
- Number of likes ()
- Hashtags (variable, average )
- Users mentioned (variable, average )
We assume each tweet requires 512 bytes on average.
- Daily Storage:
- Two-Year Storage:
Adding buffer for growth, we estimate of storage for two years.
Insights for Design
- A distributed database is required.
- Tweets should be partitioned based on user ID to balance storage across nodes.
- Cache popular tweets in Redis to optimize read performance for frequently viewed timelines.
It's not always necessary to perform capacity estimation at the beginning of the design process. Sometimes, it makes more sense to do it when the need arises, depending on the stage of your design. For example, if you're addressing a scalability challenge and deciding whether to introduce a new component or optimize an existing one, capacity estimation can provide the data needed to make an informed decision.