Skip to content
Tutorials Center
My learning

Topics

Lesson 70 of 77 ยท Projects

Project: Design and Prototype a URL Shortener

Design and prototype a URL shortener: requirements, base62 ID generation, storage, redirects with 301 vs 302, custom aliases, analytics and capacity estimates.

"Design a URL shortener" is one of the most common system design questions, because it is small enough to finish in 45 minutes yet touches nearly everything: requirements, estimation, ID generation, storage, caching, redirects and analytics. The strong answers are not the ones with the most boxes. They are the ones that clarify first, make a choice at each step, and say what they gave up.

In this project you will follow that path in order: write requirements, estimate scale, then build a working prototype in Node.js and test it. The honest limit: the prototype is an in-memory model with a fake request and response, not a web server with a real database. It shows the logic that would sit behind an HTTP service; the infrastructure around it (databases, CDNs, multiple regions) is discussed, not run. Background: System Design: The Building Blocks and System Design: Rate Limiters, Feeds and Chat.

The path of a strong answer

Requirements What exactly must it do?
Estimates Reads, writes, storage.
ID scheme How short codes are made.
Storage Keys, indexes, caching.
1 / 2

Step 1: Requirements

Ask before you draw. A reasonable set:

  • Functional. Create a short link for a long URL. Visiting the short link redirects to the original. Optionally: custom aliases, expiry, and click counts.
  • Non-functional. Redirects must be very fast and highly available. Short codes must be unique and hard to guess one-by-one if links are private. Links should not change meaning after creation.
  • Out of scope for now. User accounts, abuse detection and link previews. Say so out loud; it shows you are managing the scope.

Step 2: Back-of-the-envelope numbers

Pick assumptions and show the arithmetic. Suppose 100 million new links per month and a read-to-write ratio of 100 to 1. This small script turns assumptions into numbers you can reason about.

Node.js Capacity estimate

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 node:readline or process.stdin), it gets the next line from this box.

About 39 writes and 3,900 reads per second on average; peaks are several times higher. That is modest for a cache-backed system. Five years is 6 billion links, and 62^7 is about 3.5 trillion codes, so seven characters have plenty of headroom. Storage of about 3 TB fits one modern database, though you would still replicate it for availability.

Step 3: Generating short codes

There are three common strategies, and the trade-offs are the interview.

  1. Hash the URL and take the first characters. Same URL gives the same code (nice), but different URLs can collide, so you must check and retry, and anyone can predict codes.
  2. Random code with a uniqueness check. Hard to guess; costs a lookup per insert and retries on rare collisions.
  3. Counter encoded in base62. Each new link takes the next integer, encoded with 0-9, a-z and A-Z. No collisions by construction. Codes are predictable and sequential, which leaks volume; a common fix is to scramble the counter with a reversible mixing step. At scale the counter is handed out in ranges to each app server so they do not fight over one number.

Our prototype uses a counter with a reversible scramble: multiply by a number coprime to the code space, modulo that space. Because the multiplier and the modulus share no factor, every number maps to a unique different number, so uniqueness is kept and the order disappears.

Node.js Base62 and a reversible scramble

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 node:readline or process.stdin), it gets the next line from this box.

Plain base62 gives 0000001, 0000002, and so on: easy to enumerate. The scrambled codes look random yet 200,000 counters still produce 200,000 distinct codes. The final line checks only that the multiplier is odd and not divisible by 31; for a rigorous guarantee you need it to share no prime factor with 62^7, whose only prime factors are 2 and 31. This is obfuscation, not security: it stops casual guessing, not a determined attacker. If links are secret, use long random tokens and access checks instead.

Step 4: Storage and the data model

Each record needs the code (primary key), the long URL, a creation time and an optional expiry. The access pattern is a pure key-value lookup: given a code, return a URL. That fits a key-value store or a relational table with a primary key index, and it shards naturally by code. Put a cache (an LRU cache, as in the previous project) in front: a small set of popular links serves most traffic.

For custom aliases, check that the alias is free before inserting, and reject reserved words such as api or admin. The database's unique constraint is the final guard against two users claiming the same alias in the same instant; application checks alone are a race.

Step 5: Redirects: 301 or 302

A redirect response carries a status code and a Location header. Two choices matter:

  • 301 Moved Permanently. Browsers and proxies may cache it, so later visits skip your server. Great for load, but you lose click counts and cannot change or expire the link for clients that cached it.
  • 302 Found (temporary). Every visit reaches your server, so analytics and expiry work. It costs more traffic.

Shorteners that count clicks usually use 302 (or 307), and accept the load because the read path is cheap. A deleted or expired link should return 404 or 410 Gone, never a redirect.

Step 6: The prototype

This prototype ties the pieces together. shorten creates a link (with an optional alias and expiry), handle models a GET /:code request and returns a status and headers, and clicks are recorded for analytics. The clock is injected so expiry can be tested.

Node.js A working URL shortener model

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 node:readline or process.stdin), it gets the next line from this box.

Notice the validation: only http and https are accepted, because a shortener that redirects to javascript: or data: URLs becomes an attack tool. Notice also that an expired link returns 410, and the stats count only successful redirects.

A production service would add: a rate limiter on creation, a check against a list of known malicious domains, hashing or deduplication if you want one code per URL, and analytics written to a queue instead of the request path. Writing clicks synchronously is fine for a prototype, but at 4,000 reads per second you would batch them.

Step 7: Putting it together in an answer

Close the interview with a short architecture description: clients hit a load balancer, then stateless app servers; a cache serves hot codes; the database stores code -> URL, sharded by code; a counter service hands ranges of IDs to servers; a queue feeds an analytics pipeline. Then name the trade-offs you made (301 vs 302, scrambled counter vs random code, cache TTL) and one thing you would monitor, such as the redirect latency at the 99th percentile.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Reads outnumber writes 100 to 1 in a URL shortener. What does that suggest?

    The hot path is the redirect lookup, a key-value read, so caching and fast lookups matter most.
  2. 2. Why use 302 instead of 301 when you want click analytics?

    A 301 may be cached by browsers and proxies, hiding later visits from you. A 302 is temporary, so every visit hits the service.
  3. 3. What is the advantage of a base62 counter over hashing the URL for codes?

    Each counter value maps to exactly one code. The downside is predictability, which a reversible scramble hides but does not make secure.
  4. 4. Which check is essential before redirecting to a stored URL, and when creating it?

    Accepting javascript: or data: URLs would let attackers abuse your domain. Validate the scheme and the URL's shape.
  5. 5. A short link has expired. What should the service return?

    An expired link no longer exists, so the response should say so. Redirecting would keep serving content the owner wanted to stop.

Summary

  • Start with requirements and numbers: reads dominate, so optimize and cache the redirect path.
  • Seven base62 characters give about 3.5 trillion codes.
  • Code generation options: hash (collisions, predictable), random plus check, or a base62 counter (unique, predictable unless scrambled).
  • The data model is a key-value lookup; the database unique constraint guards custom aliases.
  • Use 302 when you need analytics or expiry; return 404 or 410 for missing or expired links.
  • Validate schemes and reserve special aliases; record clicks asynchronously at scale.

Next: Practice: JavaScript and Python Question Set.