← Whiteboard questions

Design a URL shortener

Turn long links into short ones and redirect

System design, Medium

Design a service that turns a long URL into a short link and redirects visitors. Assume 100 million new links per month and 10 times more redirects than creations. Links live for 5 years. A redirect must answer in under 50 ms at the 99th percentile. Cover the API, how the short code is made, the storage choice, how you serve the read load, what happens when a link expires, and one abuse risk. Out of scope: user accounts and the analytics dashboard (but say where click events go).

Examples

Input POST /links with { "url": "https://example.org/products/2026/summer-sale?ref=mail&id=88231" }
Output { "code": "aZ3k9Qx", "shortUrl": "https://sho.rt/aZ3k9Qx" }
Why The code has a fixed short length whatever the length of the long URL.
Input GET /aZ3k9Qx
Output A redirect response with a Location header holding the long URL, in under 50 ms at the 99th percentile.
Why This is the read path. The status code you pick is part of the design.
Input GET /zzzzzzz where no link has this code
Output HTTP 404, and the answer must not scan the whole store to find out.
Why Edge case: a code that does not exist is a normal request and must be cheap.

Constraints

Your answer must contain

A strong answer covers A strong answer covers the numbers, the code format, the data model, the read path with a cache, and one abuse risk.

Hints

Try the problem first. Open one hint at a time.

Hint 1 a nudge

Start with numbers. How many links a second, and how many bytes over 5 years? The answer tells you whether one machine is enough.

Hint 2 the approach

The code must be short and unique. Think about base62, and about how to get unique numbers without one busy counter. Then pick a store for reads by key.

Hint 3 the key step

Most traffic is redirects of popular codes. Put a cache in front of the store and cache missing codes too. Send click events to a queue, not through the redirect.

Walkthrough

Full walkthrough brute force to best

Naive design

One web server and one SQL table with an auto-increment id. The short code is the id written in base 62. It works for a demo. But the server is one point of failure, every redirect reads the database, and ids in order are easy to guess.

Improved design

Split the work. Stateless API servers hand out unique ids in ranges, so there is no single hot counter. The data sits in a key-value store keyed by code, or a partitioned SQL table with a read replica. This scales storage and writes, but every redirect still reads the store.

Final design

Numbers first: 100 million links per month is about 40 creations per second on average and about 400 redirects per second, with peaks about 5 times higher. Over 5 years that is 100 million x 60 = 6 billion links, about 3 TB at 500 bytes each, which fits a sharded key-value store. Codes: 7 base62 characters give about 3.5 trillion codes, far more than 6 billion. Make them from unique numbers handed out in ranges, then shuffle the bits if guessing matters, or use random codes with an insert that fails on a duplicate key. Redirects: answer 302 if every click must reach you, or 301 if browser caching matters more. Put a cache in front, because about 20 percent of codes carry most reads. Size it: 400 redirects per second x 86,400 seconds is about 35 million reads a day, 20 percent of that is about 7 million entries, and 7 million x 500 bytes is about 3.5 GB, which fits one cache node. Cache missing codes too, so wrong codes never scan the store. Expiry: store expires_at with each link. A redirect that finds an expired link answers 404 and treats it as missing. Use the store TTL or a nightly sweep to delete expired rows, and cache an entry for no longer than its expiry. Allocator failure: if the id-range allocator is down, servers keep using the range they hold and ask again when it returns. A server that crashes loses the rest of its range, which is never reissued. Skipped ids cost nothing, because 3.5 trillion codes are available. Abuse: block lists for phishing links, a creation rate limit per key, and a report path. Click events go to a queue after the redirect.

Follow one request

Follow a create and a read. POST /links with a long URL: the API takes the next unique number from its range, writes it as 7 base62 characters, stores the code, the URL, the created time and the expiry, and returns the short URL. GET /aZ3k9Qx: the API checks the cache, which misses the first time, reads the store, fills the cache and answers a redirect with the long URL in the Location header. The second GET is answered from the cache. A code that does not exist is cached as missing, so the store is not read again.

API and data model

POST /links    body { url, alias?, expiresAt? }  ->  { code, shortUrl }
GET  /{code}    ->  302 with a Location header, or 404
Table links:    code (primary key), url, created_at, expires_at
Cache:          code -> url, and a short-lived "missing" marker for unknown codes

Numbers and capacity

About 40 creations and 400 redirects per second on average. About 6 billion rows and 3 TB over 5 years. About 3.5 GB of cache holds the popular 20 percent of one day of reads.

Common mistakes

  • No estimates, so the storage and cache choices have no reason behind them.
  • A hash of the URL cut short with no collision check. Two URLs can get the same code.
  • Every redirect reads the database. The 50 ms target fails at peak.
  • Writing click events inside the redirect request. A slow queue then slows every redirect.

Related problems

Design a URL shortener: walkthrough and Python solution