SysDesignAtlas

Build Pastebin

The simplest complete system design. One service, one database, one cache — and the exact numbers at which each stops being enough.

HLDEasy8 min readlevel-1storagecache
Confidence

#The product

A user pastes text, gets a short URL, shares it. Anyone with the URL reads the text. Optionally the paste expires, or is private.

That's it. Four operations: create, read, expire, delete.

#Phase 1 — The version that works

Start honestly. For a product with a few thousand users a day:

Client App server Postgres pastes
Phase 1 — one service, one database. This covers most products' entire lifetime.

Schema:

sql
CREATE TABLE pastes (
  id          BIGSERIAL PRIMARY KEY,
  short_code  VARCHAR(8) UNIQUE NOT NULL,
  content     TEXT NOT NULL,
  created_at  TIMESTAMPTZ NOT NULL DEFAULT now(),
  expires_at  TIMESTAMPTZ,
  is_private  BOOLEAN NOT NULL DEFAULT false
);
CREATE INDEX ON pastes (expires_at) WHERE expires_at IS NOT NULL;

API:

   POST /pastes    { content, expires_in, private }  → 201 { code }
   GET  /p/{code}                                    → 200 { content }
   DELETE /pastes/{code}                             → 204

ID generation: a database sequence, base62-encoded. Not Snowflake, not UUIDs — one database means one sequence, and that's genuinely correct here. See Unique ID Generation for why the sophisticated options exist and why you don't need them yet.

How far this goes: comfortably to ~1,000 writes/s and tens of thousands of reads/s on one well-specified Postgres box with a read replica. That covers the entire lifetime of most products.

#Phase 2 — Reads outgrow the database

The trigger: a paste goes viral. One short_code receives 50,000 requests/second while your database happily serves 5,000.

The content is immutable — once a paste is created it never changes. That's the single most important property of this system, because it removes the hard part of caching entirely: a cached copy can never be out of date, so there's nothing to invalidate and no staleness to reason about.

miss Client App Redis ~99% hit Postgres
Phase 2 — content is immutable, so caching needs no invalidation.
   Working set: pastes viewed in the last 24h
   Say 1M pastes/day × 10 KB average = 10 GB
   Hot 20% = 2 GB  →  fits in one Redis node with room to spare

Because content is immutable, there is no invalidation problem. Set a long TTL and never think about it again. Compare this to caching a mutable resource, where invalidation is the hard part — immutability is what buys you the simplicity.

Add a CDN in front for the read path. A CDN is a network of cache servers close to users. A paste is a static document, so those edge servers can answer most requests themselves, and most traffic never reaches your own servers (the origin) at all. Because a given short_code always points to the same content, its URL never needs a version number or a cache purge — you can tell the CDN to cache it for a very long time.

#Phase 3 — Storage outgrows the database

The trigger: average paste size climbs and you're storing 500 GB of text inside database columns. Backups take hours. The memory the database reserves for hot data is now full of paste bodies, pushing out the index pages that actually needed to be there — so unrelated queries get slower too.

The fix: separate metadata from content.

   Postgres (metadata — small, indexed, queryable)
     short_code · created_at · expires_at · size · s3_key

   Object storage (content — large, cheap, CDN-friendly)
     pastes/2026/08/aB3xK9p.txt

This is the same move as the object storage pattern in every media system: the database holds pointers, the object store holds bytes. Your database shrinks from 500 GB to a few GB, backups take minutes, and the working set fits in RAM again.

Keep small pastes (say under 4 KB) inline in the database — the extra round trip to object storage isn't worth it for content that fits in a single row comfortably. That hybrid is a nice detail to mention.

#Phase 4 — Expiry at scale

The trigger: you have 500M pastes and the nightly DELETE FROM pastes WHERE expires_at < now() locks the table for 20 minutes.

