Design a URL Shortener
🔗 What is a URL shortener? It is the service behind links like
sho.rt/000a7Q. You give it a long address and it hands back a short code. When someone opens the short link, the service looks the code up and sends them to the long address. Some links are permanent; others, like a link for a one-week sale, stop working after a while.
In a system design interview the URL shortener is about databases, caches and scale. In a low-level design interview it is about the code inside one process: how codes are made, how a user's own alias fits next to generated codes, and how a link quietly dies when its time is up. It is a good easy problem because each of those three pieces has a simple wrong answer and a slightly less simple right one. For the distributed version, see /system-design/url-shortener.
Requirements
"Design a URL shortener. Users give it a long URL and get a short code back. Opening the code returns the long URL. Users can pick their own alias, and links can expire."
Three features, each with a decision hidden inside. The questions below find them.
Clarifying Questions
You: How should generated codes look? Random, or is anything fine as long as it is short?
Interviewer: Short and unique is what matters. Use letters and digits. Fixed length is nice, because it looks tidy in print.
You: If two people shorten the same long URL, do they get the same code?
Interviewer: No. Each request gets its own code. Different people may want different expiry times, and later we may count clicks per code.
You: What makes a custom alias valid?
Interviewer: 3 to 20 characters: letters, digits and dashes. Case matters, so Docs and docs are different links.
You: Do custom aliases and generated codes live in the same space? What if someone picks an alias that looks like a generated code?
Interviewer: Same space, since both appear after the same domain. A code can only point to one link. If a user takes a code the generator would produce later, the generator has to skip it.
You: How does expiry work? Who passes the time?
Interviewer: Each link may have a time to live in seconds; zero means forever. Pass the current time into every call. An expired link behaves as if it never existed, and its code can be taken again.
You: Can a generated code ever come back after its link expires?
Interviewer: No. Someone might still have the old link printed somewhere. Generated codes are never reused. A user can deliberately claim an expired code as an alias, though.
The fourth answer is the interesting one. It means the generator cannot blindly trust its counter: it has to check the store before handing out a code.
💡 Interview tip: When two features share one namespace (here, aliases and generated codes), ask what happens when they collide. The answer is almost always a rule you must code, and it is easy to miss until the walkthrough.
Final Requirements
shorten(long_url, now, ttl)stores the link under a new generated code and returns the code. Codes are 6 characters of base62 (0-9,a-z,A-Z).- Generated codes come from a counter that only moves forward. A counter value whose code is already in use is skipped.
shorten_custom(long_url, alias, now, ttl)stores the link under the user's alias. It fails if the alias is not 3 to 20 letters, digits or dashes, or if a live link already uses it.expand(code, now)returns the long URL, or nothing if the code is unknown or expired.- A link with
ttl = 0never expires. Otherwise it is live whilenow < created + ttl. - An expired code is free: an alias may take it, and the generator does not need to skip it.
- Shortening the same long URL twice gives two different codes.
Out of scope
- Click statistics, users and accounts (we come back to clicks in Extensibility).
- Several servers sharing one counter, and persistence to a database.
- Checking that the long URL is reachable or safe.
- Deleting or editing links.