bigonotes
System DesignDesign Problems

URL Shortener (like Bitly)

A URL shortener run through the Interview Approach: create short links, redirect them fast, keep every code unique, and scale reads far beyond writes.

7 min readLast updated

A URL shortener turns a long link into a short one and sends anyone who opens the short link to the original page. It looks small, but it's a good first problem: the reads far outnumber the writes, every code must be unique, and a redirect has to feel instant. This page runs it through the Interview Approach, one step at a time.

1. Functional requirements#

Start with the three actions the system can't do without:

  1. A user gives the system a long URL.
  2. The system computes a short URL for it and returns it.
  3. When anyone opens the short URL, the system maps it to the long one and redirects there.

Each link also has an optional expiration time, after which the short URL stops working.

Out of scope: custom aliases, click analytics dashboards, user accounts beyond a creator ID, and editing a link after it is created.

2. Non-functional requirements#

These four qualities shape the design. Each one gets a number or a clear rule:

Quality Requirement
Scalable In requests and in data: 1 million new links a day, 100 million redirects a day, links kept for years
Availability over consistency Redirects must keep working when parts fail. A brand-new link may take a moment to work everywhere
Uniqueness Two long URLs never get the same short code
Low latency A redirect completes in under 50 ms at p99, not counting the target page

A quick estimate shows where the pressure is:

  • Writes: 1 million ÷ 86,400 ≈ 12 per second. Small.
  • Reads: 100 million ÷ 86,400 ≈ 1,200 per second on average, about 10,000 at peak. Reads outnumber writes 100 to 1, so the read path gets most of the design work.
  • Storage: 1 million links a day for 5 years ≈ 1.8 billion rows. At about 500 bytes each, that's under 1 TB.
  • Code length: 7 characters from a–z, A–Z and 0–9 (base62) give 62⁷ ≈ 3.5 trillion codes, far more than 1.8 billion.

3. Actors#

Two kinds of people use the system, and they use different halves of it:

Actor What they do Access
Creator Submits a long URL and gets a short one back Identified by a creator ID, rate-limited
Visitor Opens a short URL and gets redirected Public, no sign-in

The split matters. Creators hit the write path, visitors hit the read path, and the visitors are 100 times more numerous.

4. API routing#

Two endpoints cover both actors:

Actor Endpoint Response
Creator POST /v1/urls 201 with the short URL
Visitor GET /v1/{shortCode} 302 redirect to the original URL

The create request carries the long URL in the body:

{
  "originalUrl": string,
  "expiresAt": timestamp (optional)
}

Why 302 and not 301? A 301 Moved Permanently is cached by the browser, so the next visit never reaches your servers. That saves load, but you lose the ability to expire a link or count its clicks. A 302 Found is a temporary redirect, so every visit comes back to you. Choose 302 unless saving load matters more than control.

Requests go through a load balancer, which sends POST traffic to the write servers and GET traffic to the read servers.

5. High-level design#

Build the design one functional requirement at a time. It needs only a few boxes.

Create a short URL (requirements 1 and 2). The client sends POST /v1/urls to a write server. The write server computes the short code with a hash function, then writes one row to the database:

Column Type
short_url (primary key) string, the 7-character code
original_url string
expiration timestamp
created_by creator ID

Redirect (requirement 3). The client sends GET /v1/{shortCode} to a read server. The read server looks up the row by short_url, checks it hasn't expired, and returns a 302 with the original URL in the Location header. If the code doesn't exist or has expired, it returns 404.

Read and write servers are separate on purpose. They scale on their own, and a burst of creates never slows down redirects.

6. Address the non-functional requirements#

The basic design works. Now take each quality and find where the design breaks it.

Uniqueness#

A hash of the long URL, cut down to 7 characters, will sometimes give two different URLs the same code. There are two common fixes:

Approach How it works Trade-off
Hash and check Hash the URL, take 7 base62 characters, insert with a unique constraint on short_url. On a collision, add a salt and hash again Collisions get more frequent as the table fills, and each retry costs a database round trip
Counter and encode A global counter hands out the next number, and the write server encodes it in base62. Every number is used once, so every code is unique Codes are sequential and easy to guess. The counter is one shared component

The counter approach is the cleaner way to guarantee uniqueness. To keep the counter off the hot path, each write server grabs a range of numbers at a time, say 1,000, from a counter like Redis INCR, and hands them out locally. If a write server crashes, the unused numbers in its range are skipped, which is fine with 3.5 trillion to spare. If guessable codes are a concern, shuffle the number with a reversible scramble before encoding it.

