Ask before you build
An interviewer says: design a link shortener. You ask, and learn it gets 5,000 req/s, and people open links about 99 times for every link they create.
Play this levelPart of Bugpit Pro. It runs in your browser: build the design, press Run, and watch what breaks.
The lesson
The worst way to start is by drawing boxes. "Design X" is vague on purpose, and the first job is to find out what X has to do.
Functional requirements say what the system does: make a short link, and send people to the long one. Non functional requirements say how well: how fast, how many people, how reliable.
The answers decide the design. Almost every request here is a read, and the same popular links are opened again and again, so a cache will do far more than a bigger database.
What you have to achieve
5,000 requests a second at the busiest hour, 1% of them writes.
- No broken or illegal connections
- Use a Cache (Redis)
- No component is overloaded
- Fewer than 0.1% of requests fail
- p99 response time under 200 ms
What you learn
You let the requirements choose the design. Two questions told you this is a read heavy system, and that made the cache the obvious first move.
Check yourself
The interviewer has just said "design a link shortener". What do you do first?
- Draw the servers and database
- Ask what it must do and how much traffic it gets
- Choose a programming language
Show the answer
Ask what it must do and how much traffic it gets. Requirements come first. Without them you can't know whether a design is too small, or far too big.
Which of these is a non functional requirement?
- A user can create a short link
- A short link redirects to the long one
- 99% of redirects finish within 100 ms
Show the answer
99% of redirects finish within 100 ms. Functional means what the system does. Non functional means how well it does it, like speed and reliability.
People open links 99 times for every link made. What does that tell you?
- Design for reads first
- Design for writes first
- Reads and writes matter equally
Show the answer
Design for reads first. A read heavy system gets the most from caches and replicas, so that's where to spend effort.