AlgoViz

Choosing and defending

easy 30 min

Every box on the board is a choice.

Video lesson · Chapter 1 of 5

A green seat at nine o'clock

0:00 / 3:45

Choice, reason, cost, trigger

1:20Reel coming soon

Theory

Read or listen first. About 1 min.

What it is

Design is a chain of decisions. Choosing is picking one option for a stated reason. Defending is explaining it, with its cost, to someone who asks hard questions.

A complete decision has four parts: the choice, the reason, the cost and what would change it.

Why it exists

There is rarely one right answer, so interviewers look at how you decide. A list of options without a choice leaves them guessing.

At work the same habit saves later engineers from asking why something was done.

A short history

  1. 1772

    Benjamin Franklin describes weighing pros and cons on a sheet of paper in a letter to Joseph Priestley.

  2. 2000

    Eric Brewer presents the CAP conjecture, a famous trade-off between consistency and availability when the network splits.

  3. 2011

    Michael Nygard proposes short architecture decision records.

Words to know

Option
One way to solve the problem.
Cost
What the choice makes worse.
Trigger
The fact that would change your mind.
Reversible
Easy to undo later.
Constraint
Something you cannot change.

How it works

Step through the idea, or press Play.

Step 1 of 3

Choose. Two or three options, one pick, one reason with a number

Understand it

Start here

How should Sam travel?

Nobody asks "which is better, a bike or a bus?" without saying where they are going. Technology is the same. An option is only good for a trip.

The trip decides
WalkBikeBusCarFour ways to go

The four parts

What a full decision sounds like

  1. 1

    Choice

    One option, named. "I will cache the seat map."

  2. 2

    Reason

    A requirement or a number. "We expect 5,000 map reads a second, mostly the same maps."

  3. 3

    Cost

    What gets worse. "The map can be two seconds old."

  4. 4

    Trigger

    What would change your mind. "If readers must see every seat change at once, I would push updates instead."

From need to decision

A decision

  • "I choose a unique rule in the database."
  • "Because two readers must never hold one seat."
  • "It costs a refused write when two people race."
  • "If one database cannot take the writes, I would split by building."

Not a decision

  • "We could use SQL or NoSQL."
  • "Kafka, because it scales."
  • "It is the industry standard."
  • "This has no downsides."

Compare

Three options in one minute

Draw a tiny table in your head. Columns are the two needs that matter most here, never ten. If one option fails a need, drop it and say why. If two pass, take the one that is simpler to run.

School bus tracker: how do parents see the bus?

OptionPosition at most 10 s old?Cheap at 20,000 parents?Verdict
Reload the page by handNoYesbreaks
App asks every 10 secondsYesYes, 2,000 requests/sstrong here
Server pushes on an open connectionYes, about a secondYes, but 20,000 connections to look afterworks; more than we need
Assumptions: 20,000 parents watch at 7:45 and a position may be 10 seconds old.

The number that decides

20,000 parents10 s=2,000 requests/s,20,0001 s=20,000 requests/s\frac{20{,}000\ \text{parents}}{10\ \text{s}} = 2{,}000\ \text{requests/s}, \qquad \frac{20{,}000}{1\ \text{s}} = 20{,}000\ \text{requests/s}
At a 10 second target, polling is easy. If the target became 1 second, the load is ten times higher and push becomes attractive. That is the trigger.

Grade

Breaks, works, strong

Three grades for any option

  • Breaks

    It fails something we agreed on. You can name the request, the number or the failure.

  • Works

    It meets the need today, with a limit you can name. Often right for a smaller system.

  • Strong

    It meets the need with room to grow and a cost we accept. It is strong for this problem only.

Grading an option

Recipe sharing: resizing photos

OptionGradeWhy
Resize while the cook waitsworks2 s of waiting is tolerable at first; a burst of uploads blocks web requests
Put a job on a queue, workers resizestrongUpload returns at once; bursts wait in the queue; needs a retry rule and a "processing" state
Resize on the phone onlybreaksOld phones and other clients send any size, so the server still has to check

How many workers?

