Design a Real-Time Fraud Detection System
Understanding the Problem
🕵️ What is a real-time fraud detection system? A real-time fraud detection system looks at every payment as it happens and decides whether to approve it, decline it or send it for review, before the money moves. It combines rules written by fraud analysts ("this card was used in five countries in the last minute") with machine-learning scores that estimate how likely a transaction is to be fraud. Card issuers, payment processors and fintechs all run one, and several vendors sell it as a service to many banks at once.
You'll hear this question in a few forms: "detect a card used in 5 countries within 1 minute," "build a rules engine for fraud," "design a fraud platform used by many banks." They're the same system. The windowed rule is the classic way in, the ML score usually comes as "the data science team exposes a model as an API; integrate it," and the multi-bank version adds tenancy and shared signals. We'll design a platform that does all three.
Functional Requirements
Let's fix the setting with the interviewer. We are a fraud platform that banks (card issuers) call during card authorization. For every transaction, the bank sends us the details and waits for our decision before it approves or declines the card.
Core Requirements
- Banks should be able to send a transaction and get a decision (approve, decline, review) with a risk score and the reasons, in real time.
- Fraud analysts should be able to define rules over streaming data, including windowed rules such as "the same card used in 5 or more countries within 1 minute," and deploy them without a code release.
- The decision should combine rule results with an ML fraud score obtained from a model-serving API.
- The platform should serve many banks, each with its own rules, thresholds and data, and let analysts review flagged transactions and record outcomes.
Below the line (out of scope)
- Training the ML models. We consume a scoring API and supply it features and labels.
- Account takeover, login fraud and anti-money-laundering. Similar ideas, different data.
- Chargeback processing and disputes, beyond using them as labels.
Non-Functional Requirements
Ask for numbers. A reasonable set: 50 bank clients, 10,000 transactions per second on average and 50,000 per second at peak (holiday shopping), across about 500 million cards.
Core Requirements
- The system should return a decision within 100 ms at p99. Our check sits inside the card authorization, which as a whole must finish within a couple of seconds and has many other steps.
- The system should be highly available (99.99%). If we can't answer, the bank must still decide, so we need a defined fallback per bank.
- Windowed features must be fresh: a transaction should count toward its card's windows by the time the next transaction on that card is scored, which can be a fraction of a second later.
- New or changed rules should take effect within a minute, and be testable before they go live.
- Each bank's data and configuration must be isolated from the others, and one bank's traffic spike must not slow the rest.
Below the line (out of scope)
- Exactly-once accounting of money. We decide; we don't move funds.
- Analytics dashboards for long-term fraud trends.
The numbers point at the hard part. 50,000 transactions per second at about 1 KB each is only 50 MB/s of input, which Kafka handles easily. The hard part is state: each decision needs per-card history (countries in the last minute, spend in the last hour, merchants in the last 30 days), and it needs it in single-digit milliseconds. With 500 million cards and a few hundred bytes of aggregates per card, that's on the order of 100-200 GB of hot state, too big for one machine's memory but easy for a sharded in-memory store.
Then the latency budget. If the whole decision has 100 ms, a realistic split is: ~5 ms of network in and out, ~10 ms to fetch features, ~30-50 ms for the ML call, ~5 ms for rules, with the rest as headroom for the tail. There's no room for a database query that scans recent transactions, and no room to call things one after another if they can run in parallel.
The key design question is whether a rule runs before the decision (synchronously, so it can block this transaction) or after it (asynchronously, on a stream, so it can only react to the next one). "5 countries in 1 minute" should block the fifth transaction, not alert someone after it's approved. Say this out loud; it drives the whole architecture.