Intermediate
Preview

System Design Interview

Design large-scale systems from scratch with visual diagrams.

system-design
scalability
architecture

System Design Interview


Learn to design large-scale systems from scratch with step-by-step visual diagrams and real-world examples.


What You'll Learn


  • Scalability Fundamentals: Handle millions of users and requests
  • Database Design: Choose the right database for your use case
  • Caching Strategies: Implement multi-level caching systems
  • Load Balancing: Distribute traffic across multiple servers
  • Microservices: Design distributed systems
  • Real-time Systems: Handle live data and notifications

Preview Chapter: Designing a URL Shortener


Let's design a URL shortener like bit.ly that can handle 100 million URLs per day.


Requirements


Functional Requirements:

  • Shorten long URLs
  • Redirect short URLs to original URLs
  • Custom short URLs (optional)
  • Analytics (click count, referrer, etc.)

Non-Functional Requirements:

  • High availability (99.9%)
  • Low latency (< 100ms)
  • Scalable to 100M URLs/day
  • URL should be as short as possible

Capacity Estimation


Daily URLs: 100M
URLs per second: 100M / (24 * 3600) ≈ 1,200 URLs/sec
Peak traffic: 3x average = 3,600 URLs/sec
Read/Write ratio: 100:1 (more reads than writes)
Reads per second: 3,600 * 100 = 360,000 reads/sec
Writes per second: 3,600 writes/sec

Database Schema


URLs Table:
- id (BIGINT, Primary Key)
- short_code (VARCHAR(7), Unique, Indexed)
- original_url (TEXT)
- created_at (TIMESTAMP)
- expires_at (TIMESTAMP, nullable)
- user_id (BIGINT, nullable)
- click_count (INT, default 0)

Analytics Table:
- id (BIGINT, Primary Key)
- short_code (VARCHAR(7), Foreign Key)
- ip_address (VARCHAR(45))
- user_agent (TEXT)
- referrer (TEXT)
- country (VARCHAR(2))
- created_at (TIMESTAMP)

High-Level Design


┌─────────────┐    ┌─────────────┐    ┌─────────────┐
│   Client    │───▶│ Load Balancer│───▶│   App       │
│  (Browser)  │    │             │    │  Servers    │
└─────────────┘    └─────────────┘    └─────────────┘
                                              │
                                              ▼
                                    ┌─────────────┐
                                    │   Cache      │
                                    │  (Redis)     │
                                    └─────────────┘
                                              │
                                              ▼
                                    ┌─────────────┐
                                    │  Database   │
                                    │ (PostgreSQL)│
                                    └─────────────┘

URL Encoding Strategy


Base62 Encoding:

  • Characters: a-z, A-Z, 0-9 (62 characters)
  • 7 characters = 62^7 ≈ 3.5 trillion URLs
  • Example: abc123Xhttps://short.ly/abc123X

Algorithm:

def encode_url(url_id):
    characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    result = ""
    
    while url_id > 0:
        result = characters[url_id % 62] + result
        url_id //= 62
    
    return result.zfill(7)  # Pad to 7 characters

Caching Strategy


Multi-level Caching:

1. Application Cache: In-memory cache for frequently accessed URLs

2. Redis Cache: Distributed cache for hot URLs

3. CDN: Cache static content and popular redirects


Cache Invalidation:

  • TTL-based expiration
  • LRU eviction policy
  • Write-through for new URLs

Course Structure


1. System Design Fundamentals (1 hour)

- Scalability concepts

- Load balancing strategies

- Database sharding

- Caching patterns


2. Design a URL Shortener (1 hour)

- Requirements gathering

- Capacity estimation

- High-level design

- Database schema


3. Design a Chat System (1 hour)

- Real-time messaging

- Message persistence

- Online presence

- Push notifications


4. Design a Social Media Feed (1 hour)

- Timeline generation

- Content ranking

- Real-time updates

- Media storage


5. Design a Video Streaming Platform (1 hour)

- Video upload and processing

- CDN integration

- Adaptive streaming

- Analytics


6. Advanced Topics (1 hour)

- Microservices architecture

- Event-driven systems

- Distributed consensus

- Monitoring and observability


Practice Exercises


Each chapter includes hands-on exercises:

  • Design a specific system from scratch
  • Handle edge cases and failure scenarios
  • Optimize for different constraints
  • Present your design to an interviewer

Interview Tips


  • Start with requirements: Always clarify functional and non-functional requirements
  • Think out loud: Explain your reasoning process
  • Consider trade-offs: Every design decision has pros and cons
  • Scale gradually: Start simple, then add complexity
  • Handle failures: Design for fault tolerance

What's Next


After completing this course, you'll be ready to design any system from a simple web app to a distributed microservices architecture. You'll understand the trade-offs between different approaches and how to scale systems to handle millions of users.

System Design Interview | DevDiagrams