Design CanvasOpen the canvas

Design a Paste Sharing Service

People paste snippets of text, get a short link back, and share it. Most links are read many times after being written once, and a few go viral. Every paste page also shows a sponsored ad, and pastes expire, so something has to find and delete the old ones. How you split those jobs across servers is part of the design.

Constraints

  • Reading a paste should take under 150 ms
  • About 15,000 reads per second at peak, with roughly 1 write for every 20 reads
  • Half of the pages show an ad, but the page must still load if the ad lookup is slow or down
  • Ads come from an external ad network: about 120 ms per call, and a rate limit you buy by the plan
  • Expired pastes stop being served and are deleted by a background job
  • Short links must never collide
  • Keep infrastructure under $10,000 a month

Scenarios to survive

Regular day

Steady read-heavy traffic.

Interviewer asks: Why is this workload a good fit for caching, and where would you run the ad lookup and the expiry sweep?

A paste goes viral (3x)

One link is shared on social media and traffic triples.

Interviewer asks: One key receives most of the extra traffic. How do you keep that single key from overloading a node?

Primary store fails

The main database node is lost.

Interviewer asks: What can still be served while the database recovers, and what is lost?

Ad lookups time out

Whatever runs the ad lookup becomes 20x slower.

Interviewer asks: The ad lookup hangs. What does a reader see, and why does or doesn't it matter where the ad code runs?

Ad network outage

The external ad network stops answering.

Interviewer asks: You do not control the ad network. What does a reader see when it is down, and how do you avoid waiting on it?

What a good design must do

  • Reads meet the latency target at peak
  • At least 98% of requests are served when traffic triples
  • Total monthly cost stays within $10,000
  • Reads are served from a cache or CDN in front of storage
  • Something shows the ad
  • A slow ad lookup cannot slow down reading a paste
  • Pastes still load when the ad network is down
  • Something runs the expiry job
  • Something issues short links, and never gives two pastes the same one