AlgoViz

Design a URL shortener

medium 45 min

Turn a long URL into a short code and redirect anyone who opens it.

Practise it on the whiteboardGuided steps, draw your design, and get AI feedback on the drawing and your answers.

Video lesson · Chapter 1 of 6

Create one durable mapping

0:00 / 6:10

URL shortener in 90 seconds

1:25Reel coming soon

Design a URL shortener in one picture
Design a URL shortener in one picture

Theory

Read or listen first. About 1 min.

What it is

A URL shortener is a service that stores a long web address under a short code. When someone opens the short address, the service looks up the code and tells the browser where to go.

It is a dictionary on the internet: code in, address out. The dictionary is easy. Making it work for billions of entries, for people all over the world, without ever opening the wrong page, is the design problem.

Why it exists

Long links break in emails, waste space in messages and are hard to type from paper. A short link fits on a poster or in an SMS.

Short links also sit in the middle of every click. That makes them useful for counting visits, changing a destination after printing, and stopping a harmful link. It also makes the service a promise: if it disappears, every link it issued stops working.

A short history

  1. 1996

    HTTP/1.0 (RFC 1945) describes the 301 and 302 redirects that shorteners rely on.

  2. 2002

    TinyURL launches and makes the idea popular.

  3. 2006 to 2010

    Short messages with a length limit make short links a habit. Bitly starts in 2008 and adds click statistics.

  4. 2010

    Twitter begins wrapping links with its own t.co domain.

  5. 2018 to 2019

    Google closes goo.gl to new links, a reminder that issued links outlive product plans.

Words to know

Short code
The few characters after the domain, such as 3fZk9Qa.
Redirect
A reply that tells the browser to open another address.
Base62
Writing a number with 62 symbols: digits, lower and upper case letters.
Read to write ratio
How many times data is read for each time it is written. Here about 100 to 1.
Hot key
One item that gets far more traffic than the others.
Conditional write
Store only if nothing is there yet.

Solve it like an interview

New to it? Start with Approach. Revising? Go straight to the mock.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

How it works

Step through the idea, or press Play.

Step 1 of 3

Shorten. POST a long URL, get back a short code like `sho.rt/3fZk9Qa`

Understand it

Whiteboard sketch: a client opens sho.rt/3fZk9Qa, the request goes through a load balancer to the service, which checks a cache and a database, then replies 302 with a Location header.
Client → load balancer → service → cache, then the database on a miss. The reply is a redirect.

Scope

What are we building?

A URL shortener converts long, unwieldy URLs into short, memorable links. Opening the short link redirects to the original. It is one of the most asked design questions because it looks simple and has clear trade-offs.

  • Shorten

    Long to short: a 108-character product URL becomes sho.rt/3fZk9Qa.

  • Redirect

    A 301 or 302 sends the browser on, in under 100 ms.

  • Analytics

    Clicks by time, country, referrer and device.

Similar applications

ServiceKnown for
BitlyMost popular, enterprise features
TinyURLThe original pioneer, simple interface
RebrandlyCustom branded domains
Short.ioAPI-first
t.coX (Twitter) link wrapping
goo.glGoogle, closed to new links in 2018-2019

The finished system

Where the design ends up

This is the last version, not the first. The Design tab starts with one service and one database, and shows which number forces each extra box.

The whole system

Clients reach a CDN and a gateway with load balancing, firewall, rate limiting and authentication. Redirects go to the redirect service, Redis and then DynamoDB. Creates go to the URL service, which calls our safety check (and Google Safe Browsing, outside our system), leases IDs from ZooKeeper and writes DynamoDB; accounts live in PostgreSQL. Clicks flow through Kafka and a stream processor to ClickHouse, read by the analytics API.
Blue is the read path, pink the write path, orange analytics. Dashed means only sometimes, or asynchronous.

Summary

The whole topic in one minute.

A URL shortener turns a long address into a short code and back. The data is simple, so the interest is in three places: making unique codes on many servers, answering far more reads than writes, and counting clicks without slowing them.

Our design grows from one service and one database. Caches, a partitioned store, leased ID ranges, the edge and an event stream are added one at a time, each because a number or a failure asked for it.

  • Links are read about 100 times more often than they are created, so the read path gets the attention.
  • Hand out numbers in ranges and the codes cannot collide; scramble them so they cannot be walked.
  • Each cache tier exists to shrink what reaches the next one.
  • Counting clicks is a separate, slower system that must never delay a redirect.
  • A code that was issued must keep working and must never point somewhere new.

Sources