← Lessons

quiz vs the machine

Silver1050

System Design

The URL Shortener Design

Turning long links into short codes that redirect, the classic warm up design.

4 min read · intro · beat Silver to climb

What it does

A URL shortener takes a long link and returns a short one. When someone visits the short link, the service looks up the original and redirects the browser there.

It is a small problem on the surface but exercises ideas that show up everywhere, id generation, key value lookup, caching, and read heavy scaling.

The two paths

  • Write path. A user submits a long url. The service creates a unique short code, stores the mapping code to long url, and returns the short link.
  • Read path. A browser hits the short link. The service looks up the code and returns an HTTP redirect to the long url.

Reads dominate. A link is created once but may be clicked millions of times.

What to get right

  • Codes must be short and unique.
  • Lookups must be fast, so a key value store plus cache fits well.
  • Redirects should return quickly with a proper status code.

Key idea

A URL shortener maps a unique short code to a long url, with a write path that creates the code and a read heavy lookup path that redirects, backed by a fast key value store.

Check yourself

Answer to earn rating on the learn ladder.

1. Which path dominates traffic in a URL shortener?

2. What does the service return when a visitor hits a short link?