URL Shortener

TinyURL / Bitly

Design a service that turns a long URL into a short code and sends anyone who opens the short link to the original page, fast, for years.

Overview

A URL shortener takes a long address such as https://example.com/docs/2026/getting-started?ref=newsletter and gives back a short one such as https://sho.rt/EDTFUwV. When someone opens the short link, the service looks up the code and answers with an HTTP redirect to the long address.

The problem looks small, and that is why interviewers like it: the whole design is one table and two requests, so they can spend the time on the parts that decide quality. How do many servers create codes without ever giving two links the same code? How does one cheap lookup serve thousands of redirects a second? What does the redirect status code do to caching and to click counts?

Questions to ask first

  • How many new links, and how many clicks? — 500 million new links a month; 100 redirects for every link created
  • How long do links live? — Forever by default; a creator may set an expiry
  • Can users choose the code? — Yes, a custom alias, if nobody has it
  • Do we count clicks? — Yes, per link, a few minutes behind is fine
  • Can a link's destination change? — No; a new destination means a new link
  • Must codes be hard to guess? — Yes; nobody should find other links by walking the codes in order

What you will be able to do

  • Choose a code length from the numbers and defend it.
  • Compare counters, hashes, random codes and pre-made keys, and pick one for many servers.
  • Explain what 301, 302, 307 and 308 do to caching and click counts.
  • Size the cache and the database for a read-heavy workload.
  • Keep analytics, abuse checks and expiry off the redirect's critical path.

Key concepts

  • Base62 encoding
  • ID blocks
  • Birthday collisions
  • 301 vs 302
  • Cache-aside
  • Key-value lookup
  • Hash sharding
  • Async click events
  • Rate limiting

Functional requirements

  • Create a short link for a long URL, and return it
  • Redirect anyone who opens the short link to the long URL
  • Let a user pick a custom alias if it is free
  • Let a creator set an expiry; expired links stop redirecting
  • Show a link's click count, a few minutes behind at most

Non-functional requirements

  • Redirects answer in under 50 ms at p99 inside our region (a target we chose)
  • Redirects stay up 99.99% of the time; creating links may degrade first
  • A short code never points to two different URLs
  • Codes cannot be walked in order to find other links
  • Links keep working for years, so stored data must be durable

Interview questions

  • Why seven characters?
  • Why not just hash the long URL?
  • How do many servers create codes without colliding?
  • Should the redirect be a 301 or a 302?
  • SQL or NoSQL for the links?
  • What happens when the database or the cache goes down?
  • How do you stop the service being used to spread malware or spam?
  • How would you serve users in several regions?

Sign up free to read the full topic

More System Design topics

Start free on Cariara · Coding problems · System design studio