Case Studies
Design a URL Shortener
The classic warm-up: turn long links into short codes and redirect billions of clicks. From requirements and estimates to a design that grows from one database to caches and edge redirects.
Beginner 5 min read· Updated 2026-11-18
What you will learn
- Turn vague requirements into numbers: traffic, storage, read/write ratio.
- Choose a short-code scheme that is unique, compact and hard to enumerate.
- Scale a read-heavy system with caching and edge redirects, step by step.
- Handle analytics, expiry and abuse without slowing redirects.
Before you start
Requirements
- Functional: create a short link for a long URL (optionally a custom alias and expiry); visiting the short link redirects to the long URL; owners see click counts.
- Non-functional: redirects are fast (p99 < 100 ms worldwide) and highly available; codes are short and not guessable in sequence; links never point to the wrong target.
- Out of scope for now: editing targets, per-link access control, rich analytics dashboards.
Back-of-the-envelope estimates
| Quantity | Assumption | Result |
|---|---|---|
| New links | 100 M per month | ~40 writes/s (peak ~200/s) |
| Redirects | 100:1 read/write ratio | ~4,000 reads/s (peak ~20,000/s) |
| Storage | 5 years × 100 M × 12 × ~500 B | ~3 TB — fits a single partitioned database for years |
| Code length | 62 characters (a–z, A–Z, 0–9) | 7 characters ≈ 3.5 × 10¹² codes |
The numbers say the problem is reads, not writes or storage: a redirect must be answered thousands of times per second, mostly for a small set of popular links. That shapes everything that follows.
API and data model
-- POST /api/links {"url": "https://example.com/very/long", "alias": null, "expiresAt": null} → 201 {"code": "aZ3k9Qx"}
-- GET /{code} → 302 Location: https://example.com/very/long
CREATE TABLE links (
code varchar(10) PRIMARY KEY,
long_url text NOT NULL,
owner_id bigint,
created_at timestamptz NOT NULL DEFAULT now(),
expires_at timestamptz
);Generating short codes
| Approach | Pros | Cons |
|---|---|---|
| Hash the URL (MD5/SHA) and take 7 chars | Same URL → same code; no coordination | Collisions must be detected and retried; a custom alias still needs a check |
| Database sequence, base62-encoded | Simple and unique | Sequential codes are guessable; one sequence is a write hotspot |
| Pre-generated ID ranges per server (or Snowflake IDs) | Unique without per-request coordination; scales writes | Must shuffle or encrypt the number so codes are not enumerable |
| Random 7–8 chars + insert-if-absent | Unguessable, trivially distributed | Rare collision retries; slightly longer codes for safety |
Evolve the design
Play each version, watch where it breaks, then move to the next. The simulation uses 1,500 requests/s — scaled down, but with the same shape: 99% redirects, a few very popular links.
Stage 1 of 3
API + one database
- Current architecture
- Every redirect reads the code from the database.
- Limitation
- Read capacity equals database capacity: ~1,100 lookups/s on 10 connections.
- Bottleneck
- The database connection pool: it is 100% busy, a third of redirects time out.
- Improvement
- Popular links are read over and over — keep them in memory.
- Performance impact
- ~67% success, p95 ≈ 300 ms.
- Complexity & operational cost
- One app tier and one database.
- Trade-off
- Simplest possible design, capped by the slowest component.
Loading simulation…
Deep dives
- 301 vs 302: a 301 is cached by browsers forever — great for load, terrible if you need click counts or to disable a link. Most shorteners use 302 (or 307) with a CDN TTL.
- Click analytics: never write to the database on the redirect path. Publish a click event to a log (Kafka) and aggregate asynchronously.
- Abuse: rate-limit link creation per user and IP (rate limiting), scan targets against malware lists, and be able to purge a code from every cache quickly.
- Growth: when one database is no longer enough, shard by code — every lookup carries the code, so routing is trivial.
Trade-offs
| Benefits | Costs | |
|---|---|---|
| Caching | Absorbs the read-heavy, skewed traffic cheaply. | Changes and takedowns propagate only as fast as TTLs and purges. |
| Code scheme | Random codes are unguessable and easy to generate anywhere. | Slightly longer codes and collision handling. |
| Edge redirects | Single-digit-millisecond redirects worldwide. | Analytics and control move partly to the CDN. |
Interview checklist
- State the read/write ratio early — it justifies caching and the CDN.
- Pick a code scheme and explain uniqueness and enumerability.
- Keep the redirect path read-only and fast; push analytics to an async pipeline.
- Mention expiry, custom aliases, abuse handling and how you would shard later.
Further reading
Related topics
Design to Scale
Caching Strategies
Put a cache in front of a struggling database: how cache-aside works, what drives the hit ratio, how to keep data fresh, and how to survive the moment the cache goes cold.
Design to Scale
Content Delivery Network (CDN)
Cache content at edge locations close to users, so most requests never cross an ocean or touch your origin — and design pages so they can actually be cached.
Design to Scale
Database Sharding
Spread data across several independent databases by a shard key to scale writes and storage beyond one machine — and avoid the classic traps: hot shards, cross-shard queries and painful resharding.
Microservice Patterns
Rate Limiting
Cap how many requests a client may make per unit of time to protect services from bursts and abuse — and pick the algorithm whose behaviour at the edges you can live with.
Design to Scale
Kafka Partitioning
Partitions are Kafka's unit of parallelism and ordering. Choose the message key and partition count well and consumers scale linearly; choose badly and one hot partition holds everything back.