Designing a Distributed URL Shortener (TinyURL)
Design a high-scale URL shortening service: Base62 encoding, Key Generation Service, 80/20 Redis caching, and HTTP redirect trade-offs.
A URL shortener (such as TinyURL, Bitly, or ow.ly) takes a long, unwieldy web address and converts it into a concise, memorable alias:
- Long URL:
https://veltoryxtech.com/learn/hld/advanced-architectural-patterns/cqrs-command-query-responsibility-segregation?referrer=newsletter&campaign=autumn - Short URL:
https://vel.to/abc1234
When a user visits https://vel.to/abc1234, the service looks up the original address and redirects the user's web browser in less than 50 milliseconds.
While the basic functionality seems trivial to implement with a simple dictionary or SQL table, building a production-grade URL shortener that handles hundreds of millions of links and tens of thousands of redirect queries per second introduces fundamental distributed systems challenges:
- How do you generate unique, collision-free 7-character aliases across multiple servers without centralized database locking?
- How do you size storage for billions of URLs over a five-year horizon?
- Should you return an HTTP 301 Permanent Redirect or an HTTP 302 Temporary Redirect?
01Core concepts and intuition
A distributed URL shortener is heavily read-dominant. In a typical consumer setup:
- Read-to-Write ratio: Approximately 100:1 (for every 1 short link created, it is clicked 100 times).
- Latency constraint: Redirection must be sub-50 milliseconds. A slow redirect frustrates users before they even reach the target website.
- Availability requirement: High availability is far more critical than immediate read-your-own-writes consistency.
Encoding: Why Base62?
To create a compact URL slug, characters should be URL-safe without requiring URL encoding (like %20).
The standard character set consists of:
- 10 numeric digits:
0-9 - 26 lowercase English letters:
a-z - 26 uppercase English letters:
A-Z - Total alphabet = 10 + 26 + 26 = 62 characters (Base62).
How long does our short alias need to be?
- A 6-character Base62 string yields: 62^6 ≈ 56.8 billion unique URLs.
- A 7-character Base62 string yields: 62^7 ≈ 3.52 trillion unique URLs.
A 7-character Base62 token provides ample headroom for decades of global scale.
Hash collision versus Key Generation Service (KGS)
There are two primary architectural approaches to generating Base62 tokens:
- Hashing with Collision Handling:
- Compute an MD5 or SHA-256 hash of the long URL, take the first 43 bits, and encode them into Base62.
- Problem: Different long URLs can produce identical short tokens (hash collision). The application must query the database to verify whether the token is already in use. If taken, it appends a random salt and hashes again. Under high write load, this creates database read overhead and latency jitter.
- Key Generation Service (KGS) - The Industry Standard:
- Instead of hashing URLs on the fly, a dedicated background service pre-generates trillions of unique 7-character Base62 strings in advance and stores them in a database.
- When an application server needs to shorten a URL, it simply requests an unused key from the KGS.
- To eliminate network bottlenecks, each application server loads a batch of keys (e.g., 5,000 tokens) into local memory. As soon as a batch is loaded, the KGS marks those keys as used in the database.
- Even if an application server crashes and loses its in-memory batch, wasting a few thousand keys out of 3.5 trillion is negligible.
Loading the rest of the lesson…