Skip to content

How to design Bitly

The first sections are free. The deep dives, level expectations and quiz are part of Pro.

On this page
Practice this question

The problem

A URL shortener turns a long link into a short code and sends anyone who opens that code on to the original page. It creates very few links and serves a huge number of redirects, so the answer turns on two things: handing out codes that can never repeat, and keeping redirects fast when one link goes viral.

Clarifying questions

AskWhy it mattersA typical answer

How many links do people create, and how many redirects do we serve?

The ratio decides the shape of the design. If redirects dwarf creates, the read side gets its own service and a cache; if not, one service is fine.

About 100 million daily users, a billion stored links, about 1 new link a second, and redirects that average 5,787 a second and peak near 600,000 a second when a link goes viral.

Can people pick their own code, and can links expire?

A chosen code shares the namespace with generated ones, and expiry changes what the cache is allowed to keep.

Both are optional per link: a custom alias if the person asks for one, and an optional expiry time.

Do the codes need to be hard to guess?

It decides between codes that come from a counter (short, but in order) and codes that look random.

Not for the first version. Say which way your scheme leans, and be ready to defend it.

Do we need click counts or analytics?

Counting clicks means every click has to reach something you control, your service or your edge, which rules out letting browsers remember the redirect.

Dashboards are out of scope, but keep the door open: pick a redirect that still passes through your servers.

Is it fine if a new link takes a moment to work everywhere?

It tells you whether to favor availability or strict consistency on the read path.

Yes. Redirects must stay up and fast; a brand-new link working a moment later in a far region is acceptable.

Can a link be deleted or changed after it is made?

A link that can change or disappear limits how long any cache may hold it.

Owners can delete a link. Changing its destination is not needed.

The requirements menu

RequirementScopeWhy

Create a short link for a long URL

Core

The whole product. Everything else hangs off the write path that makes a code and stores it.

Optionally choose a custom alias

Core

Readable codes are easier to share and type. It matters to the design because a chosen code and a generated code must never land on the same value.

Optionally set an expiry time

Core

Some links are only meant to work until a date. It forces the redirect path, and every cache on it, to respect that date.

Visiting a short link redirects to the long URL

Core

The read path, and the one that carries almost all the traffic.

Delete a short link before it expires

Optional

Worth taking if time allows: the API already has a delete, and it tests whether you can remove an entry from every cache that holds it. Leave it if you are short on time, since nothing else depends on it.

Return the existing short link when the same long URL comes in again

Optional

Tempting because it saves rows. Leave it unless asked: two people shortening the same page may want different expiries or aliases, and handing back someone else’s code tells them it was shortened before.

Change where an existing short link points

Optional

Useful when a printed link has a typo. Leave it unless asked, because a destination that can change means no cache, browser or edge may keep the old one for long.

A preview page that shows the destination before redirecting

Out of scope

A real safety feature, but it is a second product on top of the redirect. Name it and move on.

Click analytics dashboards

Out of scope

A separate pipeline. Say how a click event would leave the redirect service, then park it.

User accounts and link management

Out of scope

Sign-in and link lists are ordinary CRUD. Keep an owner id on each link so delete can check it, and stop there.

Spam and malware scanning

Out of scope

Shorteners are abused to hide bad destinations, so mention it, but scanning is its own system. A rate limit on creates is enough for this round.

Non-functional requirements

RequirementWhat it forces

Every short code is unique

The code column is the primary key, so the database refuses a duplicate no matter what goes wrong upstream. The generator must never hand out the same value twice, and a generated code that lands on a taken alias is skipped.

Redirects answer in under 100 ms

A redirect does one lookup and nothing else: no joins, no calls to other services. Popular codes are answered from memory, and far-away users from a server near them.

99.99% availability for redirects

That allows about 52 minutes of downtime a year, so no single machine may sit alone on the redirect path: the database has a standby that takes over by itself, and cached codes keep resolving while the write side is down. It also favors availability over consistency: a new link may take a moment to show up everywhere.

Reads outnumber writes about 1000 to 1

Split the redirect service from the write service so each scales on its own, and spend the design effort on the read path. The write path can stay simple.

Estimates

QuantityWorkingResultWhat it sizes

Codes that base62 strings of each length give

62^5 ≈ 916 million; 62^6 ≈ 56.8 billion; 62^7 ≈ 3.52 trillion

6 characters is the floor for 1 billion links; 7 characters yield roughly 3.5 trillion, about 3,500 per stored link

The code length: seven characters, which leaves so much room that a random code almost never lands on a taken one.

Chance a random 7-character code is already taken

1 billion taken ÷ 3.52 trillion possible ≈ 0.03%; with 6 characters, 1 billion ÷ 56.8 billion ≈ 1.8%

