Interview OS
Back to System Design

System Design

URL shortener

Mid level · full staged walkthrough

Mid

Architecture

Mid
ClientEdgeServicesStorageAnalyticsshortenresolvemissclicksClientAPI / LBWrite svcID genRead svcredirectCache / CDNKV storekey → URLKafkaWarehouse
ClientEdgeServiceDataAsync

Solution, step by step

  1. 1

    Functional requirements

    • Shorten a long URL to a short key
    • Redirect a short key to the original URL
    • Optional: custom aliases, expiry, click analytics
  2. 2

    Non-functional requirements

    • Read-heavy (~100:1 read:write)
    • Redirect latency < 50ms p99
    • 99.99% availability for redirects
    • Links are effectively permanent (high durability)
  3. 3

    Capacity & estimation

    • ~100M new URLs/month → ~40 writes/s avg, ~400/s peak
    • Reads ≈ 100× writes → ~40K reads/s peak
    • 5 years ≈ 6B URLs; ~500 bytes each → ~3 TB
    • Short key: base62, 7 chars → 62^7 ≈ 3.5T keys (ample)
  4. 4

    Preliminary design

    • Generate a unique ID, base62-encode it to a 7-char key
    • Store key → long URL in a KV store
    • On read, look up and 301/302 redirect
  5. 5

    Final architecture

    • Stateless API behind a load balancer; separate read and write paths
    • Distributed ID generator (snowflake / counter ranges) to avoid collisions
    • KV store (DynamoDB/Redis) partitioned by key; replicas for read scale
    • CDN + edge cache for hot keys; 301 for cacheable, 302 when tracking clicks
    • Async analytics pipeline (Kafka → warehouse) so redirects stay fast

Interview Q&A (9)

Take a globally unique ID from a distributed generator (Snowflake or pre-allocated counter ranges) and base62-encode it. That avoids coordination on the hot path and guarantees no collisions.

Key components

  • API service
  • ID generator
  • KV store (Redis/DynamoDB)
  • CDN/cache
  • Analytics pipeline

Bottlenecks & how to address them

  • Hot-key reads → mitigate with CDN/cache
  • ID generation contention → pre-allocated ranges
  • Analytics writes on the redirect path → make them async

Tradeoffs to articulate

  • Counter vs random key (collisions vs predictability)
  • 301 cacheable vs 302 trackable
  • Strong vs eventual consistency for analytics

Where this content comes from

For full transparency, this content is curated and verified from these sources:

Published architecture case studiesCompany engineering blogsOppZen design rubric library