Turning a long destination into a tiny key and making redirects fast.
Which path matters more for a URL shortener: creating links or redirecting links?
Before reading, draw two lanes: create short URL and redirect short URL.
This design combines database lookup, caching, unique ID generation, analytics, and hot-key protection.
A URL shortener accepts a long URL, generates a short code, stores the mapping, and redirects readers quickly when they visit the short URL.
That simplicity is useful. It makes the real design questions easier to see. The system is not hard because there are many moving parts. It is hard because one path gets very hot and one correctness rule cannot be violated: the short code must point to the right destination, and two writers must not create the same code by accident.
The system has two main lanes: a write lane that creates the short URL, and a read lane that redirects quickly from short code to long URL. The read lane usually dominates. That one observation should shape the rest of the answer.
Interviewers use URL shortener because it quickly reveals whether the candidate can scope cleanly, identify a read-heavy path, discuss uniqueness under concurrency, and keep analytics out of the critical response path.
The stronger version names the hot path, the correctness rule, and the work that should move off the path. That is what makes it interview-useful.
A URL shortener is a small CRUD app with a shorter string.
The redirect path is the product. Creation can be slower; redirects must be fast, reliable, and cache-friendly.
Separate create path, redirect path, and analytics side path before deep diving on code generation.
| Choice | Good when | Weak when | Interview line |
|---|---|---|---|
| Generated codes Default | You want predictable uniqueness and a clean write path. | Users need meaningful custom aliases as a core feature. | Generated codes keep the baseline write path simple and collision-safe. |
| Custom aliases | Branding or memorability matters. | Alias checks and collisions complicate the hot write path. | Custom aliases are useful, but they add validation and uniqueness checks I would treat as a second step. |
| Inline analytics | You need an immediately updated counter and traffic is small. | Redirect latency matters and traffic is high. | I would avoid blocking the redirect response on analytics writes. |
| Async analytics Default | Fast redirect latency matters more than immediate analytics visibility. | The product truly needs synchronous exact counters in the redirect path. | Async analytics keeps the user-facing path fast while still preserving event data. |
| Central ID allocator | Write scale is modest and the system is simple. | The allocator becomes a bottleneck or a single hot dependency. | I would use a central allocator only while write scale is still modest. |
| Distributed or range-based IDs | Many servers create codes concurrently. | The coordination logic is weak and collisions are still possible. | At higher write scale, I want each writer to have a safe code-generation strategy without central contention. |
This is the correctness question in the system. The interviewer is listening for whether you can explain the contention shape, not whether you can drop a branded algorithm name.
This is the clean pressure test for the design. The correct instinct is cache and async separation, not a dramatic re-architecture.
Do not jump straight from reading to a full answer. First see the shape, then complete part of it, then answer alone.
I would say: "The hot path is redirect, so I keep it thin: cache lookup, mapping store fallback, then async analytics."
For custom aliases and expiration, mark which checks happen during create and which happen during redirect.
Answer the practice prompt with two lanes and one side path.
Before moving on, turn recognition into production. Close the model answer, answer from memory, then retry one small slice.
Say the chapter's core idea without looking. Then name one related idea from an earlier chapter.
Change one constraint in the practice prompt and answer again in half the time.
Use the rubric to pick one dimension below 3, then retry only that dimension.
Optimize the read lane first. Everything else exists to protect it.
Talk about contention shape, not branded algorithm names.
Analytics, abuse checks, and dashboards should be asynchronous unless the product promise says otherwise.