Um momento
0xA0Lesson 11 of 13

Design a URL shortener

Walk through a classic prompt: short codes, redirects, and scale.

20 min 6-question quiz 1 code exercise
By the end of this lesson you can
  • Scope and estimate a URL shortener.
  • Generate unique short codes with base62 encoding.
  • Choose between 301 and 302 redirects and a storage model.

Requirements. Functional: create a short link for a long URL; redirect a short link to the original; optionally custom aliases and expiry. Non-functional: redirects must be fast and highly available; the system is extremely read-heavy (each link is created once but clicked many times); codes should not be trivially guessable. Estimates: 100 million new links a month is ~40 writes per second; at 100:1 that is ~4,000 redirects per second. At ~500 bytes a link, five years is 100M × 60 months × 500 B ≈ 3 TB.

API, codes, and storage

  • API: POST /links {longUrl} returns {code}; GET /{code} responds with a redirect.
  • Code space: base62 (0-9, a-z, A-Z) with 7 characters gives 62^7 ≈ 3.5 trillion codes - plenty.
  • Generating codes: (1) hash the long URL and take 7 characters, checking for collisions; or (2) take a unique integer id and base62-encode it. To avoid a single counter, each app server reserves a block of ids (say 1,000 at a time) from a coordinator.
  • Storage: a key-value lookup code → {longUrl, createdAt, expiresAt, ownerId} fits a key-value or wide-column store, or a sharded relational table. Hot codes are cached.
design.py
1ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
2
3def encode(number):
4    if number == 0:
5        return ALPHABET[0]
6    digits = []
7    while number > 0:
8        number, remainder = divmod(number, 62)
9        digits.append(ALPHABET[remainder])
10    return "".join(reversed(digits))
11
12print(encode(125))
13print(f"{62 ** 7:,} seven-character codes")
Output
21
3,521,614,606,208 seven-character codes

Redirects: a 301 (permanent) lets browsers cache the redirect, which cuts load but hides repeat clicks from analytics. A 302 (temporary) sends every click through the service, which enables analytics. Deep dives interviewers like: handling hash collisions, preventing enumeration of sequential codes, blocking malicious URLs, expiring links, and recording click analytics asynchronously through a queue so redirects stay fast.

Key takeaways

  • Seven base62 characters give ~3.5 trillion codes.

  • Generate codes by hashing with collision checks, or by base62-encoding pre-allocated unique ids.

  • Use 302 when you need click analytics; cache hot codes to keep redirects fast.

Lesson quiz

6 questions · pass with 5 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: simulate system design building blocks

Use small Python programs to estimate capacity and simulate caches, load balancers, hash rings, and rate limiters. These exercises run locally in your browser.

Exercise 1

Encode ids in base62

+25 XP

Read a non-negative integer id and print its base62 code using the alphabet 0-9, then a-z, then A-Z. The id 0 encodes as 0.

  • Small id
  • Zero
  • Largest 7-character code
main.py
Loading editor…

Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…

Gostou da aula? 😆👍
Apoie nosso trabalho com uma doação: