← Back to System Design, page 4

system-design-0076 · Easy fresher

Design a Pastebin-style service where users store text snippets and share them with a link, with optional expiry, for 1 million new pastes per day.

Generate a short random key for each paste, store the text in object storage (or a key-value store for small pastes) and the metadata in a database, and serve reads through a cache and CDN. Expiry is handled by a TTL or a cleanup job. The load is small (about 12 writes and 116 reads per second), so the design focuses on clean storage choices, expiry and abuse control.

Full answer

Requirements

Functional:

  • Create a paste (text up to 1 MB) and get a link such as https://paste.example/k7Hq2x.
  • Read a paste by link; optional expiry (10 minutes to never) and "burn after reading".
  • Optional syntax highlighting and private pastes.

Non-functional:

  • Reads are fast and highly available.
  • Keys are hard to guess so private pastes are not discovered.
  • Expired pastes stop being served promptly.

Capacity estimates

Assumptions: 1M new pastes per day, 10 reads per paste on average, 10 KB average size, 5 years of retention for pastes that do not expire.

  • Writes: 1M / 86,400 = about 12 per second.
  • Reads: 10M / 86,400 = about 116 per second.
  • Storage: 1M x 10 KB = 10 GB per day, about 18.25 TB over 5 years.
  • Key space: 8 base62 characters give 62^8, about 218 trillion keys, so random keys rarely collide.

API

POST /v1/pastes {"content": "...", "language": "python", "expires_in": "1d", "visibility": "unlisted"} -> 201 {"key": "k7Hq2x9P", "url": "https://paste.example/k7Hq2x9P"} GET /v1/pastes/{key} -> {"content", "language", "created_at", "expires_at"} GET /{key} -> HTML page DELETE /v1/pastes/{key} (owner only)

Data model

  • pastes: key (primary key), owner_id (nullable), language, visibility, size, content_ref, created_at, expires_at, burn_after_read.
  • Content: object storage at pastes/{key}, or inline in the row if under a few KB.

High-level design

mermaid
flowchart LR
  U[User] --> CDN[CDN for public pastes]
  CDN --> API[Paste API]
  API --> C[(Cache)]
  API --> DB[(Metadata DB)]
  API --> OS[(Object storage)]
  J[Expiry job] --> DB
  J --> OS

Key generation and expiry

Generate 8 random base62 characters from a cryptographically secure random source and insert with "only if the key does not exist"; on the rare collision, try again. Random keys (rather than counters) make links hard to guess.

Expiry: reads check expires_at and return 404 if passed, so expired pastes are never served even before cleanup. A daily job (or the store's native TTL) deletes expired rows and objects. Burn-after-reading pastes are deleted in the same request that returns them, with a conditional delete so two simultaneous readers cannot both see it.

Bottlenecks and trade-offs

  • Small load. A single region with a replicated database and object storage is enough; avoid over-engineering.
  • Caching. Public pastes are cached at the CDN with a TTL no longer than their remaining lifetime; private ones are never cached at shared layers.
  • Abuse. Rate limit paste creation per IP, scan for leaked credentials and malware, and support takedown requests.
  • Inline versus object storage. Inline storage is simpler and faster for small pastes; object storage is cheaper for large ones. A size threshold uses both.
  • Analytics such as view counts are written asynchronously so reads stay fast.

By level

Fresher: Sketch the create and read APIs and a table with key, content location and expiry time. A fresher should explain why random keys are safer than counting numbers.

Mid: Do the math, choose inline or object storage by size, handle expiry on read and in the background, and cache safely. Mention burn after reading.

Senior: Discuss abuse and secret scanning, viral pastes, privacy with caches, retention policy and right-sizing the design. Expect restraint about complexity.

Key points

  • Random 8-character base62 keys with a collision check
  • Metadata in a database, content in object storage or inline
  • Check expiry on read and clean up in the background
  • Cache public pastes at the CDN, never private ones
  • Load is small: about 12 writes and 116 reads per second

Common mistakes

  • Using sequential IDs that make private pastes guessable
  • Relying only on a cleanup job so expired pastes are still served
  • Caching private pastes at a CDN
  • Designing a large distributed system for 12 writes per second

Follow-ups

  • How would you add editing with version history?
  • How do you handle a paste that goes viral?
  • How would you detect leaked passwords in pastes?

Tips

  • Size it honestly; it is small
  • Explain why keys are random
  • Show how expiry works on the read path

Evaluation rubric

  • Correct capacity math
  • Sensible key generation
  • Expiry and burn-after-read handled
  • Caching and abuse considerations