# URL shortener - one write, a million reads

> Why one tiny write and a hundred cacheable reads make two systems sharing a single table. Uniqueness is bought with a single global counter: every write in…

Source: https://tierslate.com/templates/url-shortener

Infrastructure primitives · System design · starter · 8 step walkthrough · 2 tables

Why one tiny write and a hundred cacheable reads make two systems sharing a single table.

**Own this board: 200 credits.** The first act plays on the page. Owned once with credits, never a subscription.

## What this board gets wrong on purpose

The tension: Uniqueness is bought with a single global counter: every write in the world waits on one INCR, that counter is the only node here with no replica, and base62 of a counter makes every link on the service enumerable by walking the range. Read scale is bought with the opposite mistake - the CDN caches the 301, so after the first visitor a deleted or re-pointed link keeps redirecting until the TTL expires, and the clicks that never reach the service are never counted.

## Practise this as an interview

Take URL shortener - one write, a million reads as a timed system design interview on a blank board - on your own, with the requirements to check yourself against, or with a candidate you invite.

## Start here

### Why this design

The URL shortener is the most common opening question in a system design interview because the product fits in one sentence and the hard parts do not. It probes whether you start from requirements and numbers, whether you see that a 100:1 read-to-write ratio means two different systems sharing one table, and whether you can defend a key-generation scheme and an HTTP status code under questioning. In real life the same shape - tiny write, enormous cacheable read - is every link tracker, QR code service, feature-flag lookup and DNS-like resolver.

### What you will learn

- How to turn 100M new links a month into ~40 writes/s, ~3,900 redirects/s on average and ~19,000/s at peak - and why bandwidth is not the constraint here.
- Why a 7 character base62 key (62^7 ≈ 3.5 trillion) outlasts ten years of 1.2 billion links a year with 99.7 percent of the space unused.
- Counter-based keys against random keys: collision-free and short versus no single point of failure and not enumerable - and the range-leasing middle ground.
- Why the read path sits behind two caches, and what a cached redirect costs: stale deletes and clicks that are never counted.
- 301 against 302: the status code decides your server load, your analytics accuracy and whether you can ever take a link down.

### How to read this board

1. Diagram first. WRITE PATH · CREATE A LINK across the top: author, client, the Shortener API hex, the key encoder and the red counter.
2. Then DATA on the right: the link store, written once by the top half, and the hot key cache in front of it for the bottom half.
3. Then READ PATH · FOLLOW A LINK across the bottom: visitor, CDN edge, the redirect service hex, and the 301 looping back. Then ANALYTICS · OFF THE PATH: click events, click store, link analytics. Press play for the eight step walkthrough.
4. Then the pages in order: 1 · Requirements, 2 · Capacity estimation, 3 · Storage estimation, 4 · Availability, 5 · How it works, 6 · Deployment plan.
5. Last, DATA MODEL under the pages: the links row and the click_daily rollup, as real tables that Generate SQL turns into DDL.
6. The conversation to have is the red box and the CDN. Uniqueness is bought with one counter that has no replica and makes every link enumerable; read scale is bought with an edge-cached 301 that keeps deleted links alive and never counts the clicks it serves. Page 5 argues both trades; decide which you would change first.

## 1 · Requirements

Write the requirements before drawing anything: the two non-functional lines about read latency and key uniqueness decide nearly every box on the diagram.

### Functional

- Create: given a long URL, return a short link https://sho.rt/{key}. The same long URL submitted twice may return two different keys unless the caller asks for dedup.
- Redirect: GET /{key} answers with an HTTP redirect to the long URL, or 404 if the key is unknown, deleted or expired.
- Optional custom alias (sho.rt/launch) and optional expiry date, both chosen at creation.
- The owner can delete or disable a link. Abuse reports can disable one centrally (malware, phishing).
- Per-link analytics: clicks by day, referrer and country - approximate is acceptable.
- Authenticated API with per-key rate limits for creation, since a shortener is a favourite tool for spam.

### Non-functional