100,000 photos/day100,000=1/s,1×3=3/s at peak,3×2 s=6 busy workers\frac{100{,}000\ \text{photos/day}}{100{,}000} = 1/\text{s}, \quad 1 \times 3 = 3/\text{s at peak}, \quad 3 \times 2\ \text{s} = 6\ \text{busy workers}
Assumptions: 100,000 photos a day, 2 seconds each, peak 3x average. Six workers are busy at peak, so run eight or nine.

Decision 1

SQL or key-value

ChooseWhenCostTrigger to move
Relational (SQL)Rules across rows, several ways to query, data fits one large machine with a standbySplitting it later is workStorage or write rate beyond one machine after replicas, caching and batching
Key-valueOne known key answers almost every request, and size or write rate needs splittingFew joins; each new question may need a new index or copyA new feature needs rules across many records
Seat booking: SQL, for the unique rule on seat and slot. URL shortener: key-value, for 183 TB looked up by one code.

Decision 2

Sync or async

Ask one question: does the user need the result to continue? Yes means do it now. No means accept it, store it safely, and finish it later.

ChooseWhenCostExample
Sync (now)The answer depends on it, and it is quickThe user waits; its failure is your failureClaiming a seat
Async (later)It is slow, bursty or can be retriedStatus to show, duplicate work to handleConfirmation email, photo sizes
Queues usually deliver at least once, so make the work safe to repeat.

Decision 3

Push or pull

Who starts the message?

The same choice appears in feeds. Fan-out on write pushes a post to every follower's list. Fan-out on read pulls from the people you follow. Many readers per post favours push; an author with millions of followers favours pull for that author.

Decision 4

Cache or not

  1. 1

    Is the read too much for the source?

    Give the number. If the database is fine, do not add a second copy to keep right.

  2. 2

    Do reads repeat?

    A cache helps when the same thing is asked for again soon.

  3. 3

    May it be old?

    Agree how old. If the answer is never, do not cache the decision, only the page around it.

  4. 4

    What happens on a miss?

    Many requests at once must not all reach the database.

Decision 5

Strong or eventual consistency

Strong means every read after a write sees it. Eventual means copies catch up soon. Choose per operation, not once for the whole system: ask what a person loses if they see old data.

OperationOld data causesChoose
Book a seatTwo readers, one seatStrong: decided by the database
Show the seat mapA polite refusalEventual: up to 2 s old
Match a driver to a tripOne driver, two ridersStrong: conditional update
Show drivers on the mapA car drawn slightly behindEventual: next update in 3 s
Count saves on a recipeA number off by a fewEventual

Push-back

When they say "are you sure?"

Answering a challenge

Seat booking: holding your ground

  1. Interviewer

    Why not Redis for the bookings? It is faster.

  2. You

    You are worried about speed at 9:00. We expect about 300 booking attempts a second, which is well within one relational database.

  3. Interviewer

    But Redis could do it.

  4. You

    It could. What I need most is that a confirmed booking is never lost or doubled. The database gives me a unique rule and durable commits that I would otherwise build around Redis myself. If attempts grew past what the database can take, I would put the waiting room in front first, then split by building.

Bus tracker: changing your mind well

  1. Interviewer

    Parents now want to see the bus move smoothly, within a second.

  2. You

    That is the trigger I gave. Polling would go from 2,000 to 20,000 requests a second, and most answers would be empty. I would change to server-sent events.

  3. Interviewer

    What does that cost us?

  4. You

    20,000 open connections, a way to route each bus to its watchers, and reconnects after a restart. The buses would also have to report every second, not every ten, so that write path grows ten times too. I keep polling as a fallback.

Summary

The whole topic in one minute.

State the choice, the reason, the cost and the trigger.

Push-back is information. Ask what they see, weigh it, then keep or change the choice for a reason.

  • Choice, reason, cost, trigger: every box gets all four.
  • Compare on the two needs that matter here, then take the simpler option that passes.
  • Breaks, works, strong are grades for this problem, not for the technology.
  • Choose consistency for each operation by asking what old data would cost.
  • Push-back with a new fact can change the choice. Without one, explain the choice better.

Sources