Design a Pastebin service
easy 45 minStore a piece of text, hand back a short link, and delete the text on time.
Video lesson · Chapter 1 of 5
One shared link for a new document
Theory
Read or listen first. About 1 min.
What it is
A paste service stores plain text or code and gives back a short address. Anyone with the address can read it, until the text expires or is deleted.
Think of it as a drawer with a number. The number is easy. The problem is choosing the furniture: small notes and huge logs, public and secret, for ten minutes or for ever.
Why it exists
Long text breaks chat windows and bug reports. One line with a link is easier to send, to read and to throw away later.
It is a favourite interview problem because the first version takes five minutes. After that the questions get better: cost for each request, privacy, deletion and abuse.
A short history
Late 1990s
Pastebins appear to help IRC chat rooms, where long code would flood the conversation.
2002
Pastebin.com is created by Paul Dixon on 3 September.
2006
Amazon S3 opens on 14 March. Storing large text as objects becomes a service anyone can rent.
2020
In December, S3 announces strong read-after-write consistency, so a new object can be read at once.
Words to know
- Paste
- One stored text with its settings.
- Key
- The random characters in the link.
- Unlisted
- Readable by anyone with the link, but not shown in any list.
- Burn after read
- Deleted the first time someone opens it.
- Tombstone
- A small record that says a key was used and is gone.
- Sweeper
- A background job that removes expired data.
- Orphan
- Stored text that no record points to.
Solve it like an interview
New to it? Start with Approach. Revising? Go straight to the mock.
- 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
- 2ScopeAsk what changes the design, then commit to a short list.
- 3NumbersWork out a number when the answer changes what you build.
- 4API and dataThe contract first, then the data that serves it.
- 5DesignStart with the smallest system that works. Change it only when something breaks.
- 6Deep divesCompare the options before you open them, then say which you would pick.
- 7Explain your choicesAnswer first. Then compare your reasoning.
- 8By levelThe same question is judged differently for mid, senior and staff.
- 9MockAnswer against the clock, then score yourself.
How it works
Step through the idea, or press Play.
Create. POST the text, get back a link such as `paste.example.com/aB3dE5gH`
Understand it
Number, drawer, cupboard, date. Four decisions hold the whole design.
Scope
What are we building?
A simple, shareable notepad on the internet, popular with developers for error logs, configuration files, snippets and quick notes. You paste text, get a link like paste.example.com/aB3dE5gH, and send it.
Create
1 byte to 10 MB of text, with a language, an expiry and a visibility.
Read
A page with colours, raw text, a download or an embed.
Forget
Ten minutes, an hour, a day, a week, a month, never, or after one view.
Protect
Public, unlisted or private, with an optional password.
Similar products
| Product | Known for |
|---|---|
| Pastebin.com | The best-known name |
| GitHub Gist | Every gist is a Git repository with history; secret gists are unlisted, not private |
| PrivateBin | Encrypts in the browser, so the server cannot read the text |
| termbin | Pasting from a terminal |
| dpaste.com | Quick code sharing with an API |
| Hastebin, Ghostbin | Fast sharing; pastes with a password |
The hard part
Small traffic, sharp edges
At 12 writes and 58 reads a second, a single database would do. Do not invent scale. Spend the time on what can really go wrong: a secret link that can be guessed, text that lives past its date, two people opening a note that should burn, and a page that runs someone else's script.
The finished system
Where the design ends up
This is the last version. The Design tab begins with one service and one database.
The whole system
Where to read more
Ideas this problem borrows
| Idea | Its home | Used here for |
|---|---|---|
| Where bytes live | Object storage | Text over 8 KB |
| Read through a cache | Cache-aside, TTL | Hot pastes |
| Near the reader | CDN, invalidation | Public pastes |
| Unique names | Unique IDs | Keys |
| Safe repeats | Idempotency | Create after a timeout |
| Speed limits | Rate limiter | Creates, misses, passwords |
| Later work | Queues, task scheduler | Scan, highlight, sweep |
Interview tabs: approach, scope, numbers, API and data, design, deep dives and mock.
Summary
The whole topic in one minute.
A paste service is small in traffic and rich in rules. A dozen writes and sixty reads a second fit on one machine; what shapes the design is size, privacy and time.
Records and small texts live in PostgreSQL, large texts in object storage. Expiry is checked on every read and cleaned up later. Keys are random, longer when the link is the only lock. Everything users write is treated as hostile when it is shown.
- Traffic is tiny. Say so, and start with one service and one database.
- Small text in the row, large text in object storage: requests cost more than bytes.
- If the link is the only lock, 8 characters are too few. Use 12 and limit misses.
- Check expiry when reading. The sweeper only frees space.
- Burn after read needs one conditional update and a button, or a link preview will burn it.
- User text is hostile: escape it, and serve raw text from another domain.