Skip to content
Tutorials Center
My learning

Topics

Lesson 46 of 65 ยท Case Studies: Classic Designs

Design a URL Shortener

Design a URL shortener step by step: requirements, capacity estimates, base62 IDs, collisions, redirects, caching and sharding, with runnable Python simulations.

A URL shortener turns https://example.com/articles/2026/very-long-path?ref=newsletter into something like sho.rt/aZ3k9Q. It looks trivial, which is exactly why it is the most common first case study: the idea fits in one sentence, yet every real design decision (ID generation, storage, caching, abuse) shows up in miniature.

In this lesson you will walk through the standard shape of a design answer: requirements, estimates, API, data model, the core algorithm, then scaling. Use the same shape for every other case study in this chapter.

Step 1: requirements

Functional:

  • Given a long URL, create a short link (optionally with a custom alias and an expiry date).
  • Opening a short link redirects to the long URL.
  • Owners can see click counts (optional, we will treat it as a stretch goal).

Non-functional:

  • Redirects must be fast (under about 50 ms server time) and highly available. A dead redirect breaks someone else's page.
  • Short codes must be unique and hard to guess in order.
  • Reads vastly outnumber writes, which is the most important fact in this design.

Step 2: capacity estimates

Assume 100 million new links per month and a read-to-write ratio of 100:1.

  • Writes: 100,000,000 / (30 x 86,400) is about 40 writes per second.
  • Reads: about 4,000 redirects per second on average, maybe 10,000 at peak.
  • Storage: 100 million links per month x 12 x 5 years is 6 billion records. At roughly 500 bytes each (long URL, code, timestamps) that is about 3 TB. Small enough for a sharded database.
  • Code length: 62 characters (a-z, A-Z, 0-9) with 7 positions gives 62^7, about 3.5 trillion codes. Far more than 6 billion, so 7 characters is plenty.

URL shortener at a glance

Write path About 40 per second.
Read path About 4,000 per second.
Storage About 3 TB over 5 years.
Code length 7 base62 characters.

Reads dominate, so cache first.

1 / 2

Step 3: API and data model

Two endpoints are enough:

TEXT
POST /api/links        { "url": "...", "alias": "optional", "expires_at": "optional" }  ->  { "short": "sho.rt/aZ3k9Q" }
GET  /{code}           ->  302 redirect to the long URL

The data model is one table keyed by the code:

Column Notes
code (primary key) The 7-character string
long_url Up to about 2 KB
created_at, expires_at For cleanup
owner_id Optional

Because every read looks a row up by primary key and nothing joins, a key-value store or a sharded relational database both work well.

Use a 302 (temporary) redirect if you want every click to reach your servers for analytics. A 301 (permanent) redirect lets browsers cache the target and skip you, which saves load but loses the count.

Step 4: generating the code

There are two classic approaches.

Counter plus base62. A global counter hands out 1, 2, 3, and so on. You encode each number in base 62. The codes are guaranteed unique and short, but sequential, so people can enumerate links. A fix is to scramble the number with a reversible shuffle before encoding.

Hash and truncate. Hash the long URL (for example with SHA-256), take the first 7 base62 characters. No counter is needed, but two different URLs can collide, so you must check the database and retry with a salt.

Python Base62 encoding of a counter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

To hide the sequence, multiply the counter by a large number coprime with 62^7 modulo 62^7. The result is a permutation, so uniqueness is kept while neighbours look unrelated. You will implement the collision-checked variant next.

Step 5: the service in miniature

This simulation stores links in a dictionary, handles collisions for the hash approach, supports custom aliases and expiry, and counts redirects.

Python A tiny shortener with collisions and expiry

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With a 3-character space (238,328 codes) and 2,000 links you already see some collisions, because of the birthday effect. With 7 characters and 6 billion links the collision rate is still real, which is why the retry loop and a unique constraint on the primary key are not optional.

Step 6: scaling the design

flowchart LR
    U["Client"] --> LB["Load balancer"]
    LB --> A1["App server"]
    LB --> A2["App server"]
    A1 --> C[("Cache cluster")]
    A2 --> C
    C -. miss .-> D[("Sharded database")]
    A1 --> K["Click events queue"]
    K --> W["Analytics workers"]

Reads hit the cache first; clicks are recorded asynchronously so the redirect never waits for analytics.

  • Cache: the hot 20% of links serve most traffic. A cache of a few dozen GB with an LRU policy gives a hit rate above 90%.
  • Sharding: shard by code using consistent hashing so that adding a node moves few keys.
  • Counter service: if you use a counter, give each app server a range of 10,000 IDs so they do not contact the central counter for every write. A crashed server only wastes its unused range.
  • Cleanup: a background job deletes expired links, and expired codes can be reused later.
  • Abuse: rate-limit creation per user and check long URLs against a malware list.
  • Availability: replicate the database and keep the cache optional. If the cache dies, the database must still absorb the load for a while.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is a 7-character base62 code enough for 6 billion links?

    62 symbols over 7 positions give 62^7, roughly 3.5 trillion distinct codes.
  2. 2. What is the main downside of hashing the long URL and truncating the result?

    Truncation throws away bits, so collisions happen. You need a unique key constraint and a retry with a salt.
  3. 3. Which redirect status lets you count every click?

    A temporary redirect is not cached the same way, so each click reaches your servers.
  4. 4. Reads outnumber writes 100 to 1. What is the best first scaling step?

    Most traffic is repeated lookups by key, which caches handle very well.

Summary

  • Start with requirements and estimates: about 40 writes and 4,000 reads per second, 3 TB in five years.
  • Seven base62 characters give 3.5 trillion codes.
  • Use a counter with base62 (unique, may be guessable) or hash and truncate (needs collision handling).
  • Cache aggressively, shard by code, and record clicks through a queue.
  • Choose 301 or 302 according to whether you want analytics.

Next: Design a Rate Limiter