Design a URL Shortener (TinyURL / Bitly Architecture)

Design a URL Shortener (TinyURL / Bitly Architecture)
URL shorteners convert long URLs (e.g. 150 characters) into concise aliases (e.g. https://sho.rt/aX9z3), serving massive 100:1 read-to-write redirect traffic.
1. System Requirements
Functional Requirements
- Shorten URL: Given a long URL, return a unique 7-character short alias.
- Redirect: Accessing the short link redirects the user to the original destination URL.
- Analytics: Track total clicks and click timestamp metrics.
- Custom Aliases: Optional user-specified vanity URLs (e.g.
/black-friday).
Non-Functional Requirements
- Low Redirect Latency: Redirections should complete in $< 20\text{ms}$.
- High Availability: 99.999% uptime for redirect resolution.
2. Base62 Encoding & Unique ID Generation
A 7-character alphanumeric string using Base62 characters ([a-z, A-Z, 0-9]) provides: $$62^7 \approx 3.52 \times 10^{12} \text{ (3.5 Trillion unique short URLs)}$$
Distributed ID Generator (Snowflake Pattern)
Rather than hashing the URL (which causes collisions and requires database lookups), generate a 64-bit integer using a distributed ID generator (Twitter Snowflake), then convert that integer directly to Base62:
ID: 10,000,000,000 -> Base62: "aZ4b1q"3. HTTP 301 vs. HTTP 302 Redirection
- 301 Moved Permanently: Browsers cache the redirect locally. Subsequent clicks bypass the URL shortener server completely.
- *Pros*: Lowest server load.
- *Cons*: Analytics (click counts) cannot be recorded for cached clicks!
- 302 Found (Temporary Redirect): The browser always queries the URL shortener on every click.
- *Pros*: 100% accurate click analytics and tracking.
- *Cons*: Higher server traffic.
Key Architecture Trade-Offs & Decision Matrix
| Decision Factor | Choice A | Choice B | Selected Architectural Rationale |
|---|---|---|---|
| Communication Protocol | REST over HTTP/2 | WebSockets / gRPC | Use REST for stateless CRUD APIs; use WebSockets for low-latency bidirectional streaming. |
| State Management | Centralized Distributed Store | Local Memory with Sticky Sessions | Centralized Redis / Database state allows seamless container auto-scaling and zero-downtime rolling deploys. |
| Concurrency Control | Optimistic Locking (version column) | Pessimistic Locking (SELECT FOR UPDATE) | Optimistic locking delivers superior throughput for low-conflict consumer operations. |
Prerequisites
- Hashing Algorithms
- Base62 Encoding
Related Topics
Expand your knowledge by learning about adjacent concepts in system design.
Design a Distributed Caching System (Redis Architecture)
Comprehensive architecture guide for designing production-grade Redis distributed caching clusters: master-replica replication, Redis Sentinel, Redis Cluster sharding, and memory eviction.
Scaling Reads (Replication, CQRS & Multi-Level Caching)
Techniques for scaling read-heavy systems: Read Replicas, Multi-Tier Caching (L1/L2), Command Query Responsibility Segregation (CQRS), and Edge CDNs.
Design Bitly (Scalable URL Shortener & Analytics Platform)
Design a high-scale URL shortening and click tracking service like Bitly or TinyURL capable of handling billions of redirects with sub-15ms latency.
Cheat Sheet Utility
View and print a concise system design reference card.