Three fixes, in increasing sophistication:

  1. Batch the delete. DELETE ... LIMIT 10000 in a loop with a pause. Simple, effective, and enough for most systems.
  2. Lazy expiry. Check expires_at on read and treat expired pastes as absent. Users stop seeing an expired paste the moment it expires, even if its row is still on disk. Actually deleting the row can then happen whenever it's convenient — it only saves space; it no longer affects what users see.
  3. Partition by expiry month. Store each month's pastes in its own physical table (a partition) behind one logical table. Deleting a month is then just dropping that table — the database updates one piece of bookkeeping instead of visiting 40 million rows. Instant, and nothing gets locked. This is the right answer at large scale.

Combine 2 and 3: lazy expiry makes correctness immediate, partition drops make cleanup free.

#The decision log

Rehearse these. Each is a choice, a rejected alternative, and the deciding constraint.

DecisionRejectedBecause
Database sequence for IDsSnowflake, UUIDOne database means one sequence. Snowflake solves a sharding problem we don't have.
Base62 encodingRaw integers8 chars gives 218 trillion combinations and stays typable.
Cache with long TTLShort TTL + invalidationContent is immutable, so staleness is impossible.
Metadata in SQL, content in object storageAll-in-database500 GB of TEXT destroys backup times and buffer pool efficiency.
Lazy expiry + partition dropsNightly bulk DELETEBulk deletes lock the table; partition drops are instant.
302 redirects (if shortening)301Browsers cache a 301 permanently, so you could never take the link back. A 302 sends every click through you, so you can still disable an abusive paste.

#What you'd cut for v1

Private pastes, syntax highlighting, edit history, user accounts, and analytics. Ship create-and-read with expiry. Everything else is a feature request you haven't validated.

#Explaining the design

You will not get 45 minutes on Pastebin — it's a warm-up. Compress it:

"This is read-heavy with immutable content, which makes it unusually cacheable. I'd start with one Postgres instance and a Redis cache, with content in object storage once average paste size makes the table unwieldy. IDs come from a database sequence, base62-encoded — no need for Snowflake without sharding. The interesting problems are expiry at scale, where I'd use lazy checks plus partition drops rather than bulk deletes, and the viral-paste case, which the CDN absorbs because the content never changes."

Ninety seconds, and it covers storage, caching, IDs, expiry, and the hot-key case (one paste getting a huge share of all traffic). Then ask what they'd like to go deeper on.

#Next

Build an Image Host — same shape, but now the bytes are large and the write path has to do work.

#Check yourself

How far does Phase 1 Pastebin go?Show answer
One Postgres box with a read replica handles ~1,000 writes/s and tens of thousands of reads/s — most products' entire lifetime. Name when you'd revisit (~30,000 writes/s, or the table outgrowing the buffer pool).
Why a database sequence for paste IDs?Show answer
One database means one sequence; Snowflake solves a sharding problem you don't have. Base62 makes it short — 8 characters give 218 trillion codes.
What makes Pastebin's cache trivial?Show answer
Content is immutable, so a cached copy can never be stale — long TTLs, no invalidation — and a CDN can absorb a viral paste.
Why move paste bodies to object storage?Show answer
500 GB of TEXT makes backups take hours and fills the buffer pool with bodies, evicting index pages and slowing unrelated queries. The database keeps pointers; object storage keeps bytes; small pastes stay inline.
How do you expire hundreds of millions of pastes?Show answer
Lazy expiry (treat expired as absent on read) for immediate correctness, plus partitioning by expiry month so cleanup is dropping a partition — instant, no locks. Batched deletes suffice at smaller scale.
How do you present Pastebin in ninety seconds?Show answer
Read-heavy, immutable, so very cacheable; one Postgres plus Redis, bodies in object storage once large; sequence IDs; lazy expiry with partition drops; the CDN absorbs viral pastes.

#What to carry forward

  • Start with one service and one database, and say when you'd change.
  • Immutability removes cache invalidation entirely.
  • Databases hold metadata and pointers; object storage holds bytes.
  • Lazy expiry plus partition drops beats bulk deletes.
  • Warm-up questions are graded on scoping and compression.
00:00
/ 8m