Design a Rule Engine
🚦 What is a rule engine? It is a component that checks data against a list of rules written as data, not as code. Every card payment passes through it before it is approved: "block payments over 1,000 from abroad", "review casino merchants", "review a card used more than three times in a minute". Analysts add and remove rules all day, and the engine applies the new rules to the very next payment, with no deploy.
You will hear this problem as "design a rule engine", "design a fraud detection rules system" or "design a thread-safe transaction rule engine with composable conditions". They are one design. Fraud rules are the example we build on, composable conditions are the heart of the class design, and thread safety gets its own section in the Implementation, because a real engine evaluates thousands of payments at once while analysts edit rules.
Requirements
"Design a rule engine that evaluates card transactions against fraud rules. Rules can be combined, and the fraud team changes them at runtime."
The prompt leaves out what a rule looks like, what happens when several rules fire, and whether rules can remember anything. Those answers decide the whole design.
Clarifying Questions
You: What does a single check look like? Are rules arbitrary code?
Interviewer: No code. The basic pieces compare one field of the transaction with a value: amount over 1,000, country not US, merchant in a list. Analysts combine pieces with AND, OR and NOT, and can reuse a piece in many rules.
You: What can a rule do when it fires?
Interviewer: Two actions: send the payment to manual review, or block it. If no rule fires, the payment is allowed.
You: If several rules fire with different actions, which one wins?
Interviewer: The most severe: block beats review, review beats allow. But return every rule that fired; analysts need all the reasons, not just the first.
You: Do any rules need memory, like "too many payments in a short time"?
Interviewer: Yes, velocity rules: more than N payments from the same account in the last W seconds. Count every payment we evaluated, even ones we blocked; fraudsters retrying a stolen card is exactly the pattern we want to see.
You: How do we know the time?
Interviewer: Each transaction carries its own timestamp in seconds. Timestamps never go backwards.
You: Will rules change while transactions are being evaluated?
Interviewer: Constantly. Many threads evaluate payments, and analysts add or remove rules at the same time. A payment must never see half a change, and evaluation must stay fast.
The severity question is the one to slow down on. "First matching rule wins" and "most severe rule wins" give different answers as soon as two rules fire, and the tests will tell them apart.
💡 Interview tip: When a prompt says "rules", ask early whether any rule needs state. Stateless rules are pure functions of one input and easy to make thread safe. One stateful rule (a counter, a history) changes the concurrency story completely, and it is better to know that before you draw classes.
Final Requirements
- A transaction has an account, an amount, a country, a merchant and a timestamp.
- A leaf condition compares one field with a value: numbers with
>,>=,<,<=,==,!=; text with==,!=andina list. - A velocity condition holds when the account made more than N transactions in the last W seconds, counting the current one. Every evaluated transaction counts, whatever its outcome.
- Conditions combine with AND (all of), OR (any of) and NOT, to any depth, and one condition can be used by many rules.
- A rule links a condition to an action, REVIEW or BLOCK. Rules can be added and removed at runtime.
- Evaluating a transaction checks every rule. The decision is the most severe action among the rules that fired (BLOCK, then REVIEW), or ALLOW if none fired. The result also lists every rule that fired, in the order rules were added.
- Many threads evaluate at once while rules change. Each evaluation sees either the old rule set or the new one, never a mix.
Out of scope
- Parsing a rule language like
amount > 1000 AND country != "US"(see /low-level-design/expression-evaluator). - Machine-learning risk scores.
- Storing rules and history in a database or across many servers (we touch on it in Extensibility).