URL Shortener: Deep Dives and Curveballs
The last fifteen minutes are where the interviewer picks the ground. The full design is on the board, and now the conversation narrows to the two or three parts this problem is known for, then turns on you with a change of requirements. This lesson takes the deep dives in the order they usually come, then four curveballs, then what separates a mid-level answer from a staff one.
If the interviewer leaves the choice open, take the deep dive that carries the most risk first. Here that is key generation, because a mistake there is permanent: a key handed out twice cannot be taken back.
Key Generation, Compared
There are three serious ways to make the key, and each one moves the cost somewhere different. Counter blocks with a keyed mapping, the design from Scaling It, have no collision path at all, as long as the counter service records each high mark durably before the block leaves (Chapter 5). Uniqueness comes from the counter, so a write is one insert and never a retry.
Hashing the long URL and keeping seven characters of the hash gives the same key for the same URL, which is useful when duplicates must collapse into one link. It also means two different URLs will sometimes share seven characters, so every write must check for a collision and try again with a salt. Random keys behave the same way: no coordination at all, and a uniqueness check on every write that becomes more expensive as the space fills.
So the choice turns on one requirement agreed in Scope. Each request gets a new link, so counter blocks win here. If the interviewer changes that, and the same URL must always give the same link, hashing becomes the right call, and you should say which way the answer moves. You might say: "I'd keep the counter blocks. If we ever need one link per URL, I'd switch to a hash with a collision retry and an index on the long URL."
The Redirect Code and the Click
The redirect answer is either permanent or temporary, and the difference is who remembers it. A permanent redirect may be kept by browsers and caches along the way, so the next click on that link may never reach you. A temporary one asks every time. Analytics were agreed in Scope, so the temporary redirect is the choice, and every click is a read the system pays for.
That read must stay a read. The click event goes to a log or a queue that a separate pipeline aggregates, never into a database write on the redirect path. Writing a row per click would turn a design built for 20,000 cheap lookups a second into one doing 20,000 writes as well.
A 301 says the link has moved for good, so browsers and proxies may keep the answer and stop asking. Use it when the link is permanent and nobody needs to count its clicks.
A 302 says the link has moved for now, so every click comes back to you. Use it when clicks are counted, or when a link may ever have to be taken down.
When the Cache Is the System
With 99 percent of reads served from cache, the cache is no longer an optimisation; the store is sized on the assumption that it works. Four events break that assumption. After a full flush, every key misses at once. When a cache node is lost without a replica, its share of the keys misses. When a hot key expires, every server that wanted it asks the store at the same moment. And a client that walks keys that do not exist misses every time, because a miss leaves nothing to cache.
The store's headroom for each comes from the arithmetic in Chapter 2. A cold tier sends all 20,000 reads a second to a store sized for 200. One node of two lost sends half of them. The fixes are the ones to name: warm the cache from recent links before it takes traffic, keep a replica for every cache node, and spread expiry times so that hot keys do not all expire together, and limit lookups per client address, as Chapter 11 builds.
Curveballs
"Advertisers want click counts per link, updated within a minute." The redirect already emits an event. A stream job counts events per link per minute and writes the totals to a store the dashboard reads. The redirect path does not change, and that is the point to make.
"A link turns out to be malicious. It must stop working everywhere within a minute." Delete the row and write a marker that every cache layer treats as not found, then wait out the longest expiry in the path, which is why each layer's expiry has to be short and stated. A permanent redirect already kept by a browser cannot be recalled at all, and that is the strongest argument for the temporary redirect.
"Users want to choose their own alias." Aliases live in the same key space as generated keys, so the two must never meet. Either reserve a character that generated keys never use and require it in aliases, or check every alias for uniqueness on write. Aliases bypass the counter, which is fine, because they are rare.
"Half the clicks now come from another continent." Add read replicas and a cache tier in the second region, so that redirects there are answered locally. Writes can stay in one home region: new links are few, and a key carries nothing regional, so the same key resolves the same way everywhere. A key the local replica has not seen yet, a link made a moment ago, is read from the home region before any answer of not found.
How Each Level Answers
A mid-level answer reaches a working design with a sound API and data model, does the key-length arithmetic, and puts a cache on the read path. It comes to key generation when the interviewer raises it, and it handles the curveballs with some prompting.
A senior answer drives the scope and puts numbers on it: a hundred reads per write, 27 terabytes, seven characters. It raises the viral link and the loss of the auto-increment before being asked, chooses the temporary redirect and says what it costs, and keeps analytics off the redirect path without being told.
A staff answer treats takedown and cacheability as one product decision with an abuse cost, and plans the second region and the failure of the counter service. It also asks what the links are for. Marketing links and links shared inside a company want different redirect codes and different amounts of key secrecy, and the staff candidate makes that question part of the design.
- Defending a permanent redirect for speed after agreeing to analytics. The interviewer has just watched the requirement disappear.
- Answering the takedown curveball with "delete the row", while every cache layer still holds the link.
- Spending the deep dive on how base-62 encoding works, which nobody asked about, and running out of time for the parts that matter.
- Treating each curveball as a new design instead of a change to the one on the board.
- The trade between a cacheable 301 and a countable 302, stated with the requirement that decides it.
- A staleness bound for every layer that holds a link, offered before the takedown question arrives.
- The riskiest deep dive taken first when the interviewer leaves the order open.
- Each curveball answered as the smallest change to the existing design.
Knowledge Check
"A link is malicious and must stop working everywhere within a minute." What is the best move?
- Delete the link's row from the store and confirm that the row is gone
- Delete the row, write a not-found marker, and wait out the longest cache expiry
- Switch the link to a permanent redirect pointing at a warning page instead
- Flush the whole cache tier so that every layer is forced to read again
Click counts per link must appear on a dashboard within a minute. Where does each click go?
- Into a counter column on the link's row, updated on every redirect
- Into the browser's own history, which the dashboard reads each minute
- Into a log or queue that a separate job counts per link per minute
- Into a counter kept in the cache tier, read by the dashboard directly
Users may now choose their own alias. How do aliases and generated keys share one key space?
- Reserve a character generated keys never use, or check aliases on write
- Let aliases take any free key, since the counter will skip them later
- Store aliases in a separate table and look up both tables on each click
- Generate every alias from the counter too, so that both spaces stay one
Half the clicks now come from another continent. What is the smallest change that serves them well?
- Run a second full copy of the system there that accepts writes as well
- Give links created there a regional prefix so that they resolve locally
- Double the cache tier in the home region so that it answers them faster
- Add read replicas and a cache tier there, and keep writes in one region
You got correct