AlgoViz

Design a Pastebin service

easy 45 min

Store 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

0:00 / 3:43

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

  1. Late 1990s

    Pastebins appear to help IRC chat rooms, where long code would flood the conversation.

  2. 2002

    Pastebin.com is created by Paul Dixon on 3 September.

  3. 2006

    Amazon S3 opens on 14 March. Storing large text as objects becomes a service anyone can rent.

  4. 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.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

How it works

Step through the idea, or press Play.

Step 1 of 3

Create. POST the text, get back a link such as `paste.example.com/aB3dE5gH`

Understand it

The drawer of left sketches
DAY1414243444546474849A sketch is left

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

ProductKnown for
Pastebin.comThe best-known name
GitHub GistEvery gist is a Git repository with history; secret gists are unlisted, not private
PrivateBinEncrypts in the browser, so the server cannot read the text
termbinPasting from a terminal
dpaste.comQuick code sharing with an API
Hastebin, GhostbinFast sharing; pastes with a password
We design a service like these, not any one of their real systems.

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

Users reach a CDN for public pastes and a gateway with limits and sign-in. The paste service reads Redis, then PostgreSQL for the record and small text, and object storage for large text. It sends scan, highlight and view events to a queue; workers write highlighted text to object storage and views to ClickHouse. A sweeper removes expired rows and objects.
Blue is reading, pink is writing, orange is background work. Dashed means asynchronous or only on a miss.

Where to read more

Ideas this problem borrows

IdeaIts homeUsed here for
Where bytes liveObject storageText over 8 KB
Read through a cacheCache-aside, TTLHot pastes
Near the readerCDN, invalidationPublic pastes
Unique namesUnique IDsKeys
Safe repeatsIdempotencyCreate after a timeout
Speed limitsRate limiterCreates, misses, passwords
Later workQueues, task schedulerScan, highlight, sweep

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.

Sources