MLDay 49 of 50ML in Production
Design an ML System
A bank asks you to stop card fraud.
A bank asks you to stop card fraud. A good model is only one part of the answer. You also need to know what "better" means, where labels come from, how fast to answer, how many servers to run and how you'll notice when it breaks.
The idea
A system design is a written plan for the whole service, from the business goal to the alarm that fires when it decays. Walk the same seven steps every time, and answer one question at each:
| Step | Question to answer | Fraud-scoring answer | Taught in |
|---|---|---|---|
| 1. Goal | What does the business want? | Cut fraud losses without blocking good customers | |
| 2. Metric | How will you know it worked? | Fraud caught (recall) while at most 1 in 1,000 good payments is blocked | D13 |
| 3. Data | Where do features and labels come from? | Payment fields, plus each card's recent spend; labels from chargebacks | D45 |
| 4. Baseline | What simple thing must you beat? | The rule "review anything over $500", then logistic regression | D12 |
| 5. Model | What do you train, and how? | Boosted trees, trained by one tracked script | D25, D44 |
| 6. Serving | How does a request get a score? | An API that loads the whole pipeline and checks every request | D46 |
| 7. Monitoring | How will you catch decay? | Input and score drift daily; block rates by group; a model card | D47, D48 |
A chargeback is a customer disputing a charge; the bank then marks it as fraud. It can take weeks. So the newest payments have no labels yet. You split by time, training on older months and testing on the latest (D06). Until labels arrive, drift in the inputs and scores is your early warning.
Here is the design as boxes and arrows:
Every design has trade-offs. Say them out loud:
- False alarms vs fraud caught. At an assumed 10 million payments a day, each extra 1% of good payments blocked angers 99,900 customers.
- Model size vs latency. At that traffic, 3× busier at lunch, a model that takes 400 ms needs 14 servers where a 50 ms model needs 2 (sized below).
- Fresh vs cheap features. A card's spend in the last hour catches a thief's burst of payments, but it needs a live store. A nightly batch is cheaper and a day stale.
A tiny example
A small shop takes 864,000 payments a day. A day has 86,400 seconds, so that's 864,000 ÷ 86,400 = 10 requests a second on average. The lunch rush is 3× busier: 30 a second. Each score takes 0.1 s, so 30 × 0.1 = 3 requests are in progress at once. One server that handles 10 at a time is plenty.
The formula
Traffic is quoted per day, but servers feel the busiest second. The peak factor is how many times busier the busiest moment is than the average.
| Symbol | Meaning |
|---|---|
| requests arriving per second at peak | |
| requests per day | |
| the peak factor: busiest moment ÷ average |
Worked: = 10,000,000 gives 10,000,000 ÷ 86,400 ≈ 115.7 a second on average; with , .
Little's law links arrivals to the work in progress:
| Symbol | Meaning |
|---|---|
| requests in flight (being worked on) at once | |
| arrivals per second | |
| time per request, in seconds (the latency) |
Worked: at 50 ms, . With 10 requests per server, you need servers at peak. Add a spare so one crash doesn't drop payments.
Try it
Size the fraud-scoring service
Traffic isn't spread evenly over the day, and every request holds a server slot for as long as it takes. Move the sliders and watch the server count at peak.
Little's law: requests in flight = arrival rate × time each request takes (L = λ · W). Each server handles 10 at once, so divide and round up. Real sizing adds headroom on top.
It starts sized for the average day (peak at 1×), which fits on one server and falls over at lunchtime. Set peak to 3× and you need 2. Now drag latency to 400 ms, a bigger model: the same traffic needs 14.
In code
The sizing is arithmetic, so write it as a script and keep it next to the design. These are the numbers quoted above. The traffic (10 million payments a day, 3× at peak), the 10 requests per server, the fraud rate (0.1%) and the log size (1 KB a request) are all assumptions to check with the bank.
# script_id: day_049_ml_system_design/sizing
import math
def servers_at_peak(per_day, peak, latency_ms, per_server=10):
avg = per_day / 86_400 # seconds in a day
peak_rps = avg * peak
in_flight = peak_rps * latency_ms / 1000 # Little's law: L = λW
return avg, peak_rps, in_flight, math.ceil(round(in_flight / per_server, 9)) # round away float noise, then round up
for ms in (50, 400):
avg, peak_rps, live, n = servers_at_peak(10_000_000, 3, ms)
print(f"{ms:>3} ms: avg {avg:.1f}/s, peak {peak_rps:.1f}/s, in flight {live:.1f}, servers {n}")
payments, fraud_rate = 10_000_000, 0.001 # assume 1 in 1,000 is fraud
good = payments * (1 - fraud_rate)
print(f"fraud a day: {payments * fraud_rate:,.0f}")
for fpr in (0.01, 0.001):
print(f"good payments blocked at {fpr:.1%} false alarms: {good * fpr:,.0f} a day")
print(f"request log at 1 KB each: {payments * 1_000 / 1e9:.0f} GB a day, {payments * 365e3 / 1e12:.2f} TB a year")
50 ms: avg 115.7/s, peak 347.2/s, in flight 17.4, servers 2
400 ms: avg 115.7/s, peak 347.2/s, in flight 138.9, servers 14
fraud a day: 10,000
good payments blocked at 1.0% false alarms: 99,900 a day
good payments blocked at 0.1% false alarms: 9,990 a day
request log at 1 KB each: 10 GB a day, 3.65 TB a year
With 10,000 frauds a day, a 1% false-alarm rate blocks about 10 good customers for every fraud. At 0.1% it's about 1 for 1, which is why the metric caps false alarms there. The request log grows 10 GB a day, about 3.65 TB a year, so budget its storage too.
Quick check
Quick check
1. A service gets 10 million requests a day. About how many a second is that, on average?
2. Fraud labels arrive weeks later, as chargebacks. What can you watch today?
3. Traffic stays the same, but latency doubles from 50 ms to 100 ms. What happens to requests in flight?
Next
D50 builds steps 5 to 7 for real: the credit model as a tracked, served, monitored and documented service.