Stop 20 · System DesignTopics · 14/15

Designing a URL Shortener

Part of the Backend Developer Roadmap

In short

'Design a URL shortener' is the classic system design warm-up. It looks tiny, but it touches IDs, databases, caching, redirects and scale in one neat package.

A classic system design interview question — requiring you to reason about ID generation, redirect performance, database choice, and handling scale, all in a deceptively simple-sounding product.

The purple blob at a shoe stand takes a huge pile of shoes and hands back one small token.

Think of it like this

The temple shoe stand

At the temple, you hand over a big pile of shoes and get one tiny metal token. Later you show the token and get exactly your shoes back. The stand never needs your shoe size, just a lookup from token to rack. A short URL is that token for a long link.

Watch it happen

Create a link, then follow it

User
Short API
Cache
DB

POST /shorten {url}

1/7A user submits a long URL. The API validates it and checks it isn't a known malicious site.

The big ideas

What you need to know

Unique short codes

Base62 of an auto-increment or distributed ID gives unique codes with no collisions to check.

Read-heavy by nature

Clicks vastly outnumber new links, so cache hot codes and add read replicas.

301 vs 302

301 is permanent and browsers cache it, so you lose click counts. 302 sends every click through you.

Abuse and expiry

Block malicious URLs, rate limit link creation, and support expiry for temporary links.

See it in code

Walk through the code

ts
1const ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";2 3export function encode(id: number): string {4  let code = "";5  do {6    code = ALPHABET[id % 62] + code;7    id = Math.floor(id / 62);8  } while (id > 0);9  return code;10}11 12export function decode(code: string): number {13  return [...code].reduce((n, ch) => n * 62 + ALPHABET.indexOf(ch), 0);14}15 16// encode(125) === "21", decode("21") === 125

1/462 symbols: digits, lowercase and uppercase letters.

Seven base62 characters cover over 3.5 trillion IDs. Sequential codes are guessable, so add randomness if links should stay private.

Save yourself the pain

Common mistakes

Oops

Generating random codes and never checking for collisions.

Do this instead

Two links can get the same code, and someone lands on a stranger's page. Use unique IDs, or check and retry.

Oops

Using plain sequential codes for links that should be private.

Do this instead

Anyone can try sho.rt/22, /23 and browse other people's links. Use longer random codes for private links.

Your mission

Build the core

~45 min

Quick check

You want accurate click counts for each short link. Which redirect status should you use?

Go deeper

  1. 1Design the core flow: generate a short code, store the mapping, redirect on lookup.
  2. 2Decide how you'd generate unique short codes at scale without collisions.
  3. 3Consider a caching strategy for the very read-heavy redirect lookup path.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge