How to run a URL shortener design in a 45-minute interview: Base62 code generation with range allocation, the cached redirect path, 301 vs 302, async analytics and abuse handling.
Published September 21, 2026
This lesson is the interview version of the URL shortener (bit.ly, TinyURL): how to pace the conversation and which deep dives to prepare. The full reference design is the case study Design a URL Shortener in this chapter.
It looks trivial ("map a short code to a long URL"), which is exactly why interviewers use it. The interesting parts are generating unique short codes at scale without collisions or coordination bottlenecks, serving an extremely read-heavy redirect path with very low latency, and handling abuse and analytics without slowing redirects down.
sho.rt/summer-sale), expiry, click analytics, user accounts to manage links.Code length: Base62 (aβz AβZ 0β9) with 7 characters gives 62β· β 3.5 trillion codes. At 100M/day that lasts about 95 years, so 7 characters is enough.
Storage: 100M/day Γ 365 Γ ~500 bytes β 18 TB/year. That's large but ordinary for a horizontally scaled key-value store.
create: client ββΆ API ββΆ ID generator ββΆ Base62 encode ββΆ store {code β longUrl, owner, expiry}
redirect: client ββΆ edge/CDN ββΆ redirect service ββΆ cache (Redis) βmissββΆ key-value store
β
ββ click event ββΆ queue ββΆ analytics pipeline
Option A: hash the URL and truncate. Take MD5 or SHA-256 of the long URL, Base62-encode it, and keep 7 characters.
Option B: unique counter + Base62 (preferred). Give each link a unique integer ID and Base62-encode it.
Making option B scale:
Custom aliases go into the same keyspace, with a conditional insert ("create only if absent") to reject duplicates atomically.
Never write analytics synchronously on the redirect path. Emit a small event (code, timestamp, referrer, coarse location, user agent) to a queue such as Kafka. Stream processors aggregate counts per link, per time bucket and per country into an analytics store. Exact real-time counts aren't needed.
expiresAt, check it on read, and delete lazily or with a background sweeper. Most teams don't reuse expired codes, because old shared links would suddenly point somewhere new.Q: How do you prevent two servers from issuing the same code? A: Give each server a disjoint ID range from a coordinator, so uniqueness holds by construction. For custom aliases, rely on a conditional put in the store, which fails if the key exists.
Q: Should the same long URL always return the same short code? A: It's a product decision. Deduplication saves storage, but breaks per-user analytics and ownership, since two users would share one link. Many services create a new code per request, or deduplicate per user through a secondary index on (user, longUrl).
Q: How would you handle 1 million redirects per second for one viral link? A: That one key is a hot key. Serve it from CDN or edge caching, or from an in-process cache on each redirect server, so the load never concentrates on a single cache shard.
Q: How do you make codes non-guessable? A: Apply a reversible permutation (a bit-mix or a format-preserving cipher) to the sequential ID before Base62 encoding, or generate random codes with a conditional insert and retry on the rare collision. Private links can also use longer codes.
Q: What database would you pick and why? A: A horizontally scalable key-value or wide-column store, because every access is a point lookup by code, the data is immutable, and the volume grows steadily. Relational databases are fine at smaller scales, especially if you need rich per-user queries, which can live in a separate store.