URL Shortener
How URL Shortener works
Before you start
- Open lesson
Requirements
Each worked system starts from a scoped list of verbs and numbers.
- Open lesson
Caching
The read path sits behind cache-aside.
Why it matters
A paste link looks like a shortener until the value behind it can be 1 MB and must expire. A create needs a compact unique code, a read must stay fast, and keeping large text beside lookup metadata can make the hottest row unwieldy. The design must generate each code without a collision lookup and keep the 1 MB snippet bytes out of the mapping row.
Example
Design Pastebin
Users save a text snippet and receive a link. Anyone with the link can read the text. Snippets expire.
- Input
- n = 11157
- Output
- "2TX"
Approach 2 · Resolve the bottleneck
The idea
Keep one example in view
Design Pastebin
n = 11157→"2TX"
The baseline, Random codes with a check, is lookup per create · O(1). The better model below keeps this exact input and removes that bottleneck.
Instead of generating a random seven-character code and checking for collisions, encode unique id 11157 as base-62 “2TX”; store the paste’s 1 MB bytes in object storage and keep only their key and expiry in the mapping row.
The create API stores the snippet bytes in object storage and writes a small row containing code, object key, owner, and expiry. Reads resolve that row through the cache, check expiry, then fetch the object. This keeps 1 MB payloads out of the indexed mapping table.
An ID generator supplies 11157. Repeated division by 62 yields characters that reverse to “2TX,” so the mapping can be inserted without first probing for that code. Seven base-62 characters cover about 3.5 trillion ids; the tradeoff is that predictable codes may need authorization or obfuscation for private content.
Pro
The rest of this is on Pro
The idea above is free. The walkthrough, the cost, and the practice are on Pro.