About 1 insert in 3,500 needs a retry at 7 characters; about 1 in 57 at 6

The retry loop of a hash-based code: at seven characters it almost never runs twice.

Counter calls with batches of 1,000

1 write a second ÷ 1,000 values per batch = 1 call every 1,000 seconds, about 17 minutes; at Alex Xu’s 1,200 writes a second, 1.2 calls a second

Between once every 17 minutes and about once a second

The load on the counter, which decides whether one instance with a replica is enough.

Peak against average redirects

600,000 at peak ÷ 5,787 on average ≈ 104

The peak is about 100 times the average

Why you design for the peak, not the average: a viral link multiplies the load on a single code.

Memory to cache a tenth of all links

Assuming a cached entry takes about as much as a stored row, 500 bytes: 10% of 1 billion links = 100 million; 100 million × 500 bytes = 50 GB

About 50 GB for each 10% of links cached

The memory of the redirect cache.

Downtime that 99.99% allows

0.01% × 525,600 minutes a year ≈ 52.6 minutes; 0.01% × 43,200 minutes a month ≈ 4.3 minutes

About 52 minutes a year, about 4 minutes a month

How fast failover must be: automatic, not a person paged at night.

Length of a code made from a full id

A 64-bit id needs 11 base62 characters (62^11 ≈ 5.2 × 10^19 > 2^64 ≈ 1.8 × 10^19); a 128-bit UUID needs 22 (62^22 ≈ 2.7 × 10^39 > 2^128 ≈ 3.4 × 10^38)

11 to 22 characters, against 7

Why a full UUID or a 64-bit id makes a poor code: unique for practical purposes, but several characters longer than it needs to be.

Core entities

EntityWhat it holds

Short link

One row per code: where it points, when it stops working, and who made it.

User

Whoever owns links. Accounts are out of scope, so this stays a stub the link points at.

The API

EndpointNotes

POST /urls { long_url, alias?, expires_at? } → { short_url }

Checks the URL, takes the alias if one is given (a taken alias returns 409 Conflict), otherwise generates a code. Rate-limit it per caller: spammers use shorteners to hide where a link goes.

GET /{code} → 302 redirect to the long URL

One lookup by primary key. Unknown code: 404. Expired code: 410 Gone, so people and crawlers learn the link is gone on purpose. Why 302 and not 301 is a deep dive.

DELETE /urls/{code}

Owner only. Deletes the row first, then removes the code from the cache and purges it at the edge. Deleting the cached copy first would let a reader load the old row and cache it again; this order closes most of that window, and short cache lifetimes cover the rest.

The high-level design

createredirectinsertlookupClientWrite serviceRedirect serviceLinks table

Step 1 of 4

Two paths: create and redirect

Start with the two jobs. A write service takes POST /urls, checks the long URL and inserts a row. A redirect service answers GET /{code} with one lookup and a 302, or 410 if the link has expired. They are separate services because redirects outnumber creates about 1000 to 1: the redirect side scales on its own, and a bad deploy of the write side cannot stop redirects. One relational table holds every link: a billion rows of about 500 bytes is about 500 GB, which one database server holds.

POST /urlsGET /codemissnext blockinsertcheck firstmissstream changesClientWrite serviceCDN edgeRedirect serviceCounterLinks tableRedis cacheStandby replica
The whole design

What breaks first

The deep dives

Generating unique codes. Two links created in the same second must never share a code, and the code should stay short. How do you make codes, and how many characters do they need?

A concept that helps here: Unique ID generationA concept that helps here: Database indexesA concept that helps here: Bloom filters

Scaling redirects. One link goes viral and redirects jump toward 600,000 a second, nearly all for a single code. What serves them, and which redirect status do you send?

A concept that helps here: Caching strategiesA concept that helps here: CDNs and edge cachingA concept that helps here: Hot keys and hot partitions

Going multi-region. The service now runs in three regions. How does each region make codes without clashing with the others, and what happens when a link made a second ago in one region is opened in another?

A concept that helps here: Running in several regionsA concept that helps here: Replication and read replicasA concept that helps here: Consistency models

Concepts this guide uses

Sources

  1. Alex Xu, System Design Interview, chapter 9: notes by Preslav Mihaylov
  2. Redis documentation: INCRBY
  3. Redis documentation: EXPIREAT
  4. PostgreSQL documentation: CREATE SEQUENCE
  5. Amazon ElastiCache documentation: Caching strategies
  6. MDN: Cache-Control
  7. Wikipedia: HTTP 301
  8. MDN: 302 Found
  9. Wikipedia: URL shortening
  10. Wikipedia: Format-preserving encryption
Sign in to report a mistake