System Design · Architecture under constraints

How would you design a URL shortener?

Mediumurl shortenerkey generationcachingUpdated Oct 2026
Quiz this track

Short answer

Generate a short unique key, store the mapping from key to long URL in a partitioned key-value store, and redirect through a cache. Reads dominate, so caching and key generation are the core design problems.

Clarify the requirements first. Assume billions of links, a read-to-write ratio around 100:1, redirects with p99 under 50 milliseconds, links that may expire, and optional click analytics. Out of scope: full content hosting or a social feed.

API and data model

  • POST /urls with a long URL returns a short code and the full short URL
  • GET /{code} returns a 301 or 302 redirect to the long URL
  • The primary table is code to long URL, owner, created time and expiry
  • Analytics are written asynchronously so the redirect path stays fast
text
function encodeBase62(id) {
  const alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  let value = id;
  let out = "";
  while (value > 0) {
    out = alphabet[value % 62] + out;
    value = Math.floor(value / 62);
  }
  return out;
}
Seven base62 characters represent about 3.5 trillion values, which is enough headroom for most services.

For key generation, a distributed counter or a time-ordered id such as Snowflake avoids collisions without a random read. A random key needs a uniqueness check and a retry loop. Base62 encoding of a numeric id is compact and predictable, but sequential ids leak volume and enable enumeration, so add a random salt or use a non-sequential id when that matters.

The mapping store is a partitioned key-value database keyed by code, with replication for availability. Cache the hot codes in Redis and at the CDN edge, but do not cache a 404 for long because a key may be created later. Use 302 when you need to count every click or change the destination, and 301 when permanent caching is acceptable.

Analytics must not block the redirect. Emit a click event to a queue and aggregate asynchronously. Also protect against abuse with rate limits and a blocklist; an open shortener becomes a phishing tool.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1How do you avoid collisions with a random short code?
  2. 2Would you return 301 or 302 and why?
  3. 3How do you handle hot links and abusive links?
  4. 4How would you support custom aliases and expiration?

Say it in the interview

A URL shortener is a read-heavy key-value problem: map a short code to a long URL and redirect quickly. I generate a unique id, encode it in base62, and shard the mapping by code with replication. Reads go through a cache and the CDN, while click analytics go to a queue so the redirect path stays fast. For custom aliases I add a uniqueness constraint, and for abuse I rate limit and blocklist.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live System Design mock interview and get scored feedback on depth, clarity and confidence.