URL Shortener: Scope
The first ten minutes decide what the next thirty-five are about. The interviewer has given you one sentence, and the design you will draw depends on answers you do not have yet: how many links, how many clicks per link, whether the clicks are counted. This lesson covers those minutes, from the questions to the two calls of the API.
Scope is not a formality to rush through. It is where the interviewer first sees whether you build from requirements or from habit, and every number agreed here comes back later as the reason for a decision.
The Questions That Change the Design
A clarifying question is worth asking when its answer changes what you draw. Five do here, and each one is listed with the answer this lesson agrees on and what that answer changes.
- How many new links a day, and how long are they kept? The answer sets the key length and the storage. Here, 10 million a day, kept for five years.
- Roughly how many times is a link opened? The answer sets the read path, the part of the design that carries the load. Here, 100 redirects for every link created.
- Do we need click analytics? A yes means every redirect also produces an event, and it pushes the redirect code toward the kind that sends every click back to you. Here, yes, as an event; the dashboard is someone else's.
- Can users choose their own alias, and can a link be deleted or expire? Aliases need a uniqueness check on write. Deletion means every place a link is cached has to let go of it. Here, expiry is in, and aliases are left for a curveball.
- Does the same long URL always give the same short link? A yes needs a lookup by long URL on every write, with its own index. Here, no: each request gets a new key.
The order matters less than the second question. Read volume is what the whole design is built around, so if you ask only one question, ask that one. You might say: "Before I draw anything, roughly how many clicks does a link get? That tells me whether this is a storage problem or a read problem."
What You Agree On
Two things are functional: create a short link for a long URL, with an optional expiry, and send anyone who opens the short link to the long one. Three are non-functional. A redirect is fast and almost always available, because a broken link is visible to everyone who clicks it. A link never resolves to the wrong URL. A link that was created survives the loss of any one machine.
Say out loud what you are leaving out, so that the interviewer can object now rather than in minute thirty. Accounts and dashboards, abuse detection, and analytics beyond emitting the event are all out of scope. You might say: "I'll emit a click event and leave the analytics pipeline out, unless you want it in."
The Numbers That Decide
Estimate only what changes a decision, and say each step as one operation. Ten million new links a day, over about a hundred thousand seconds in a day, is about 100 writes a second, and about 200 at a peak of twice the average. Two hundred writes a second decides nothing in this design, and saying so saves a minute.
The reads do decide. A hundred redirects for every link is about 10,000 redirects a second, and about 20,000 at peak. That is the number the read path is built for, and the first design will be judged against it.
Storage and the key length come from how long links are kept. Ten million a day for a year is about three and a half billion, and five years is about 18 billion links. At about 500 bytes a link, that is 9 terabytes, and 27 terabytes with three copies. Keys written in base 62, the digits and both cases of letters, give 57 billion combinations at six characters, only three times what five years needs. Seven characters give 3.5 trillion. So the key is seven characters long.
Bandwidth is worth one sentence and no more. Twenty thousand redirects a second at about 500 bytes each is 10 megabytes a second, against a 10-gigabit link that carries more than a gigabyte a second. It decides nothing, so you skip it and say why.
Two Calls, One Entity, One Key
The API is two calls. A POST to the links endpoint carries the long URL and an optional expiry, and returns the short URL. A GET on a short key answers with a redirect to the long URL, or with not found when the key does not exist or has expired. Nothing else is needed for the functional requirements.
POST /links {"url": "https://example.com/a/very/long/path", "expires_at": "2027-01-01"}
201 {"short_url": "https://short.example/aZ3kQ9x"}
GET /aZ3kQ9x 302 Location: https://example.com/a/very/long/path
GET /unknown 404
There is one entity, the link: its key, its long URL, when it was created and when it expires. Every lookup is by key, and that single access pattern is what makes the design simple to scale. Say that now; it is the reason for a partitioning choice twenty minutes later.
- Working out the writes in detail when the reads decide the design. The interviewer watches three minutes go on a number that changes nothing.
- Choosing the key length before agreeing how long links are kept. The length is an answer to the retention question, not a preference.
- Leaving analytics unasked, then finding out in the deep dive that the redirect code was chosen wrong for counting clicks.
- Drawing a cache before the read-write ratio is agreed. The interviewer asks why, and the answer is a guess.
- The question that sets the read path, clicks per link, asked before anything is drawn.
- The agreed numbers turned into the seven-character key out loud, one step at a time.
- Out of scope said as a sentence, so that the interviewer can disagree while it is cheap.
- An estimate dropped on purpose, with the reason it decides nothing.
Knowledge Check
You have time for one clarifying question before you draw. Which answer changes this design the most?
- Which programming language the service is going to be written in
- Roughly how many times a link is opened after it is created
- Which characters are allowed in a short key and in what order
- Which cloud provider the company already runs its services on
Five years of links at 10 million a day is about 18 billion. Why are the keys seven characters of base 62 rather than six?
- Six characters cannot hold 18 billion different keys in base 62
- Seven characters are faster to look up in the store than six are
- Six give only three times the keys needed, and seven leave room to grow
- Seven characters is the length the redirect protocol always expects
Which estimate can you state in one sentence and then drop, because it decides nothing here?
- The redirects a second at peak, about 20,000 of them
- The storage over five years, about 27 TB with copies
- The bandwidth at peak, about 10 megabytes a second
- The links kept over five years, about 18 billion
Which of these is safest to rule out of scope out loud in the first ten minutes?
- Redirecting a short key to its long URL for every reader
- The dashboard that shows advertisers their click analytics
- Keeping a created link alive when one machine is lost
- Letting a link expire on the date its creator gave
You got correct