Case study: Pastebin
Overview
Pastebin lets people share text (code, logs, notes) through a link. It looks like the URL shortener, but the payload is much bigger, which changes the storage design. It’s a good exercise in splitting metadata from content.
Try the exercise first: 45 minutes, your own design, then compare.
1. Requirements
Functional
- Create a paste with text (up to 10 MB), optional title and syntax highlighting, visibility (public, unlisted, private) and expiry.
- Read a paste by its link; raw text view.
- Owners can delete their pastes.
Non-functional
- Reads are fast and available; a paste, once created, is durable until it expires.
- Unlisted links must not be guessable.
- Read-heavy (about 10 reads per paste), with occasional viral pastes.
2. Estimates
writes : 5M / day ÷ 86,400 ≈ 60 / s (peak ~200 / s)
reads : 50M / day ≈ 600 / s (peak ~2,000 / s)
size : average ~10 KB (most small, a few large)
storage: 5M × 10 KB = 50 GB / day ≈ 18 TB / yearContent dominates storage, which suggests object storage; metadata is small (a few hundred bytes per paste).
3. API
POST /v1/pastes {"content": "...", "syntax": "python", "visibility": "unlisted", "expires_in": "1d"}
→ 201 {"id": "Xk9fQ2mB", "url": "https://paste.example/Xk9fQ2mB"}
GET /v1/pastes/{id} → metadata + content (or a redirect to a signed content URL)
GET /raw/{id} → text/plain
DELETE /v1/pastes/{id} → 204 (owner only)Large pastes can be uploaded directly to object storage with a presigned URL (week 4).
4. Data model
pastes (metadata DB):
id (PK) | owner_id | title | syntax | visibility | size_bytes
storage_key | created_at | expires_at
content (object storage): key = id → raw text (compressed)5. High-level design
- Create: generate an ID, upload the content to object storage, then insert the metadata row (if the metadata insert fails, a cleanup job removes orphaned objects).
- Read: check metadata (exists? expired? allowed?), then return the content, from the CDN or cache for public pastes.
6. Deep dives
Why split metadata and content?
Storing 10 MB blobs in database rows bloats backups, replication and memory. Object storage is cheaper per GB, scales without limit and serves large objects efficiently. The database keeps only what you query: ownership, visibility, expiry. Very small pastes (a few KB) could be stored inline in the database to save an extra hop, a reasonable optimisation to mention.
IDs and privacy
Unlisted pastes are protected only by the secrecy of the link, so IDs must be random and long enough that guessing is impractical: 8 base62 characters give 2 × 10¹⁴ possibilities; use 10–12 for extra margin. Insert with a uniqueness check (collisions are extremely rare). Private pastes additionally require the owner’s authentication. Never put private content behind a public CDN cache; serve it through the API or with short-lived signed URLs.
Expiry
- On every read, check
expires_atand return 404/410 if passed (lazy expiry). - A background job deletes expired metadata rows in batches, and object storage lifecycle rules or the job delete content.
- Set CDN and cache TTLs no longer than the remaining lifetime.
- “Burn after reading” pastes are deleted on first successful read using a conditional delete.
Caching and hot pastes
Most pastes are read a few times; some go viral when shared widely. Public pastes get Cache-Control headers so the CDN absorbs viral traffic; metadata for hot pastes is cached in Redis.
Abuse and safety
Limit paste size and creation rate per user/IP, scan for malware and leaked credentials (API keys, passwords) and notify or block, and provide reporting and takedown.
7. Trade-offs and bottlenecks
- Two writes per create (object store + database) aren’t atomic: write content first, metadata second, and clean up orphans.
- Object storage adds latency for small pastes; inline storage or caching helps.
- The metadata database is small and can be a replicated relational database for years before sharding is needed.
8. Follow-up questions
- Search public pastes? Index public pastes asynchronously in a search engine.
- Editing with history? Store each version as a new object and keep a versions table.
- Analytics? Log views to a queue and aggregate, as with the URL shortener.
Recap
Pastebin separates small, queryable metadata in a database from large content in object storage. Random, unguessable IDs protect unlisted pastes; public pastes are cached at the CDN while private ones never are; expiry combines checks on read with background cleanup and lifecycle rules; and size limits, rate limits and content scanning keep abuse in check.
Design it yourself (45 minutes)
Set a 45-minute timer and design Pastebin. Users paste up to 10 MB of text and get a link; pastes can be public, unlisted or private and can expire (10 minutes to never). Assume 5 million new pastes per day and 10 reads per paste on average. Use the framework, then compare.
1. Clarifying questions:
2. Functional requirements:
3. Non-functional requirements:
4. Estimates:
5. API:
6. Data model:
7. High-level design:
8. Deep dives (storage split, expiry, privacy):
9. Trade-offs and bottlenecks:A strong answer covers:
- Requirements: create paste (text, syntax, visibility, expiry), read by link, delete.
- Estimates: 5M/day ≈ 60 writes/s; 50M reads/day ≈ 600/s (peaks ~3x).
Average paste ~10 KB → 50 GB/day, ~18 TB/year (bigger if large pastes are common).
- API: POST /v1/pastes -> {id, url}; GET /v1/pastes/{id}; DELETE.
- Data: metadata in a DB (id, owner, visibility, size, expires_at, storage key);
content in object storage (key = id); small pastes could be inline.
- IDs: random 8+ char base62 (unguessable for unlisted pastes).
- Reads: CDN/cache for public popular pastes; private pastes require auth and are
not CDN-cached (or use signed URLs).
- Expiry: expires_at checked on read + background cleanup + storage lifecycle rules.
- Abuse: size limits, rate limits, scanning for leaked secrets/malware.Summary
- Separate small metadata (database) from potentially large content (object storage).
- Use random, unguessable IDs because unlisted pastes rely on the link being secret.
- Serve popular public pastes from a CDN or cache; never cache private pastes publicly.
- Handle expiry with lazy checks on read plus background cleanup and lifecycle rules.
- Limit size and rate, and scan content for abuse and leaked credentials.