Design a URL Shortener
Design a URL shortening service similar to bit.ly or TinyURL. Users submit a long URL and receive a short alias (e.g., https://short.ly/aB3xY). When a user visits the short URL, the service redirects them to the original. The service must handle high read traffic, provide low-latency redirects, and allow optional analytics per short URL.
Requirements
- โGiven a long URL, generate a unique short alias and return it to the user.
- โGiven a short alias, resolve and redirect to the original URL with low latency.
- โShort aliases must be unique and collision-resistant.
- โOptionally support custom aliases chosen by the user.
- โOptionally record click count and basic analytics per short URL.
- โShort URLs should not expire by default; expiry is an optional configuration.
Constraints & Scale
- โWrite volume: ~100 million new URLs per day (assume 100:1 read-to-write ratio).
- โRedirect p99 latency target: under 10 ms.
- โAlias length: 7 characters (default) using [a-z, A-Z, 0-9] โ 62^7 โ 3.5 trillion combinations.
- โData retention: indefinite unless expiry is explicitly set.
- โAssume globally distributed read traffic, writes concentrated in a few regions.
Interactive Architecture Blueprint1 view
Visual topology, node responsibilities, traffic flow steps, and bottlenecks.
Before designing, clarify the problem scope. What are the core functional requirements? What constraints and scale assumptions define the design space? Identify what is given, what you must assume, and what is explicitly out of scope.
Core requirements: generate short alias โ store mapping โ resolve + redirect. Key constraints: 100M writes/day โ 1,160 writes/sec; 100:1 read ratio โ 116,000 reads/sec; 62^7 alias space; p99 redirect < 10 ms. Out of scope for base design: per-user authentication, abuse prevention, real-time analytics dashboards.
Trade-offs
- โGlobal read traffic favors edge caching or a distributed KV store over a single relational DB.
- โCustom aliases introduce collision risk and require reservation logic separate from the ID generation path.
