ML System Design · Module 1 of 47 · 11 concepts · about 16 min

What ML System Design Is

What it means to design a machine learning system rather than train a model, the parts every such system has, how it differs from ordinary software, and the step-by-step method this course uses for every design.

About ML System Design: A machine learning model on its own is a function that turns inputs into a guess. Making that guess useful to millions of people, every day, for years, is a much bigger job: the data has to be collected and checked, the right answers gathered, the inputs computed the same way in testing and in real use, the guesses delivered in milliseconds, and the whole thing watched as the world changes around it. That job is ML system design. This course starts from the very beginning, with a short refresher on machine learning itself, then builds each part in turn: framing a business problem, data and labels, features, choosing and training models, measuring them offline and with real users, serving predictions fast and cheaply, releasing safely, monitoring and debugging, and doing all of it fairly and securely. It then works through fifteen complete designs (recommendations, search, fraud, delivery times, forecasting, ads, moderation, visual search, blurring faces in street imagery, video search, video recommendations, events, similar holiday homes, a social feed and people you may know), compares them side by side, a capstone project you build yourself, and the interview, from the method to beginner, advanced and scenario questions.

At work: The work of an ML engineer once a model has to serve real users: deciding where the data comes from, how predictions reach the product, and how anyone will know when it stops working.

What ML system design is

What it is

ML system design is planning everything around a machine learning model so that it helps a real product, reliably, for years:

  • where the data comes from and how it is cleaned;
  • how the labels (the right answers it learns from) are collected;
  • which features (the facts it looks at) are computed, and when;
  • how the model is trained, checked and released;
  • how predictions reach the product fast enough and cheaply enough;
  • how the system is watched and retrained as the world changes.

Why it matters

A model that scores well in a notebook is not yet useful to anyone:

  • it needs fresh inputs every time it is asked something;
  • its answers must arrive in milliseconds, or overnight, depending on the product;
  • it slowly gets worse as people's behaviour changes, and nothing will crash to tell you.

Design is how you plan for all of that before it goes wrong.

In the real world: A team whose fraud model scored brilliantly offline launches it and finds it needs a customer's last hour of activity, which no system computes in time. The model is fine; the system around it was never designed.

Treating it as 'pick the best algorithm'. In an interview and at work, the model is usually the shortest part of the conversation; data, serving and monitoring are where designs succeed or fail.

Practise it: Design a Predictive Maintenance System, Design a Real-Time Anomaly Detection System, Design a Recommendation System, Design an Image Search System, Design a Real-Time Fraud Detection System, Design a Search Ranking System, Design an Ad Ranking System

A model versus a system

What it is

A model is one learned function: inputs go in, a prediction comes out. A system is everything that makes that function useful:

  • the pipelines that collect and clean data;
  • the code that turns raw data into features, the same way in training and in production;
  • the service that answers prediction requests;
  • the release process, the dashboards, the alerts and the retraining jobs.

In a typical production system the model's own code is a small fraction of the total.

Why it matters

Most failures in production are system failures, not model failures.

  • A feature computed differently in production than in training.
  • A data feed that silently stops updating.
  • A release with no way to roll back.
  • Better modelling fixes none of these.

In the real world: A well-known Google paper, 'Hidden Technical Debt in Machine Learning Systems', drew the model as a small box in the middle of a much larger diagram of data, serving and monitoring. Most teams find it describes their system exactly.

Visual example

Model vs system

The model is one row; the system is all of them.

What each one is responsible for
partquestion it answersif it is missing
Data pipelinesWhere do the inputs come from, and are they correct?Garbage in, confident garbage out
LabelsWhat counts as the right answer?The model learns the wrong goal
FeaturesWhat does the model look at, computed how?Training and production disagree
ModelGiven the features, what is the prediction?Nothing to serve
ServingHow does a prediction reach the product, and how fast?A good model nobody can use
MonitoringIs it still working today?Silent decline for months
RetrainingHow does it keep up with a changing world?A model frozen in the past

