AlgoViz

How to think about any design

easy 30 min

You do not need to have seen the problem before.

Video lesson · Chapter 1 of 5

A map with a thousand pins

0:00 / 2:56

Five questions for any design

1:30Reel coming soon

Theory

Read or listen first. About 1 min.

What it is

This topic is about the thinking that happens before and during a design: what to ask, what to build first, and when to add something.

A system design is a plan for how parts such as services, databases and caches work together to do a job for people. The job comes first. The parts are answers to questions the job raises.

Why it exists

Interview prompts are open on purpose, and real projects are too. Without a method, people either freeze or draw everything they know.

A small set of questions gives you a place to begin on a problem you have never seen, and a reason for every part you add.

A short history

  1. 1945

    George Pólya publishes How to Solve It: understand the problem, make a plan, carry it out, look back.

  2. 1975

    John Gall writes that working complex systems grow from working simple ones, now called Gall's law.

  3. Late 1990s

    Extreme programming spreads the advice to build only what is needed now.

  4. 2016

    Google publishes its Site Reliability Engineering book, followed by a workbook that teaches design with numbers.

Words to know

Requirement
Something the system must do or a quality it must have.
Target
A requirement with a number.
Hard part
What makes this system different from a simple store and show.
Trade-off
What you give up to get something else.
Source of truth
The store whose answer wins.

How it works

Step through the idea, or press Play.

Step 1 of 3

Ask. Five questions tell you what matters and what is hard

Understand it

Start here

The lemonade stand

Picture a child selling lemonade. Nobody begins by building a factory. They pour a few cups, see what goes wrong, and fix that one thing. Good system design feels the same.

A stand that grows only when it must
LEMONADEIt works

The five questions

Ask these before you draw

  1. 1

    Who does what?

    Name the actors and their actions. They become your features and endpoints.

  2. 2

    What must never go wrong?

    Money lost, seat sold twice, message leaked. This decides where you need strict correctness.

  3. 3

    What is the hardest thing here?

    Every design has one part that makes it different. Find it, name it, and save time for it.

  4. 4

    What breaks first as it grows?

    Reads, writes, storage, connections, one hot item, distance. Use a number.

  5. 5

    What am I giving up?

    Freshness, cost, simplicity, some availability. If you cannot name it, you have not decided yet.

From question to design

Use them

Three prompts you have not practised

QuestionLibrary seat bookingSchool bus trackerRecipe sharing
Who does what?Readers book, librarians block seatsParents watch, buses reportCooks post, others save and search
Must never go wrongOne seat, two readersA stranger sees a child's stopA saved recipe disappears
HardestThe 9:00 rush on a few seatsPrivate, live positionsSearch by what is in the kitchen
Breaks firstMany writers on the same seatsOpen connections at 7:45Photo storage and search reads
Giving upSeat map 2 s oldPosition 10 s oldNew recipe searchable in a minute
Same questions, three different hard parts: contention, privacy and search.

Grow it

Smallest version first

The improvement loop

Grown

  • Correct from the first version
  • Each box has a reason
  • Easy to remove a box and say what fails
  • Time left for the hard part

Assembled

  • Ten boxes in three minutes
  • Nothing works end to end yet
  • "Why Kafka?" has no answer
  • The hard part gets two minutes

The skeleton

The bones most systems share

Client, CDN, load balancer, API gateway, services, cache, database. It is a checklist: when something fails a target, look here for where the fix could live. Draw only the bones this system needs.

A request through the bones

Listen

Words that point at a part

Some words in a prompt hint at a well-known part. Treat the hint as a candidate. Check it with a number before you draw it.

Eleven hints

You hearThe limitCandidateNot needed when
Real-time, live, chatWaiting between pollsWebSockets or server-sent eventsUpdates are rare: poll
Search, typeaheadScanning textInverted index (Elasticsearch)Small data: database text search
Analytics, trendingVolume and countingKafka and stream processing (Flink)Yesterday's numbers are fine: nightly job
Background job, emailUser waitingMessage queue (RabbitMQ, SQS)The work takes milliseconds
Global, static, mediaDistanceCDN (CloudFront, Akamai)Users sit in one region and data is private
Video, image uploadStorage cost and I/OBlob storage (S3), direct uploadTiny icons
Payment, bookingCorrectnessTransactions and idempotency keysNothing is scarce or paid for
Sensor data, metricsWrite rateTime-series database (InfluxDB)A few writes a second
Nearby, locationTwo-dimensional searchGeohash, quadtree, Redis geoAbout a thousand places: scan
News feed, timelineRead loadCache and fan-outFew followers: query on read
Spikes, viralOverloadLoad balancer, autoscalingHeadroom already covers the spike

Where you have met them

  • Open connection

    Slack, Discord, trading screens, live scores.

  • Inverted index

    Google Search, Amazon product search, GitHub code search.

  • Stream processing

    Trending topics, recommendations, ad analytics.

  • Queue

    Email, image processing, report generation.

  • CDN and blob storage

    Netflix video, Instagram photos, Dropbox files, static sites.

  • Transactions

    Card payments, ride booking, hotel booking.

  • Time series

    Server metrics, smart homes, stock tickers.

  • Spatial index

    Driver matching, nearby restaurants, map games.

  • Fan-out

    Social timelines and activity updates.

  • Autoscaling

    Holiday sales, product launches, breaking news.

