System DesignIntermediate20 min

Case study: URL shortener

Apply estimation, storage, caching and ID generation to a small system with a big read ratio.

See it on the map

Requirements: create a short link for a long URL, redirect fast, optionally custom aliases and expiry. Reads vastly outnumber writes, perhaps 100 redirects for every creation, so the redirect path is the one to make fast and cheap.

The core is a key to URL mapping. For the key, generate unique IDs with a counter or a distributed ID generator and encode them in base62, or hash the URL and handle collisions. Counters give short, collision-free keys but need coordination, so give each app server a block of IDs.

Store the mapping in a key-value or relational table keyed by the short code, and put a cache in front, since popular links dominate traffic. Use a 301 for permanent redirects if you want browsers to cache, or 302 if you need to count every click. Analytics should be fed asynchronously through a queue so it never slows the redirect.

Key points

  • Optimise the read path: cache hot keys, keep the lookup a single key access.
  • Base62 of a unique ID gives short codes without collision handling.
  • 301 versus 302 is a product decision about caching versus click counting.
  • Record analytics asynchronously.

Answer these out loud

  1. 1How many characters do you need for 10 billion links in base62?
  2. 2How do you prevent guessable or abusive short codes, and how do you expire links?

Build it

Build the service with an in-memory store, add base62 encoding with block-allocated IDs, then add a cache and measure redirect latency.

Use this from an agent

Any MCP client, including Grok, can read this lesson. See how to connect.

REST
curl https://engineeros-delta.vercel.app/api/v1/topics/case-url-shortener