Spending the whole design on the model architecture. An interviewer who hears ten minutes about layers and none about where the inputs come from at prediction time marks it down.

How ML systems differ from ordinary software

What it is

Ordinary software does what its code says. An ML system does what its data taught it. That changes four things:

  • Behaviour comes from data: change the data and you change the product, without touching code.
  • It fails quietly: a wrong prediction looks exactly like a right one; nothing throws an error.
  • It decays: the world changes, the data it learned from goes out of date, and accuracy slips week by week.
  • It is tested with statistics: there is no single correct output to assert, only rates of being right across many cases.

Why it matters

Each difference demands a part ordinary software does not need.

  • Data comes from outside your control, so it needs checks of its own.
  • Quiet failure needs monitoring of the predictions themselves, not just of the servers.
  • Decay needs retraining.
  • Statistical testing needs evaluation sets and experiments.

In the real world: During the 2020 lockdowns, shopping patterns changed overnight. Demand forecasts at many retailers kept running without a single error while being badly wrong for weeks.

Monitoring an ML service like any other API: uptime, errors, latency. All three can be perfect while the model recommends nonsense, because a nonsense answer is still a fast, successful response.

The parts of an ML system

What it is

Every ML system, whatever it predicts, has the same parts, joined in a loop:

  • Data collection: events and records from the product.
  • Labelling: attaching the right answer to past examples.
  • Feature pipelines: turning raw data into the model's inputs.
  • Training: fitting the model, and checking it against held-back data.
  • Model registry: storing each trained version with its scores.
  • Serving: answering prediction requests.
  • Monitoring: watching inputs, outputs and results.

Monitoring and new data feed back into training, which is what makes it a loop.

Why it matters

Naming the parts gives every design the same skeleton.

  • Each later module of this course is one of these parts in depth.
  • In an interview, walking the loop once at a high level, before going deep anywhere, shows you see the whole system.

In the real world: A ride-hailing company's ML platform is organised around exactly these boxes: one team owns feature pipelines, another the training and registry, another serving and monitoring.

Visual example

Loop

The last box feeds the first: production data becomes the next training data.

The parts of an ML system, in order

  1. Learning

    CollectWhat users see and do
  2. LabelThe right answers
  3. FeaturesThe same in training and serving
  4. Train and checkOn data it has not seen
  5. ReleaseStored, then rolled out
  6. In production

    ServeWithin the time limit
  7. MonitorInputs, predictions, results

Then back to Collect: the predictions and what followed are the next round's data.

Drawing a straight line from data to prediction with no way back. Without the return path, the system cannot learn from its own mistakes or keep up with change.

The ML lifecycle

What it is

A system is never finished. It goes round a cycle, again and again:

  • Scope: what decision does the model help, and how will success be measured?
  • Data: collect, label and explore.
  • Model: build a baseline, then improve it.
  • Deploy: release safely, alongside the old version first.
  • Operate: monitor, debug and retrain.

Lessons from operating send you back to any earlier step, including scoping.

Why it matters

Expect to go round many times.

  • The first version is usually simple and gets a team most of the value.
  • Each later round is driven by what monitoring and error analysis reveal, not by guesses.
  • Planning for iteration from the start (versioned data, repeatable training) makes each round cheap.

In the real world: Netflix's and Spotify's recommendation systems have been rebuilt many times over a decade; each rebuild started from what the previous one's monitoring showed.

Planning a single big launch of a perfect model. The teams that do best ship a simple version early and improve it on real traffic.

When not to use machine learning

What it is

ML earns its cost only when all of these hold:

  • there is a pattern to learn, and it is too complex to write down as rules;
  • there is data with that pattern in it, including the right answers;
  • mistakes are tolerable: a wrong prediction is a cost, not a disaster;
  • the problem repeats at a scale where automation pays;
  • the pattern changes over time, so hand-written rules would need constant upkeep.

If a few simple rules get most of the way, ship the rules first.

Why it matters

ML is expensive to build and to keep running.

  • It needs data pipelines, training, serving and monitoring, forever.
  • A rule is cheaper, explainable and easy to change.
  • Rules also make the best baseline: the score an ML model must beat to justify itself.