Numbers

Let a number decide

Per day to per second

per second≈per day100,000,peak≈3×average\text{per second} \approx \frac{\text{per day}}{100{,}000}, \qquad \text{peak} \approx 3 \times \text{average}
A day has 86,400 seconds. Dividing by 100,000 is easier in your head and 14% low, which is close enough.

Live positions

1,000,000 drivers3 s≈333,000 writes/s\frac{1{,}000{,}000\ \text{drivers}}{3\ \text{s}} \approx 333{,}000\ \text{writes/s}
This number decides something: it is too many small writes for the database that holds trips and money.

Worked example

Ride hailing in four steps

"Riders can book a ride and see drivers in real time." The four steps are: lay the foundation, find the hard part, define the data flow, plug in the special parts.

How it opens

  1. Interviewer

    Design a ride-sharing service like Uber.

  2. You

    I would like to clarify the scope. Riders request rides, see nearby drivers in real time and get matched. Should I include payments and trip history?

  3. Interviewer

    Yes, include payments. Focus on matching and the real-time side.

  4. You

    I hear two hard parts: real-time positions and matching by location. They suggest an open connection and a spatial index. Let me check both against the numbers, then build up from a simple booking flow.

What makes each app hard

AppThe hard partThe special parts
Ride hailingLive positionsWebSockets and a spatial index
Social feedReading millions of feedsCache and fan-out
Video siteStreaming huge filesCDN and blob storage

Ride hailing, complete

Mobile apps outside our system reach an API gateway for booking and a WebSocket gateway for positions. The matching service reads Redis geo, books trips in PostgreSQL and calls the payment service. The location service updates Redis geo and sends points through Kafka to Cassandra. The card network is outside our system.
Blue is booking, orange is positions. Each system has its own box; users and the card network are outside the dashed outline.

How a position travels

  1. 1

    Send

    The driver's phone sends GPS every 3 seconds over a WebSocket.

  2. 2

    Update

    The location service updates Redis at once, so "nearby" is fresh.

  3. 3

    Publish

    The same point goes to Kafka without waiting.

  4. 4

    Archive

    An archive job writes trip history to Cassandra.

Defending it

  1. Interviewer

    Why Redis for positions, and not the main database?

  2. You

    At a million drivers it is about 333,000 small writes a second, and losing one position costs nothing because the next arrives in 3 seconds. Redis geo commands (GEOADD, GEOSEARCH) keep a geohash index in memory and answer in about a millisecond.

  3. Interviewer

    Two riders request the same driver at once. What happens?

  4. You

    The claim is a conditional update in the trip database: set the driver to matched only if still free. One wins and the other is offered someone else. An idempotency key on the booking makes a network retry safe.

  5. Interviewer

    And if a driver goes offline in the middle of a ride?

  6. You

    We watch heartbeats. After 30 seconds without a position we mark the driver as possibly offline and tell the rider. After a 2 minute grace period we look for another driver. The trip is stored in PostgreSQL, so restarting the live services loses nothing.

  7. Interviewer

    What changes at 100 times the load?

  8. You

    Split positions by city, so each has its own Redis. Partition Kafka the same way. The new problem is trips that cross a boundary; a routing layer sends each request to the right region.

Speak

Tell it as a story

PartTimeSentence to fill inRemember
Opening30 s"I will design {system} for {scale}. The hard parts are {two or three}, and I will address them with {ideas}."Own words; two or three challenges; show you know the scale
Main flow2 min"Let me follow one request: {first}, then {next}..."Client to database; latency at each hop; why each part exists
Deep dive5 min or more"The trickiest part is {challenge}. Here is how I handle it."Hardest part; trade-offs; failure and recovery
Wrap-up1 min"We handle {challenge} with {solution} and reach {target}."Key decisions; what you would do with more time; invite questions

The same four, for ride hailing

  1. 1

    Opening

    "I will design this for 10 million rides a day in 50 cities. The hard parts are live positions, avoiding double booking at peak and picking a good driver. I will use WebSockets, transactions and a spatial index."

  2. 2

    Main flow

    "When a rider opens the app we read nearby drivers from Redis, in under 50 ms. Positions arrive every 3 seconds over WebSockets and go on through Kafka to the archive."

  3. 3

    Deep dive

    "The trickiest part is double booking. A conditional update lets one request win, and an idempotency key covers retries."

  4. 4

    Wrap-up

    "Positions live in Redis with lookups under 100 ms, trips in PostgreSQL, history in Cassandra. With more time: surge pricing, arrival estimates and region failover."

When you freeze

Getting unstuck

Four ways back

Summary

The whole topic in one minute.

You can design something new by asking five questions, building the smallest version, and fixing one break at a time.

Words in a prompt suggest parts; numbers decide whether you need them. Say what you chose, why, and what it costs.

  • Five questions come before the first box.
  • The smallest correct design first; then one break, one fix.
  • A word suggests a part; a number decides.
  • One source of truth, everything else is a copy.
  • Choose, give the reason, name the cost, say what would change your mind.

Sources