URL Shortener System Design

Senior-level distributed system design: 100M writes/day · 10B redirects/day · 99.99% availability

ℹ️ Scope: Public URL shortening service (think bit.ly / TinyURL) with analytics, custom aliases, and global low-latency redirect.

System Architecture Diagram

flowchart TD subgraph Clients["👤 Clients"] Browser["🌐 Browser"] Mobile["📱 Mobile"] APIClient["⚙️ API Client"] end subgraph Edge["Edge Layer"] CDN["☁️ CloudFront / Fastly CDN\n85% redirect cache hit"] WAF["🛡️ API Gateway + WAF\nRate Limit · Auth · DDoS"] end subgraph AppServices["Application Services (Stateless, Auto-scaled)"] RS["⚡ Redirect Service\nGo · p99 < 10ms"] WS["✍️ Write Service\nPython · idempotent"] AnS["📊 Analytics Service\nPython · async consumer"] end subgraph IDGen["ID Generation"] SF["❄️ Snowflake Nodes\n4096 IDs / ms / node\nBase62 → 7-char code"] end subgraph CacheLayer["Cache Layer"] L1["🔵 L1 In-Process LRU\n~0.1ms · 256 MB per pod"] L2["🟢 L2 Redis Cluster\n~1ms · 64 GB · sharded"] end subgraph StorageLayer["Storage Layer"] Cass[("🗄️ Cassandra Cluster\nURL store · RF=3\nCL=LOCAL_QUORUM")] PG[("🐘 PostgreSQL\nUser accounts\nACID billing")] CH[("📈 ClickHouse\nClick events\n1B rows/day")] end subgraph AsyncPipeline["Async Pipeline"] Kafka["📨 Kafka\n64 partitions · 7d retention"] Flink["⚙️ Flink / Samza\nStream enrichment\nGeoIP · UA parse"] end subgraph MultiRegion["Multi-Region Active-Active"] R1["🇺🇸 US-East"] R2["🇪🇺 EU-West"] R3["🇸🇬 AP-Southeast"] end Browser -->|GET /:code| CDN Mobile -->|GET /:code| CDN APIClient -->|POST /shorten| WAF CDN -->|cache miss| WAF WAF -->|read| RS WAF -->|write| WS WAF -->|auth| PG RS --> L1 L1 -->|L1 miss| L2 L2 -->|L2 miss| Cass RS -->|fire & forget| Kafka WS --> SF SF -->|Snowflake ID| WS WS -->|write-through| Cass WS -->|warm cache| L2 WS -->|user quota| PG Kafka --> Flink Flink --> CH Flink --> AnS Cass -.->|async replication| R1 Cass -.->|async replication| R2 Cass -.->|async replication| R3 style Edge fill:#e0f2fe,stroke:#0284c7 style AppServices fill:#f0fdf4,stroke:#16a34a style CacheLayer fill:#fef9c3,stroke:#ca8a04 style StorageLayer fill:#fdf4ff,stroke:#a855f7 style AsyncPipeline fill:#fff7ed,stroke:#ea580c style IDGen fill:#f0fdf4,stroke:#16a34a style MultiRegion fill:#f1f5f9,stroke:#64748b

Cache Layers
3-Tier
CDN Hit Rate
85%
Regions
3 Active
P99 Redirect
< 10ms