Design a text sharing service like Pastebin: a system design walkthrough
Pastebin is a good second problem after the URL shortener. You paste some text, get a short link, and anyone with the link can read it until it expires.
Most of it will feel familiar, and that is the point. The interesting part is the one thing that changes: the size of the value.
Requirements to pin down
- Create a paste and get a short link back.
- Read a paste by its link.
- Optional expiry: ten minutes, a day, a month, or never.
- Optional: unlisted pastes that only people with the link can find.
Non-functional: reads far outnumber writes; a popular paste may be read many times a second; once a paste expires, it must be gone.
Estimate, with assumptions you say out loud
Say 1 million new pastes a day, about 12 a second on average (1,000,000 / 86,400). Say the average paste is 10 KB. That is 10 GB of new text a day, about 3.65 TB a year if nothing expired.
Suppose reads are ten times writes: about 120 a second on average, with popular pastes far above that.
The conclusion: the metadata is small, the bodies are not, and reads are concentrated on a few popular pastes. Each of those shapes a choice below.
What carries over from the URL shortener
- Short ids without collisions. Lease ranges of ids from a counter and encode them in base62, so there is no collision check on create. For unlisted pastes, permute the numbers with a secret key so consecutive pastes do not get consecutive links. Seven base62 characters give 62^7, about 3.5 trillion, ids.
- A cache-first read path. Popular pastes are read from memory, not from the database.
If you have practised the URL shortener, say this early. Reusing a known answer and naming what is different is exactly what interviewers want to see.
What changes: the value moves out of the database
A long URL is a couple of hundred bytes and belongs in the row. A paste is kilobytes to megabytes. Putting it in the row makes every metadata read drag the body along, and fills the database with data it does not need to query.
So the row holds metadata: the id, the owner, the creation and expiry times, and a storage key. The body goes into object storage, which is cheap, durable and made for whole-object reads.
Reads then take two steps: metadata from the cache, then the body from a CDN in front of object storage. Because the body never changes, it caches perfectly, and a popular paste costs your servers almost nothing.
What is genuinely new
Expiry deletes the bytes, not just the row. If you only delete the database row, the object lives on, and you pay for storage that nothing references. A lifecycle rule in the object store saves you running a cleanup job: write each body under a prefix or tag for its expiry choice (a day, a month) and let the rule delete objects of that age. Rules count age in whole days and run in the background, so a ten-minute paste's bytes can outlive it by a day or more; every read must check the expiry time and refuse an expired paste.
Pastes are untrusted documents. Anyone can paste HTML with a script in it. If you serve it as HTML from your main domain, every paste is a cross-site scripting attack on your users. Serve bodies as plain text, from a separate origin that holds no cookies or sessions.
How the answer grows with level
- Junior: the API, the split between metadata and object storage, expiry that deletes both, and plain-text serving.
- Senior: the numbers above, the CDN with sensible cache lifetimes so a deleted paste does not linger, id generation that is not guessable in sequence for unlisted pastes, and abuse limits on create.
- Staff: abuse at scale, takedowns that reach every cache quickly, regions, and cost per million reads.
Mistakes that sink this question
- Storing paste bodies in the database row.
- Deleting the row on expiry and leaving the object behind.
- Serving pastes as HTML from the main domain.
- Sequential ids on unlisted pastes, which lets anyone walk through all of them.
Practise it
Read the free design, Design a Text Sharing Service like Pastebin, a single diagram with a write-up. Read Design a URL Shortener first; Pastebin is easiest to learn as the difference between the two.
With Pro ($10/month) you can draw your answer on the practice canvas and get an AI review against the rubric.
Questions people ask
Why not store paste bodies in a key-value database instead of object storage?
You can for small pastes, but item size limits and cost push large bodies out. Object storage is cheaper per gigabyte, has no practical size limit for text, serves whole-object reads well and sits naturally behind a CDN. Keep the database for what you query: ids, owners and expiry times.
How do you make an unlisted paste hard to find?
Make its id unguessable. Either generate a long random id, or permute sequential ids with a secret key so consecutive pastes do not get consecutive links, and rate-limit lookups of ids that do not exist.
Does a CDN make expiry harder?
Yes. A cached copy can outlive the paste. Set the cache lifetime no longer than the time left before expiry, and invalidate the CDN when a paste is deleted early.