How would you design a URL shortener like Bitly?
Tests API design, identifier generation, storage choices, caching, and read-heavy scaling.
Sample answer
I would first clarify expected traffic, link lifetime, custom aliases, analytics, and the availability target. The core APIs are createShortUrl(longUrl, optionalAlias) and redirect(shortCode). A relational or key-value store can hold the short-code-to-URL mapping, while a Base62-encoded sequence or a distributed ID generator produces compact, unique keys without retry-heavy collisions.
Because redirects dominate writes, I would cache popular mappings close to users and use a CDN or edge layer where appropriate. I would partition the primary store by a stable hash of the short code, replicate it across availability zones, and make redirect reads tolerant of a brief replication delay. The final discussion should cover expired links, abuse detection, hot keys, analytics as an asynchronous pipeline, and what happens when the cache or a region fails.