Part 8 of 9 · 3 min read
A small design, from question to wrap-up
One complete run of the four steps on a paste-sharing service, with numbers.
A small design, from question to wrap-up
The question: "Design a service where users paste text and share a short link." (The numbers below are made up to show the method.)
Step 1: Understand.
- Core: create a paste, get a short link, open a link and see the text.
- Later: expiry, editing, accounts. Out of scope: syntax colouring, comments.
- 500,000 new pastes a day. Reads are ten times writes. An average paste is 10 KB. Keep pastes for one year.
Step 2: High-level design.
Estimates:
- Writes: 500,000 ÷ 86,400 is about 6 per second. Peak ×3 is about 18.
- Reads: 5,000,000 a day, about 58 per second. Peak is about 175.
- Storage: 500,000 × 10 KB = 5,000,000 KB = 5 GB a day, about 1.8 TB a year.
These numbers are small. One data store and a few servers are enough. The design:
Client --> Load balancer --> App servers --> Data store (text)
|
+--> Cache (hot pastes)
The load balancer (a front door that spreads requests; Level 1 explains it) is there for safety. The cache is there because reads are ten times writes.
Walk through: a user posts text. An app server makes a short code, saves the text under the code and returns the link. A reader opens the link. The server checks the cache, then the store, and returns the text.
Step 3: Deep dive on the short code.
- Problem: the code must be short, unique and not guessable.
- Option A: random 7 characters from 62 symbols (letters and digits). That gives 62^7, about 3.5 trillion codes. At 500,000 a day for 10 years (about 1.8 billion pastes), collisions are rare, and a check at save time handles them.
- Option B: a counter turned into letters. No collisions, but codes follow a pattern, so they can be guessed, and the counter becomes a single point of control.
- Choice: Option A. Gained: unguessable codes and no central counter. Given up: a small extra check for collisions.
Step 4: Wrap up.
- Bottleneck: the data store, as reads grow. Improve with more cache and copies of the store (Level 4).
- Failure: if the store is lost, pastes are lost. Add copies and backups.
- Operations: watch response time at p99 and error rate. Alert on both.
- Next scale: at 100 times the traffic, split the data across several stores (Level 4).
- More time: expiry, abuse filters, rate limits on creation.
The whole run, as a speech, takes about 20 minutes at an easy pace.
Check yourself:
- How many different 7-character codes can 62 symbols make? Write the number as a power.
- Which part of the example would change first at 100 times the traffic?
Words you will meet
| Word | Plain meaning |
|---|---|
| Framework | A fixed order of steps to follow on every problem of a kind |
| High-level design | A small blueprint with the main parts and flows |
| Deep dive | Detailed work on one or two key parts |
| Bottleneck | The part that limits the whole system |
| Time budget | A plan for minutes per step |