LOAD-BEARINGfree
Architecture labTier 13 · unit 1 of 31

URL shortener

Users submit a long URL and get a short code back. Anyone hitting the short code is redirected. Reads dominate overwhelmingly and must be fast everywhere.

Envelope100M new links/yr · 100:1 read:write · p99 < 100 ms redirect

What the design must do

  • Create a short code for any URL
  • Redirect a short code to its target
  • No collisions, no central counter bottleneck
  • Redirects served from cache, not the database

The worked envelope

Assumed. 100M new links a year, 100:1 read:write, average target URL 200 bytes.

Worked envelope
Writes100M ÷ 31.5M s/yr ≈ 3/s, peak ~10/s
Reads100× writes ≈ 320/s, peak ~1,000/s
Row sizecode 7 B + URL 200 B + owner, timestamps ≈ 500 B
Storage100M × 500 B = 50 GB/yr; 250 GB over five years
Hot set10% of links carry ~90% of reads → 10M × 300 B ≈ 3 GB
Egress1,000/s × ~500 B of redirect headers ≈ 500 KB/s

What the numbers decide. Every figure here is small. Three writes a second is a laptop. Fifty gigabytes a year is one disk, for a decade. The entire hot set fits in three gigabytes of Redis, which is a single small instance with room to spare.

So the arithmetic has already ruled out most of the architecture you were tempted to draw: no sharding, no queue, no eventual consistency. What it has not ruled out is the cache, because the constraint that survives is p99 < 100 ms on the read path, and that is about tail latency rather than volume. This is the lab where estimation earns its keep by telling you to stop.

The decisions, and what each one costs

Base62 of a distributed unique ID for the short code

Because
3 writes/s needs no coordination, but a central counter row is a single point of contention forever, and hashing the URL collides — and wrongly merges two users who shortened the same link
The cost
Codes are roughly sequential, so they are guessable and enumerable; a vanity-code path has to be handled separately
Rejected
Random-with-retry, which adds a read before every write to buy unguessability nobody asked for

Cache-aside on Redis in front of Postgres, long TTL

Because
The hot set is 3 GB and the read:write ratio is 100:1, so a hit rate above 95% is free and turns the p99 budget from a database problem into a network problem
The cost
An edited or deleted link stays live until the TTL expires unless you invalidate on write
Rejected
Read replicas — same storage engine, same milliseconds, and replica lag on a key-value lookup buys nothing

One unsharded Postgres table keyed by short code

Because
50 GB/yr means the working set stays in the buffer pool for years, and sharding permanently removes the transactions the vanity-code path needs
The cost
A single write primary, so a failover is visible; growth past five years needs a plan
Rejected
Sharding on day one, which Lesson 0.03 puts at the bottom of the ladder for exactly this reason

302 redirect rather than 301

Because
A 301 is cached by the browser and never seen again, which destroys the click analytics the product exists to sell
The cost
Every click costs a request; the CDN cannot absorb them for you
Rejected
301 with a separate analytics beacon — an extra request, an ad blocker away from silence

Components in the reference design

Client
Browser or mobile app
Load Balancer
L7 routing, TLS termination, health checks
App Service
Stateless business logic
Cache
Redis / Memcached, sub-ms lookups
SQL DB
Relational, ACID, joins
ID Generator
Snowflake-style distributed unique IDs

What not to draw

  • Search Index — A search index solves nothing here — every lookup is an exact key match on the short code.

The reference design

Code generation. Take a distributed unique ID (Snowflake: timestamp + machine ID + sequence) and base62-encode it. Seven base62 characters give 3.5 trillion codes. No collision checks, no coordination, and codes stay roughly time-ordered which helps index locality.

Hashing the URL instead is tempting and wrong: hashes collide, and two users who shorten the same URL should still get separate links with separate analytics.

Read path. This is 99% of traffic and it is a pure key-value lookup, so it should never reach the relational store. Cache-aside on Redis with a long TTL gives a hit rate above 95% because link popularity follows a power law. The database is the fallback and the durable record.

Storage. A single table keyed by short code. At 100M links a year with ~500 bytes per row that is 50 GB/yr, which one Postgres instance handles comfortably for years. Do not shard this on day one.

301 or 302? A 301 permanent redirect lets browsers and proxies cache it, which cuts your traffic dramatically — and destroys your click analytics, because you never see the repeat visits. Use 302 if analytics matter, 301 if they do not. This is the tradeoff the question is really asking about.

Custom aliases need a uniqueness check, so they take a different path: insert with a unique constraint and let the database reject duplicates, rather than checking then inserting.