Catalog My learning Blog
System Design Roadmap: 100-Day Course for Engineers · Phase 1 · Lesson 10 · 5 min read

System Design: Design Pastebin

On this page
  1. The problem: design a Pastebin
  2. Pastebin requirements
  3. Estimating traffic and storage
  4. The write path and the read path
  5. Key design decisions: ids, storage, caching and expiry
  6. The Pastebin API
  7. What a strong Pastebin design includes
  8. Check yourself

The problem: design a Pastebin

Design a Pastebin: a user submits a block of text and gets a short link; anyone with the link can read it. Optionally, pastes can expire. Before reading on, write down requirements, an estimate and a sketch, using what you've learned in Days 1 to 9.

Pastebin requirements

Functional: create a paste and get a unique URL; read a paste by URL; optional expiry; optional custom name.

Non-functional: reads should be fast, say under 100 ms at p95; a paste must never be overwritten by another; the read path should be highly available; pastes up to 1 MB.

Estimating traffic and storage

Code
Writes   1M pastes/day        ≈ 12/s average,  ~40/s peak
Reads    10 reads per paste   ≈ 115/s average, ~350/s peak
Storage  1M × 10 KB average   ≈ 10 GB/day ≈ 3.6 TB/year

These numbers are small. One database handles 40 writes per second easily, and reads can be cached. Storage grows steadily, so plan where large text lives. Price it too: standard object storage costs around 2 cents per GB per month at the major clouds, so a year of pastes is roughly 70 dollars a month. Cost is a requirement (Day 1), and here it says storage isn't the problem.

The write path and the read path

Write: client sends text → load balancer → API generates a unique id → stores the paste → returns https://paste.example/aZ3kQ9.

Read: client requests the URL → CDN or cache → API → database on a miss → text returned.

Key design decisions: ids, storage, caching and expiry

  • Where text lives. Metadata (id, created, expires) in a relational table; the text itself in object storage keyed by id. Keeps the database small as storage grows by terabytes. At 10 GB a day, storing text in the database also works for a while; say which you'd start with and when you'd switch.
  • Unique, unguessable ids. A paste must never get an id already in use, and anyone with a link can read it, so ids must not be guessable. A database sequence encoded in base62 is unique but enumerable: aZ3kQ9, aZ3kQA, aZ3kQB lets a script read every paste ever made. Use random ids from a cryptographic generator with a uniqueness constraint, and retry on the rare collision. Eight base62 characters give about 218 trillion values; even after a billion pastes, a new id collides about 0.0005 percent of the time. Uniqueness is the one place that needs a strong guarantee (Day 6).
  • Caching. A paste never changes after it's created, so cache it aggressively at the CDN and in Redis. But "never stale" has two exceptions. Expiry: if a paste expires in 10 minutes and the CDN caches it for a day, it stays readable for a day. Set Cache-Control: max-age to the smaller of your normal TTL and the paste's remaining lifetime. Deletion: a user who pasted a password, or a takedown request, needs the paste gone now, so the delete path must also purge the CDN (Day 35).
  • Expiry cleanup. Check expiry on every read, so an expired paste returns 404 even before it's deleted. A background job deletes expired rows in batches, and an object storage lifecycle rule removes the text.
  • Abuse. Anonymous writes attract spam and malware links. Limit writes per IP and enforce the 1 MB cap at the load balancer, not just in code. Day 20 designs the rate limiter.
  • Availability (Days 4 and 5). The read path leans AP: serving a cached paste during a database problem is fine. Redundant API instances and a database replica remove single points of failure.
  • Architecture (Days 2 and 9). One stateless API service, scaled horizontally, with a database, object storage and a cache. Splitting into microservices would add work and buy nothing at this size.
A Pastebin's read-heavy traffic and immutable pastes favor a cached, stateless service, with one component dedicated to generating unique ids.
A Pastebin's read-heavy traffic and immutable pastes favor a cached, stateless service, with one component dedicated to generating unique ids.

The Pastebin API

Code
POST /pastes          body: text, optional expires_in, optional slug  -> 201 { "url": ... }
GET  /pastes/{id}     -> 200 text, or 404 if missing or expired

What a strong Pastebin design includes

  • Numbers that justify keeping it simple.
  • Ids that can't collide and can't be guessed, with a reason.
  • Caching that exploits immutability, with expiry and deletion handled at every layer.
  • Text stored where it can grow, separate from metadata.
  • A clear statement of what changes at 100 times the load: more cache, read replicas, perhaps a CDN for everything.

Check yourself

6 million reads a day divided by 86,400 seconds is about 70 reads per second on average. Applying a peak factor of two to three gives roughly 150 to 200 per second at peak. That's modest: a single well-cached service handles it easily, which shapes the rest of the design.

Pastes never change after they're created, so a cached copy can't become stale. You can cache aggressively at the CDN and in memory with long lifetimes. Only expiry and deletion need handling, for example by giving cached responses a lifetime no longer than the paste's remaining time.

Id generation. Two pastes must never receive the same id, or one would overwrite the other or expose it to the wrong person. Reads can tolerate replica lag for a moment, but ids must be unique, which calls for a single sequence, a coordinated id service or ids random enough that collisions are negligible.

The ids are sequential, so anyone can enumerate and read every paste, including ones users thought were private. Use random, unguessable ids.