🔗 Design URL Shortener — System Design Interview Guide

Easy · Storage & Encoding

Design a service like bit.ly that converts long URLs into short, unique aliases and redirects users to the original URL when visited.

Open the interactive URL Shortener design on PrepGrind → Drag load balancers, caches, databases, and queues onto a canvas, run a live traffic simulation to watch latency and bottlenecks under load, and follow the full interview walkthrough below — free, in your browser.

Functional requirements

Non-functional requirements & scale

Capacity estimation

Assume 100M URLs/day writes, 10B reads/day. Each URL record ~500 bytes. 5 years = ~90 TB. Short code: 7 chars from Base62 [0-9A-Za-z] = 62^7 ≈ 3.5 trillion unique codes. Read-heavy system; cache is critical.

Core entities

API design

High-level design

On create: generate Base62 short code from a Snowflake ID, write to Postgres + Redis. On redirect: Redis cache lookup (cache-aside) → DB fallback → publish click event to Kafka → 302 redirect. Analytics consumer writes to ClickHouse.

Deep dives

🔑 Short Code Generation

Option A: Hash (MD5) + take first 7 chars — risk of collisions under load. Option B: Auto-increment DB ID + Base62 encode — simple but single-point of failure. Option C: Snowflake ID (Twitter) + Base62 — distributed, monotonic, collision-free. Best choice is C. 62^7 gives 3.5 trillion codes; at 1,200/sec it takes ~90,000 years to exhaust.

⚡ Redirect at 115k RPS

Store active shortCode→longUrl mappings in Redis cluster. Cache hit = no DB query. 128GB Redis holds ~256M URLs (500 bytes each). LRU eviction; warm cache on startup. Use HTTP 302 (not 301) so browsers do not cache — ensures analytics fire on every visit.

📊 Analytics Pipeline

Synchronous analytics writes would add 50-100ms latency. Instead: publish click event to Kafka topic (fire-and-forget). Analytics consumer batch-writes to ClickHouse (columnar OLAP). Use Redis HyperLogLog for approximate unique visitor counts (12KB per key).

🌍 Global Scale

Deploy read replicas in each region. Use GeoDNS to route users to nearest region. Short code generation must stay globally unique — either use a central Snowflake ID service or assign node ID ranges per region (bits 10-19 of Snowflake = datacenter/machine ID).

Scaling considerations

What interviewers expect by level

Practice more system design case studies

PrepGrind runs entirely in your browser, free, no installation required. Loading the interactive playground…