In the real world: Google's 'Rules of Machine Learning' guide opens with this advice: do not be afraid to launch a product without machine learning, and use simple rules until the data justifies a model.

Reaching for a model before trying a rule. 'Flag orders over ₹20,000 from accounts less than a day old' may catch most fraud on day one, and tells you what the model must improve on.

The running example: Kirana Kart

What it is

One example carries this whole course. Kirana Kart is a made-up grocery delivery app with 5,000,000 monthly users, 600,000 orders a day and 400 small warehouses ("dark stores"). It uses machine learning in five places:

  • Recommendations: 'you might also like' on the home screen and in the basket.
  • Search ranking: ordering results when someone types 'milk'.
  • Delivery time (ETA): the minutes shown before someone places an order.
  • Fraud detection: spotting fake accounts, stolen cards and refund abuse.
  • Demand forecasting: how much of each product each store should stock tomorrow.

Why it matters

The five want different things, which is what makes every design decision a real choice:

  • recommendations and search must answer in tens of milliseconds, for millions of requests;
  • the ETA must be accurate to the minute and is checked by every customer;
  • fraud is rare, its labels arrive weeks late, and fraudsters fight back;
  • forecasts are made once a night, but a wrong one wastes food or loses sales.

In the real world: Real grocery and food-delivery apps run each of these as a separate ML system, often owned by separate teams, sharing one data platform underneath.

Visual example

Kirana Kart

Illustrative figures. Each column is a design constraint later modules return to.

Five ML features and what each demands
featurekind of taskwhen it predictstime limithardest part
RecommendationsRankingEvery home-screen open~100 msMillions of requests; cold start
Search rankingRankingEvery search~100 msUnderstanding the query
Delivery timeRegressionBefore each order~50 msLive traffic and store load
Fraud detectionClassificationAt payment and refund~100 msRare, late labels; adversaries
Demand forecastingForecastingOnce a nightHoursSeasonality; promotions; stock-outs

Thinking one design fits all five. A nightly batch job is right for forecasting and useless for fraud, which must decide before the payment goes through.

The trade-offs every design balances

What it is

Almost every decision in ML system design trades between a few forces:

  • Accuracy: how often the prediction is right.
  • Latency: how long it takes to get one.
  • Freshness: how up to date the data behind it is.
  • Cost: compute for training and serving, and people's time.
  • Simplicity: how easy it is to run, debug and change.

You cannot maximise all of them. A design is a defensible choice of which to favour, for this product.

Why it matters

Naming the trade-off is what interviewers listen for.

  • 'A larger model would be more accurate but would take 200 ms, over our 100 ms budget, so I would use it only to rank the top 50' is a design decision.
  • 'I would use a deep model' is not.

In the real world: A food-delivery firm accepts slightly less accurate delivery estimates from a simpler model because it can retrain it every hour, and freshness beats sophistication when traffic changes by the minute.

Claiming a design is best on every axis. Every real choice gives something up; saying what, and why it is acceptable here, is the point.

The design framework

What it is

Every design in this course follows the same eight steps, in order:

  • 1. Clarify the problem: the business goal, the users, the scale, the constraints.
  • 2. Frame it as ML: what exactly is predicted, from what, and how success is measured.
  • 3. Data and labels: what exists, what must be logged, where the right answers come from.
  • 4. Features: what the model looks at, and how fresh each input must be.
  • 5. Model: a baseline first, then the model that fits the constraints.
  • 6. Evaluation: offline measures, then an online experiment.
  • 7. Serving: how predictions reach the product, within time and cost.
  • 8. Monitoring and iteration: what is watched, and how the model is retrained.

Why it matters

The order matters.

  • You cannot choose features before you know what is predicted.
  • You cannot choose a model before you know the latency budget.
  • You cannot claim success without saying how it is measured.
  • The same eight steps work in a forty-five-minute interview and in a two-week design document.

