Quick Calculations in System Design

Sep 4, 2024

Back of the Envelope Math in System Design

Introduction

  • Back of the envelope math is a quick, approximate calculation.
  • Used for sanity-checking designs.
  • Absolute accuracy isn’t crucial; aim for within an order of magnitude.

Applications of Back of the Envelope Math

  • Helps quickly assess system requirements:
    • Example: Web service needs 1 million requests/sec.
      • Each server handles 10,000 requests/sec.
      • Need ~100 web servers with load balancing.
    • Example: Database needs 10 queries/sec at peak.
      • Single server sufficient initially; no sharding or caching needed.

Key Metrics to Estimate

  • Requests per second (service level).
  • Queries per second (database level).

Common Inputs for Calculation

  1. Daily Active Users (DAU):
    • Obtainable directly or as a percentage of Monthly Active Users (MAU).
  2. Usage per DAU:
    • Percentage of DAUs using a specific feature/service.
  3. Scaling Factor:
    • Peaks and valleys in usage across the day.
    • Example: Google Maps peaks during commute hours.

Example Calculation: Tweets per Second

  • Assumption:
    • 300 million MAU on Twitter.
    • 50% use daily (150 million DAU).
    • 25% make tweets, averaging 0.5 tweets/DAU.
    • Morning peak causes traffic to double.
  • Calculation:
    • Formula: (150 million DAU x 0.5 tweets/DAU x 2 scale factor) / 86,400 seconds.
    • Result: ~1,500 tweets/sec.

Simplifying Calculations

  • Use scientific notation to reduce errors:
    • Example: 150 million = 1.5 x 10^8.
    • Seconds in a day = ~10^5.
  • Group powers of ten and other numbers separately:
    • Example: 1.5 x 0.5 x 2 = 1.5, 10^8 / 10^5 = 10^3.

Handy Conversions

  • Memorize key conversions:
    • Example: 10^12 = trillion or terabyte.
    • 50 TB = 5 x 10^13.

Example Calculation: Multimedia Storage for Tweets

  • Assumptions:
    • 150 million tweets/day.
    • 10% contain pictures (100 KB each).
    • 1% contain videos (100 MB each).
    • 3 copies and 5-year retention.
  • Picture Storage Calculation:
    • Formula: 150 million x 0.1 x 100 KB x 400 days x 5 years x 3 copies.
    • Result: ~9 petabytes.
  • Video Storage Calculation:
    • Videos are 1000x larger than pictures.
    • 1/10th as popular.
    • Total: 100 x 9 PB = 900 PB.

Conclusion

  • Back of the envelope math is critical in system design.
  • Precision isn’t vital; close approximations suffice.
  • Useful for quick validation of design decisions.
  • Further learning available through books and newsletters.