Design a URL shortener
“Design a URL shortener” is a system design interview question. Design a service that turns a long URL into a short link, like sho.rt/aZ3k9Q, and sends anyone who opens the short link on to the original URL with a redirect. It takes about 100 million new links a month and keeps them for five years. Each link is opened about 100 times on average, and redirects peak near 20,000 a second. A long URL averages 500 bytes, and users can choose their own alias instead of a generated code. Below is what to cover and 3 hints; press Practice and Dana, the interviewer, asks it out loud while you sketch the design.
Updated · By InterviewMind
Free in your browser, no account.
The problem
System design
Design a service that turns a long URL into a short link, like sho.rt/aZ3k9Q, and sends anyone who opens the short link on to the original URL with a redirect.
It takes about 100 million new links a month and keeps them for five years. Each link is opened about 100 times on average, and redirects peak near 20,000 a second. A long URL averages 500 bytes, and users can choose their own alias instead of a generated code.
Things to cover
- Generating short codes, and how long they need to be
- The data model, and where the links are stored
- Keeping redirects fast: caching, and a 301 versus a 302
- Custom aliases, collisions and abuse
Hints
Open one at a time, and try the question again after each.
Hint 1
Start with storage: 100 million links a month for five years is 6 billion links, about 3 TB at 500 bytes each. Then the code length: 62 possible characters in 7 positions give about 3.5 trillion codes, plenty of room.
Hint 2
The core is a key-value lookup from code to long URL, with a cache in front, since a small share of links gets most of the clicks. Codes that come from a counter, written in base 62, can’t collide.
Hint 3
The hard part is handing out unique codes from many servers at once without a shared bottleneck. Give each server its own block of counter values to use up, and decide whether codes that are easy to guess are acceptable.
Practice it out loud
In the practice room Dana reads the question aloud in your browser’s own voice, with captions, and you answer in the editor beside it, in a design doc with the requirements filled in and a section for each part. It’s free and needs no account. Run the InterviewMind desktop app beside it and it hears the question the way it hears a real call, so you can see what it would show you in the live round.
Practice this question