Low latency#

A database lookup on every redirect works at 1,200 per second, but it adds latency, and most traffic goes to a small set of popular links. Two layers fix this:

  • A Redis cache in front of the database. The read server checks the cache first and only goes to the database on a miss (the cache-aside pattern). Links never change after they're created, so the cache never serves stale data. It only needs a TTL that respects each link's expiration.
  • A CDN at the edge for the most popular links, so many redirects are answered close to the visitor without reaching your servers. This works with a short cache time on the 302 response.

Availability over consistency#

One database is a single point of failure for every redirect. Add read replicas in several availability zones, and run several read servers behind the load balancer. If one replica or server fails, traffic moves to the others.

The trade-off is replication lag: a link created a moment ago may not be on every replica yet. That's the "availability over consistency" choice from step 2. To hide it from the creator, the write server also puts each new link straight into the cache, so the first click usually hits the cache.

Scalable#

The read and write split from step 5 already lets each side scale on its own. For reads, add read servers and replicas. For data, 1 TB fits on one well-sized database for years. When it doesn't, shard by short_url: every redirect already has the code, so each lookup goes to exactly one shard.

Expiration at this scale needs one more piece: a background job that deletes expired rows in batches, so the table doesn't grow forever. The read server still checks expiration on every redirect, so an expired link stops working even before the job removes it.

Whiteboard sketch#

The sketch below is the design from steps 5 and 6. The client posts to a write server, which computes the short code with a hash function and writes it to the database. Redirects go through a read server, which reads from the Redis cache, falls back to the database, and answers with a 302. A CDN sits in front for popular links.

Whiteboard sketch: the client sends POST /urls to a write server with a hash function that computes the short code and writes it to the database (entry: originalUrl, shortUrl, expiration, createdBy). The client sends GET /shortURL to a read server, which uses a Redis cache for faster reads, reads from the database on a miss and returns a 302 redirect. A CDN sits beside the client for popular links

Key takeaways#

  • Three functional requirements: submit a long URL, get a short one back, redirect from short to long.
  • Reads outnumber writes about 100 to 1, so separate read and write servers and put most of the effort into the read path.
  • Seven base62 characters give 3.5 trillion codes.
  • For uniqueness, a counter encoded in base62 is cleaner than hashing. Hand out counter ranges so the counter isn't hit on every create.
  • Use 302 to keep control of expiration and clicks. Use 301 only if saving load matters more.
  • Links never change, so a cache in front of the database is safe and catches most redirects.
  • Choose availability: replicas and several read servers, and write new links to the cache to hide replication lag.
  • Shard by short_url when the data outgrows one database.
Revision notes · 2 min

Core idea#

A URL shortener maps a long URL to a unique short code and redirects visitors from the code to the original. Reads outnumber writes about 100 to 1, so the read path gets most of the design.

Requirements#

  • Functional: submit a long URL, get a short URL back, redirect short to long. Optional expiration.
  • Non-functional: scalable (requests and data), availability over consistency, unique codes, low-latency redirects.
  • Estimate: 12 writes/s, ~1,200 reads/s (10,000 at peak), about 1 TB over 5 years.
  • 7 base62 characters give 62⁷ ≈ 3.5 trillion codes.

Key points#

  • Actors: creator (write path, rate-limited) and visitor (read path, public).
  • API: POST /v1/urls returns 201 with the short URL. GET /v1/{shortCode} returns 302.
  • 302 keeps every visit coming to you, so you can expire links and count clicks. 301 is cached by browsers.
  • Basic design: write server computes the code and stores short_url, original_url, expiration, created_by. Read server looks it up and redirects.
  • Separate read and write servers so each scales on its own.

Step 6 changes#

Requirement Change
Uniqueness Counter encoded in base62. Write servers take ranges of 1,000 numbers
Low latency Redis cache-aside (links never change), CDN for popular links
Availability Read replicas across zones, several read servers. Write new links to the cache to hide lag
Scale Shard by short_url when data outgrows one database
Expiration Check on every read. A background job deletes expired rows

Pitfalls#

  • Truncated hashes collide. Hashing needs a unique constraint and a retry.
  • Counter codes are guessable unless you scramble the number first.
  • Using 301 loses expiration and click counts.
  • Ignoring replication lag: a new link may miss on a replica.

Short line#

Hand out unique codes from a counter in base62, store one row per link, and serve redirects with a 302 from read servers backed by a cache, replicas and a CDN, sharding by code when the data grows.

Found a mistake? Email ask@bigonotes.com