- Redirect latency: p99 under 50 ms at the origin, under 20 ms when served from the edge. A redirect is an extra hop before the page the visitor actually wanted.
- Redirect availability 99.99 percent (about 4 minutes a month). A dead redirect breaks every link ever printed on a poster or in an email.
- Create availability 99.9 percent. A failed create is retried by a person; a failed redirect is not.
- Uniqueness is absolute: two links may never share a key, and a key is never reused after deletion.
- Durability: a link that has been returned to its author must survive any single machine, zone or region loss.
- Reads may be eventually consistent by seconds; a newly created link must work on its first click (read-your-write for the author).
- Analytics are best-effort and eventually consistent; they must never slow a redirect.

### Out of scope

- Link previews, QR rendering, branded domains per customer and A/B routing - each is a feature on top of the same lookup.
- Exact, billable click counts. If clicks are money (ad networks), the design changes: no edge caching and a durable click log.
- Content scanning of the destination page beyond a blocklist check at create time.

## The rest of this board

This page publishes the question. The answer - 5 more written pages, an 8-step narrated walkthrough and a 2-table schema - is the board itself, and it opens in Tierslate.

- 5 written pages
- 8 walkthrough steps
- 2 tables (10 columns)

5 pages behind this one:

- Capacity estimation
- Storage estimation
- Availability
- How it works
- Deployment plan

## Related system design templates

- [Distributed cache system design](https://tierslate.com/templates/distributed-cache) - Why a hash ring moves a sliver of keys when a node leaves, and why the hot key and the miss storm remain.
- [Key-value store system design](https://tierslate.com/templates/key-value-store) - How a key finds its replicas, and why choosing how many to wait for buys overlap rather than order.
- [Rate limiter system design](https://tierslate.com/templates/rate-limiter) - Why the hard part of limiting requests is where the shared counter lives, not the algorithm.
- [Unique ID generator system design](https://tierslate.com/templates/unique-id-generator) - How 64 bits buy sortable ids with no central counter, and what happens when the clock goes backwards.
- [Web crawler system design](https://tierslate.com/templates/web-crawler) - Why a crawler that feeds on its own output needs politeness, deduplication and loop brakes more than speed.
- [Pastebin system design](https://tierslate.com/templates/pastebin) - Why storing text is the easy part and expiry, abuse and the read path are the design.
- [Load balancer system design](https://tierslate.com/templates/load-balancer) - How requests are spread across servers, and why the health check matters more than the algorithm.
- [API gateway system design](https://tierslate.com/templates/api-gateway) - Why auth, routing and limits move to one front door, and what that door costs when it falls over.
- [Login and sessions system design](https://tierslate.com/templates/login-sessions) - How a password becomes a session, and what changes when the session is a signed token nobody can revoke.
- [Feature flags system design](https://tierslate.com/templates/feature-flags) - Why a flag is evaluated locally from a pushed ruleset, and what a stale ruleset does during an incident.
- [Secrets manager system design](https://tierslate.com/templates/secrets-manager) - How secrets are encrypted under a key nobody holds directly, and why rotation is the feature, not storage.
- [DNS system design](https://tierslate.com/templates/dns-resolver) - How a name becomes an address through layers of caches, and why the TTL decides how fast a mistake spreads.
- [Distributed cron system design](https://tierslate.com/templates/cron-scheduler) - Why a scheduled job needs a lease so it runs once, not once per server, and what a missed tick should do.
- [Background job queue system design](https://tierslate.com/templates/job-queue) - Why a job queue delivers at least once, and how idempotent workers and dead letters keep retries honest.
- [Code execution sandbox system design](https://tierslate.com/templates/code-execution-sandbox) - How untrusted code is run in isolated, time-limited sandboxes, and why the queue in front of them is the real system.
- [Distributed log system design](https://tierslate.com/templates/message-queue) - Why an append-only partitioned log gives ordering and replay, and what replication and consumer offsets cost.
- [Consensus system design](https://tierslate.com/templates/raft-consensus) - How Raft keeps replicas agreeing through elections and a replicated log, and what a network partition really does.
- [Object storage system design](https://tierslate.com/templates/object-storage) - How objects are split, erasure-coded and placed across failure domains so losing disks never loses data.

All 100 system design templates: https://tierslate.com/templates (Markdown: https://tierslate.com/templates.md)
