sehaj/learn
System design/01 The approach

01.06 · concept · 9 min

Trade-offs: there is no best design

Every design choice buys something and pays for it somewhere else. The common trade-offs, and how to talk about them.

There is no best design

"What's the best database?" has no answer. "What's the best database for 10 writes a second, strict consistency and a team that already knows Postgres?" does. A design is only good relative to its requirements, and every choice improves one thing by making another worse.

That's why interviewers care so much about trade-offs. Anyone can list components. Knowing what each one costs, and when the cost is worth paying, is the skill.

The trade-offs you'll meet most

You can have more……but you pay withExample
Consistencylatency and availabilityWaiting for every copy to confirm a write before answering.
Read speedwrite speed and storageEach extra index or precomputed copy speeds reads and slows writes.
Availabilitycost and consistencyMore copies in more places, which may disagree for a moment.
Low latencyfreshnessServing from a cache means sometimes serving old data.
FlexibilitysimplicitySplitting into many services lets teams move apart, and adds network calls and failure modes.
Controlyour timeRunning your own database versus paying for a managed one.

You'll meet each of these properly in Part 2. For now, notice the shape: nothing on the left is free.

A small trade-off with big effects: 301 or 302?

When someone opens a Snip link, Snip answers with an HTTP redirect. There are two common kinds:

  • 301 Moved Permanently tells the browser "this short link always goes there". Browsers remember it, so the next time the same person clicks, the browser goes straight to the long URL without asking Snip.
  • 302 Found (a temporary redirect) tells the browser "go there this time". Every click comes back to Snip.

Step through what happens when the same person clicks a link twice:

The second click, with a 301
BrowserSnipLong URL's site1 GET /x7Kp2 301 → example.com/…3 GET example.com/…later: same person clicks again4 cached 3015 GET example.com/…

5 messages. Use Next and Back to follow them in order.

Text version
  1. Browser → Snip: GET /x7Kp — First click: the browser asks Snip where the short link goes.
  2. Snip → Browser: 301 → example.com/… (reply) — Snip counts the click and answers with a permanent redirect. The browser stores it.
  3. Browser → Long URL's site: GET example.com/… — The browser follows the redirect.
  4. Note over Browser: later: same person clicks again
  5. Browser → Browser: cached 301 — The browser already knows where /x7Kp goes, so it doesn't ask Snip at all.
  6. Browser → Long URL's site: GET example.com/… — Straight to the long URL. Fast for the user, and Snip never sees this click.

So the choice is a trade-off:

301 (permanent)302 (temporary)
Repeat clicksSkip Snip entirelyAlways go through Snip
Load on SnipLowerHigher
Speed of repeat clicksFasterOne extra round trip
Click countsMiss repeat clicksEvery click counted
Changing the target laterHard: browsers cached the old oneEasy

Which is right? It depends on the requirements. Snip promised owners click counts (01.02 Functional and non-functional requirements), so Snip uses 302, and pays for it with more traffic, which the estimate in 01.05 Back-of-the-envelope estimation says it can afford. A shortener with no analytics might rightly choose 301.

Predict first

A link shortener's main selling point is accurate click analytics. Which redirect should it return?

Commit to an answer before you read on.

How to talk about a trade-off

A good answer has four parts. Use it whenever you make a choice in a design:

  1. The options: "We could return a 301 or a 302."
  2. What each buys and costs: "301 cuts our load but loses repeat clicks; 302 counts everything but every click hits us."
  3. The choice and the requirement behind it: "Owners need click counts, so 302."
  4. What would change your mind: "If we dropped analytics, or load became a problem, I'd revisit it."

The fourth part is what makes it sound like judgement rather than a memorised answer.

Recap

Interview questions

JuniorfreqGood to knowDesign

Should a URL shortener return a 301 or a 302 redirect?

Report a mistakeEdit this page