URL Shortener: First Design
Minutes ten to twenty are for a design that works. Not the final one, and not a sketch drawn to be torn down: the design a careful team would ship on day one, correct for every functional requirement, walked from the click to the answer. The interviewer is about to push on it, and a design you believe in is much easier to improve than one you have already disowned.
This lesson draws that design, walks one create and one redirect through it, says why it is a fair answer, and names where it breaks. The quiz at the end is the interviewer's half of the conversation: the five pushes that the next lesson answers.
The Design
Clients reach a tier of application servers behind a load balancer. The servers keep no state of their own, so any of them can answer any request, and adding one adds capacity. Behind them sits one relational database with one table of links. Its primary key is a 64-bit number that the database increments for every new row.
The short key is that number written in base 62. The first link is key one, the billionth is six characters long, and so is the 18 billionth, five years out. The seven characters agreed in Scope are the room left after that. Because the database hands out each number exactly once, two links can never get the same key, and nothing else in the design has to check.
One Create, One Redirect
A create arrives at any application server. The server inserts a row with the long URL and the expiry, and the database returns the new row's number. The server writes that number in base 62 and returns the short URL. One insert, one round trip, and the key is unique by construction.
A redirect arrives as a GET on the short key. The server turns the key back into the number, reads the row by its primary key, checks that the link has not expired, and answers with a redirect to the long URL. If there is no row, or the link has expired, it answers not found. One indexed read per click.
Why It Is a Fair First Answer
It meets every functional requirement agreed in Scope. Keys cannot collide. A redirect is a lookup by primary key, the cheapest read a relational database does. Expiry is a column and a comparison. At launch traffic, this is the design a careful team ships, and you should say that before you say anything against it.
It also keeps the questions that matter visible. Every later change will be a response to something this design does at a specific load, which is exactly the conversation the interviewer wants to have.
Where It Breaks
Four limits are worth naming, without fixing them yet. Every redirect is a trip to the one database, at up to 20,000 a second. The one database is the one machine whose loss takes the whole service down. The table grows to 18 billion rows and 27 terabytes with copies. And the sequence that makes keys unique lives in one place.
Volunteer the first limit before the interviewer finds it, with the number where it bites and the way you would see it. You might say: "This holds while one database serves every redirect. As we head toward 20,000 a second, I'd watch read latency on the primary climb, and the read path is the first thing I'd change."
- Presenting this design as a straw man to be torn down. The interviewer asked for a first design and gets a sketch with no conviction behind it.
- Hashing the long URL for the key on the first pass, which brings collisions into the round before anything has forced them.
- Drawing a cache, a queue and shards before the first design works end to end. The interviewer cannot tell which box answers which requirement.
- Stopping without saying where the design breaks, so the interviewer has to find the limit for you.
- One create and one redirect walked end to end, every hop named.
- Why the design is correct, said before why it will not last.
- The first limit volunteered with the number where it bites.
- A design held with conviction and changed with reasons when pushed.
Knowledge Check
"Reads outnumber writes a hundred to one, and every redirect is a lookup on the one database. What do you say?"
- "Put a cache in front of the lookups and keep the database for writes and misses."
- "Add an index on the short key so that each one of the redirect lookups gets faster."
- "Move the links table to a document store that is built for very heavy reads."
- "Batch the redirects and answer all of them every few seconds from one query."
"A celebrity posts one of your links and it gets 50,000 reads a second. What happens?"
- "One cache node serves twice that many reads, so the cache absorbs it just as it is."
- "Shard the cache more finely so the viral link's reads spread over more nodes."
- "Give the viral link a longer TTL so its entry stays cached while it is hot."
- "Keep a short-lived copy of the hottest keys inside each application server."
"Five years from now the table holds 18 billion links. Does it still fit?"
- "At about 500 bytes a link, three copies make 27 TB: split the table by key."
- "At about 500 bytes a link it is 9 TB, which one large disk holds, so keep it."
- "Archive links older than a year to cold storage and keep the rest on one node."
- "Compress the long URLs, which halves the size and keeps the table on one node."
"The table is now split across fourteen nodes. Where does the next key come from?"
- "Each node keeps its own auto-increment, and the keys stay unique as before."
- "A central counter service hands out the next number, one request per new link."
- "A counter service hands each app server a block of numbers to spend locally."
- "Hash the long URL and take the first seven characters as the short key."
"Your keys come from a counter, so anyone can walk them in order. Does that matter?"
- "No, a short link is public by design, so walking the key space reveals nothing."
- "Yes, so scramble each number with a keyed one-to-one mapping before encoding."
- "Yes, so switch to random keys and accept that two of them may sometimes clash."
- "Yes, so rate-limit the redirect endpoint to stop anyone walking the keys."
You got correct