What it is
A paste service lets anyone store a block of text and share it by link: POST some text, get back /p/9fK2xQ, and anyone with the link can read it until it expires. It looks like a toy, but it is a compact version of a common shape: write-once content, read many times, sometimes by a huge crowd at once. The same design carries over to file sharing, gists, build logs and image hosting.
This lesson builds the design step by step in the 3D scene. The read path and the abuse and failure scenarios run in the simulation; the create, expiry and upload sequences are scripted illustrations.
Why it exists
Requirements shape the design, so pin them down first:
- Functional: create a paste (text, up to 512 KB in this design), read it by id, optional expiry from 10 minutes to "never" (default 30 days), optional owner account; pastes are immutable.
- Non-functional: reads should feel instant worldwide; links must not be guessable; a viral paste must not take the service down; expired pastes must not be readable.
Illustrative estimates (not measurements of any real service):
| Quantity | Arithmetic | Result |
|---|---|---|
| Creates | 5M per day ÷ 86,400 s | ≈ 60 /s average |
| Reads | 10 reads per create | ≈ 600 /s average, several times that at peak |
| Body bytes | 5M × 10 KB average | ≈ 50 GB/day, ≈ 18 TB/year before replication |
| Metadata | 5M × ~200 B | ≈ 1 GB/day |
Two facts fall out: reads dominate requests, and immutable bodies dominate bytes (about 98%). Both point away from "one database holds everything".
How it works
Data split. A metadata row per paste (id, owner_id, size, content_type, created_at, expires_at, object_key, status) lives in a database keyed by id; a key-value or relational store both work at this size. The body lives in object storage under its id. Object storage is cheap per GB, durable through provider-side replication, and serves large immutable objects well; it is a poor fit for queries or small frequent updates, which is what the metadata database is for.
Ids. Random ids of 8 or more base62 characters (62^8 ≈ 2.2 × 10^14) make guessing impractical at this scale; generate them from a cryptographically secure RNG and insert conditionally so a collision is retried. A counter would let anyone enumerate every paste.
Create path. The gateway checks the size, the API generates an id, writes the body to object storage first, then inserts the metadata row, then returns 201. There is no transaction across the two stores, so the order decides the failure: body-first can leave an orphaned object (garbage, cleaned up later); metadata-first can publish a link with no body.
Read path. Readers hit a CDN. On a miss the CDN calls the API, which reads the metadata row (exists? expired? taken down?) and then fetches the body. The response carries Cache-Control: public, max-age=N with N capped at the time left until expires_at, so the edge never serves past expiry.
Expiry. Enforced on read by the metadata check. Cleanup is lazy: a sweeper deletes expired rows in batches using an index on expires_at, then bodies are deleted, either by the sweeper or by an object-storage lifecycle rule (expiry is known at creation, so objects can be tagged or prefixed by expiry date).
Limits and abuse. The gateway rejects bodies over the cap with 413 before reading them (and stops reading once the cap is passed, in case Content-Length lies). Creates are rate-limited per API key, account and IP with a token bucket. If large uploads are ever allowed, the API hands out a short-lived presigned URL and the client uploads straight to object storage.
When to use it
This shape (metadata store + object storage + CDN) fits any write-once, read-many content: pastes, gists, attachments, images, build artifacts, exported reports. The case study is also a standard interview warm-up, close to the URL shortener but with real payloads.
When not to
- If content is edited collaboratively and often, immutable objects behind a long-lived CDN cache are the wrong model; you need versioning or a database designed for updates.
- For tiny values (a URL, a counter), object storage adds a hop for nothing; keep the value in the key-value store, as the URL shortener does.
- For private, per-user content a shared CDN cache is risky; use signed URLs with short lifetimes or skip the CDN for those reads.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Bodies in object storage | Cheap durable bytes, small fast database | Two stores, orphans to clean up, one extra hop on misses |
| Long CDN max-age | High hit ratio, tiny origin load | Slower takedowns and deletes (purge needed) |
| Random ids | Unguessable links | No natural ordering; collisions must be retried |
| Expiry checked on read | Correct even if cleanup lags | A metadata read on every origin request |
| Presigned direct uploads | API never handles big bytes | Validation and scanning must happen after upload |
Alternatives
- Bodies inline in the database for a small service: simplest, fine until size, backup time or read load hurt (the lesson shows that point).
- A key-value store with large values (several hundred KB): workable, but storage cost per GB is usually far higher than object storage.
- Content-addressed storage (key = hash of the body): deduplicates identical pastes for free, but the hash leaks whether a known text exists, and deletion needs reference counting.
- Serverless edge functions with object storage only (metadata in object tags or an edge KV store): very little to run, at the cost of weaker queries over metadata.
Failure modes
- Viral paste without a CDN: every read goes to the origin, object storage fetches queue and requests fail.
- Abuse on the write path: spam creates saturate storage writes and hurt reads that share the same backend.
- Object storage outage: with a CDN, only cache misses fail; without one, everything fails. stale-if-error and cross-region replication reduce it further.
- Metadata database outage: misses fail; CDN hits continue. Read replicas help the read path.
- Orphans: crashes between body and metadata writes leave unreferenced objects; a periodic reconciliation (list objects older than a day with no metadata row) removes them.
- Takedown lag: a malicious paste stays cached at the edge until purged; purge on takedown and keep anonymous max-age short.
In production
Scan new pastes for malware links and leaked secrets (API keys, tokens) asynchronously, and flag or block them; offer a report button and a takedown path that flips status and purges the CDN. Serve pastes from a separate domain from your main site so user content cannot read your cookies, and send Content-Type: text/plain with X-Content-Type-Options: nosniff so browsers never execute pasted HTML. Watch the CDN hit ratio, origin request rate, object-storage error rate, and creates per client.
Common misconceptions
- "Object storage is just a slow database." It is a different tool: durable, cheap, immutable blobs addressed by key, with no queries or transactions.
- "Deleting the row is enough for expiry." Only if the read path checks it and edge caches cannot outlive it.
- "A CDN is only for speed." Here it is also capacity (origin load drops tenfold) and availability (hits survive origin outages).
- "A size limit is a UI concern." It must be enforced at the gateway before the body is read, or one client can tie up your workers with huge uploads.
Interview angle
Lead with estimates that separate bytes from requests, then justify the metadata/object-storage split. Expect follow-ups on id generation, write ordering across two stores, how expiry interacts with caching, how you would handle a viral paste, and how you would stop abuse. Naming what fails and what keeps working during an object-storage outage ("CDN hits continue; misses and creates fail") shows you think in failure domains. The URL shortener interview is a good companion exercise.
Model assumptions
The simulation is a simplified, scaled-down model, not a benchmark; one simulated request stands for many real ones. Version 1 keeps bodies in one database with 8 connections and 35 ms per read. The split design has a metadata database (16 connections, 3 ms reads, 8 ms writes) and object storage modelled as a store with 24 concurrent fetches, 30 ms per GET and 60 ms per PUT (about 800 GETs per second; an illustrative budget, not a provider limit). Readers ask for 10,000 pastes with Zipf(1.3) popularity. The CDN's edge cache is an LRU of 1,000 bodies that stores every answer from the origin, so its hit ratio emerges from the skew (about 90%); the challenge's 60-second max-age is a short TTL that brings it down to about half. A failed or restarted edge cache comes back empty. Misses take a 30 ms hop to the origin. The Paste API has 64 workers and 4 ms of own work. Simulated requests (including the bot's creates) visit the metadata database before object storage; the body-first write order, the expiry sweeper, the 413 rejection and the presigned upload are scripted illustrations. The token bucket in the abuse step stands for the bot's per-key bucket. stale-if-error and cross-region replication are described, not modelled. Arrivals are Poisson, service times lognormal.