Pastebin
Text Sharing
Design a service where anyone can paste text or code, get a link back, and have the paste disappear on schedule, while keeping it from becoming free file hosting or a malware drop.
Overview
A paste service takes a block of text, stores it, and returns a link. Whoever opens the link sees the text, until the paste expires or its owner deletes it. It looks like a URL shortener, and it shares that problem's short code and read-heavy traffic (see the URL Shortener topic for code generation). This page covers what is different: the content itself.
- Pastes are bodies, not pointers. A short link stores a URL of about 100 bytes; a paste can be anything up to the size limit. Where the bytes live decides cost and read latency.
- Pastes die. Most expire after minutes, days or weeks. The system must stop serving them at exactly the right time, from every cache, and then reclaim the space.
- The link is the password. An unlisted paste has no access check except that nobody knows its key. Keys that can be guessed leak other people's pastes.
- Anonymous uploads attract abuse. Leaked credentials, malware, phishing pages and people using the service as free file hosting.
Questions to ask first
Pin down the scope before designing. Here are the questions worth asking, each with the answer this design assumes.
- How much traffic? 1 million new pastes a day, each read about 10 times.
- How big? 10 KB on average, with a hard limit of 1 MB.
- How long do pastes live? The owner's choice: 10 minutes, 1 hour, 1 day, 1 week, 1 month or never. Half never expire.
- Who can read a paste? Anyone, if it is public (listed); anyone with the link, if it is unlisted; or only those with the password, if it is password-protected.
- Accounts? Optional. Anonymous pastes are allowed, with stricter limits.
- Editing? No. A paste never changes after it is created.
The design at a glance
A request from a browser or curl passes the CDN and reaches the paste API, which enforces limits and checks expiry on every read. Reads try the cache of hot pastes before the metadata DB, which holds each paste's row with small bodies inline, while large bodies live in object storage. New pastes also go to the abuse scanner, and in the background an expiry sweeper deletes expired rows and puts their blobs on the blob GC queue for deletion. The Visual Explanation below draws this design.
What you will be able to do
- Decide where each paste's bytes live and justify the size cut-off.
- Make an expired or deleted paste unreadable at once, everywhere, while reclaiming its space lazily.
- Choose key lengths for public and unlisted pastes from a guessing budget.
- Enforce size limits and rate limits before storing anything.
- Serve untrusted text so it can never run as a web page on your domain.
Key concepts
- Inline vs object storage
- Unguessable keys
- Expiry at read time
- Batched sweeping
- Burn after reading
- Size limits at the edge
- Cache lifetime capped by expiry
- Serving untrusted text safely
- Rate limiting
Functional requirements
- Create a paste with text, an optional title and language, an expiry and a visibility
- Return a link; open the link to read the paste, or its raw text
- Burn after reading: the paste is deleted by its first read
- Password-protected pastes need the password to read
- The owner can delete a paste at any time
- Report a paste; moderators can take it down
Non-functional requirements
- An expired, deleted or taken-down paste is never served again, from the origin or a cache
- Read p99 under about 100 ms for a cached paste (a target we chose)
- A stored paste is not lost before its expiry (durable storage, replicated)
- Unlisted pastes cannot be found by guessing keys within any realistic number of requests
- Rate limits per IP and per account keep abuse from crowding out normal users
Interview questions
- How is this different from a URL shortener?
- Where would you store the paste content?
- How do you make sure an expired paste is never shown?
- How long should the keys be?
- How do you implement burn after reading safely?
- How do you stop the service from being abused?
- Would you deduplicate identical pastes?
- What does the system need at 100 times this traffic?
Sign up free to read the full topic
More System Design topics
- Online Auction
- Online Chess
- Online Coding Judge
- Order Management System
- Outbox Pattern
- Pagination Strategies
- Payment Gateway
- Payment System
- Phi Accrual Failure Detection
- Platform Provisioning on Bare Metal: From Silicon to Kubernetes
- Polling vs WebSockets vs Webhooks
- Pre-Silicon Software: Virtual Platforms and SystemC TLM in CI
Start free on Cariara · Coding problems · System design studio