In the real world: Many companies' internal design-document templates for ML projects have these exact headings; a proposal that cannot fill 'how will we measure success' is not approved.

Visual example

Framework

Each step constrains the next. The model is step five, not step one.

Eight steps for any ML design

  1. ClarifyGoal, users, scale, limits
  2. Frame as MLTarget and success metric
  3. Data and labelsSources and right answers
  4. FeaturesInputs and freshness
  5. ModelBaseline first
  6. EvaluateOffline, then online
  7. ServeBatch or live
  8. MonitorDrift and retraining

Jumping to step 5. 'I'd use a two-tower model' in the first minute, before the goal or the data is clear, is the most common way to fail an ML design interview.

Interview questions: what ML system design is

What it is

The questions interviewers ask about what ML system design is, from the first screening call to the senior round. Cover the answer, say yours aloud, then compare.

Why it matters

Reading an idea and explaining it are different skills.

  • Each answer below is the two or three sentences an interviewer is listening for.
  • The reasoning behind each one is in the cards above. If an answer surprises you, go back to that card.

In the real world: Screening rounds for ML engineering roles often open with 'walk me through the components of a production ML system'. The answers that go well name the loop and the quiet-failure problem in the first minute.

Q: What is ML system design?

Designing everything around a model so that it works in a product: the data and labels, the features, training and release, serving predictions within time and cost limits, and monitoring and retraining as the world changes.

Q: How is an ML system different from ordinary software?

Its behaviour comes from data, not only code; it fails quietly, because a wrong prediction looks like a right one; it decays as the world changes; and it is tested statistically, by rates of being right, not by exact outputs.

Q: What are the main components of an ML system?

Data collection, labelling, feature pipelines, training, a model registry, serving and monitoring, joined in a loop so production data becomes the next training data.

Q: When would you not use ML?

When simple rules work, when there is no labelled data, when mistakes are unacceptable, or when the problem is too small to pay for the upkeep. Rules are also the baseline any model must beat.

Q: How do you approach an ML system design question?

Clarify the goal and constraints, frame it as an ML task with a success metric, then data and labels, features, a baseline and a model, offline and online evaluation, serving, and monitoring. Model choice comes after the constraints are known.

Q: What trade-offs come up most?

Accuracy against latency and cost, freshness against complexity, and model sophistication against how easy the system is to run and debug.

Answering with a list of tools (Kafka, Spark, Kubernetes). Name what each part does first; tools are a detail.

Practise it: Design a Predictive Maintenance System, Design a Real-Time Anomaly Detection System, Design a Recommendation System, Design an Image Search System, Design a Real-Time Fraud Detection System, Design a Search Ranking System, Design an Ad Ranking System

Exercise: map an ML system you use

What it is

A short build that uses this module's ideas. Write it yourself before looking anything up; the checks at the end tell you whether it works.

Why it matters

An idea you have only read fades in a week.

  • Building it once, even in fifty lines, is what makes it yours.
  • Interviewers can tell the difference between someone who has read about a loop and someone who has debugged one.

In the real world: Interviewers sometimes ask 'how do you think the recommendations on this app work?' as a warm-up; this exercise is the practice for it.

Your task. Pick an app you use daily that has an ML feature (a feed, recommendations, a delivery estimate, spam filtering) and sketch its system on one page.

Steps

  1. Name the feature and the decision the model makes, in one sentence.
  2. Guess the prediction target: what exactly is predicted, and as what kind of value (a probability, a number, a ranked list)?
  3. Guess the right answers it learns from: what event would tell the system it was right?
  4. List five inputs it probably uses, and mark each as known in advance (age of account) or computed live (items in basket right now).
  5. Draw the seven parts of the loop for it, from data collection to monitoring.
  6. Write one way it could fail quietly, and what you would watch to notice.

You are done when

  • Every one of the seven parts has at least one concrete line.
  • You have named a label and the delay before it arrives.
  • You have one quiet failure and one signal that would reveal it.

Stretch. Decide whether a set of simple rules could do the job instead, and estimate how much worse they would be.