Recommended resources
The freely available resources this lesson will direct you to.
Hello! Welcome back to our course on designing security products.
In our last lesson, we designed a robust telemetry pipeline to capture raw streams of user behavior—specifically, mouse movements and keystrokes. We now have a firehose of high-fidelity data. But raw data isn't enough; we need to transform it into meaningful insights to distinguish a human from a bot.
This brings us to today's learning outcome: Design a feature engineering pipeline and compare heuristic vs. ML-based approaches for behavioral bot detection.
We will architect the "brains" of our bot detection system. First, we'll design a pipeline to distill the raw telemetry into quantifiable features. Then, we'll compare two core philosophies for making detection decisions based on these features: using hand-crafted, explicit rules (heuristics) versus letting a machine learning model discover the patterns automatically.
1. The Feature Engineering Pipeline: From Raw Data to Insight
The telemetry data we collect is a time series of coordinates and key presses. To make sense of it, we need to extract higher-level attributes, or features. A feature is a measurable property or characteristic of the phenomenon we're observing. For example, instead of a thousand (x, y, t) coordinates, we can compute a single number: the average mouse speed.
Our first task is to design a pipeline that ingests the raw event stream from our Kafka topic and computes these features.
Categories of Behavioral Features
A good feature engineering process starts with hypotheses about what makes bot behavior different from human behavior. Bots are often too perfect, too fast, too random, or too repetitive. Our features should be designed to capture these differences.
Bot Detection - ML System Design in a Hurry: Features
Let's explore a structured breakdown of potential features. The following resource provides an excellent, detailed categorization of features that are commonly used in production bot detection systems. As you read, think about how you would compute each of these from the raw data streams we designed in the last lesson.
Please read the section titled 'Features'. Focus on the following categories, as they relate directly to our behavioral telemetry: Activity Patterns: Pay attention to features derived from timing, like posting cadence, session patterns, and circadian rhythms. These quantify the 'when' and 'how often' of user actions. Content Signals: Think about how you could derive features like typo rates or semantic diversity from the keystroke data we're collecting. Real-time Behavioral Signals: This is an interesting category where the system's own actions (like flagging an account) become features themselves.
As you've just read, we can group our features into several logical categories. For our behavioral analysis system, the most relevant are:
- Movement Kinematics:
- Average/Maximum mouse speed and acceleration.
- Jerkiness or smoothness of the cursor path (e.g., calculated as the rate of change of acceleration).
- Curvature of the path (humans rarely move in perfectly straight lines).
- Activity and Timing:
- Number of clicks, mouse movements, and key presses per second (action frequency).
- Time between key presses (inter-keystroke delay). Bots might have unnaturally consistent timings.
- Ratio of mouse movement time to idle time.
- Path Geometry:
- Total distance traveled by the cursor.
- Ratio of the cursor path length to the straight-line distance between start and end points (movement efficiency).
- Angle distribution of mouse movements.
Advanced Feature Engineering: An Example
Feature engineering can be highly creative. A powerful technique is to transform the problem into a different domain where well-established models already exist. For example, we can convert mouse trajectories into images and then use powerful image classification models (Convolutional Neural Networks).

Feature Pipeline Architecture
From a system design perspective, the feature engineering pipeline is a stream processing application. It would look something like this:
- Consumer: A service (e.g., built with Apache Flink or Spark Streaming) consumes the enriched telemetry batches from the main Kafka topic.
- Stateful Processing: The service maintains a state for each active user session. It aggregates data over time windows (e.g., 10-second tumbling windows) to compute features like
clicks_per_10soravg_mouse_speed_last_minute. - Feature Vector Generation: At the end of each window, or upon session termination, the service assembles a feature vector—an array of numbers representing the state of that session's behavior.
- Producer/Storage: This feature vector is then published to a new Kafka topic for real-time consumption by the detection engine, and/or stored in a Feature Store for model training and batch inference.
This architecture is scalable and decouples the complex task of feature generation from the actual detection logic.
2. Detection Strategies: Heuristics vs. Machine Learning
With a pipeline producing rich feature vectors, how do we make a "bot" or "not bot" decision? We'll now compare two fundamental approaches.
Approach 1: Heuristic-Based Detection
A heuristic is a simple, experience-based rule designed for efficient problem-solving. In our context, a heuristic-based detection engine is a set of hand-crafted if-then rules that operate on our features.
For example:
IF mouse_movement_count == 0 AND clicks_per_second > 20 THEN score = 0.9 (likely bot)IF profile_is_incomplete AND avg_inter_keystroke_delay < 10ms THEN score = 0.8 (likely bot)IF has_circadian_rhythm == false AND session_duration > 18_hours THEN score = 0.95 (likely bot)
Cloudflare Bot Management: Heuristics engine
Heuristic engines are widely used in production for their speed and reliability. Let's look at how a major security provider, Cloudflare, describes their own heuristics engine.
Please read the short section 'Heuristics engine'. Note its key properties: speed, deployability, low false positives, and its role in providing labels for machine learning.
As the Cloudflare blog highlights, the main advantages of a heuristic engine are:
- Speed and Efficiency: Evaluating simple rules is extremely fast and computationally cheap, making it suitable for high-throughput, low-latency environments.
- Interpretability: If a user is flagged, you know exactly which rule was triggered. This is invaluable for debugging and explaining decisions.
- Low False Positives: Because rules are created by human experts, they can be made very conservative to ensure that legitimate users are not impacted.
However, the primary disadvantage is that heuristics are brittle. An attacker can analyze the rules (or make educated guesses about them) and subtly change their bot's behavior to fly under the radar. This leads to a constant cat-and-mouse game where security engineers must continuously update the rules.
Approach 2: Machine Learning-Based Detection
Instead of manually defining the rules, a machine learning (ML) approach learns the patterns of bot-like behavior from a large dataset of examples.
Bot Detection | ML System Design Problem Breakdown
This video provides a fantastic walkthrough of designing an ML-based bot detection system. It clearly contrasts a simple benchmark model (which is very similar to a heuristic approach) with a sophisticated, state-of-the-art architecture.
Please watch the segment from 00:29:29 to 00:40:55. Focus on the following points: The Benchmark (00:29:42): Notice how the starting point is a simple logistic regression using hand-engineered features. This is a crucial first step and represents a simple, data-driven version of a heuristic system. The Advanced Model (00:33:00): Understand the concept of the 'late fusion' architecture. It combines different types of models (a GNN for network graphs, a GRU for activity sequences, an MLP for simple counters) to get a more holistic view. This represents the power of ML to synthesize diverse signals.
The video illustrates the spectrum of ML-based detection:
-
Classical ML on Engineered Features: Models like Logistic Regression or Gradient Boosted Trees (like CatBoost, mentioned in the Cloudflare blog) are trained on the feature vectors we designed earlier. They learn the optimal weights and combinations of these features to produce a bot score. This is a big step up from manual rules, as the model can uncover relationships that are not obvious to a human.
-
Deep Learning on Raw(er) Data: More advanced models like Recurrent Neural Networks (GRUs, LSTMs) or Transformers can directly process sequences of events (like our telemetry stream). In this paradigm, the model performs implicit feature engineering, learning the relevant patterns from the sequence data itself, reducing the need for extensive manual feature creation.
The main advantages of the ML approach are:
- Adaptability: It can identify complex, non-linear patterns that would be nearly impossible to write as a heuristic rule.
- Resilience: It is often more robust to minor changes in bot behavior.
- Scalability: The detection logic can be improved by simply retraining the model on new data, rather than requiring engineers to write new rules.
The disadvantages include:
- Complexity: Building, training, and maintaining ML systems (MLOps) is a significant engineering effort.
- Opacity: The decisions of complex models can be difficult to interpret (the "black box" problem).
- Data Dependency: It requires a large, high-quality labeled dataset for training, which can be expensive to create.
Here is what an ML inference service might look like in an edge architecture:

3. Synthesis: The Hybrid System
You might be asking: which approach is better? The answer is neither. In the real world, the most effective bot detection systems are hybrid. They combine heuristics and machine learning to get the best of both worlds.
- Heuristics as a Fast Path: Simple, high-confidence rules run first to catch unsophisticated bots with minimal computational cost. If a request is flagged here, there's no need to run the expensive ML model.
- ML for the Nuanced Cases: Requests that pass the heuristic filter are then scored by one or more ML models to catch more advanced bots that exhibit subtle behavioral anomalies.
- A Symbiotic Relationship: As noted in the Cloudflare article, heuristics are a fantastic source of high-quality labels for training ML models. When you are very certain a rule catches a bot, you can use that to bootstrap your training data.
Finding "Unknown Unknowns" with Anomaly Detection
One final piece of the puzzle is how to catch entirely new bot attacks that neither your rules nor your trained models have ever seen. This is where unsupervised learning, or anomaly detection, comes in.
Techniques like Isolation Forests or Autoencoders are trained on all user behavior (which is mostly normal). They learn what "normal" looks like, and their job is to flag users who deviate significantly from this baseline. These flagged anomalies can then be investigated by analysts, who in turn can create new heuristics and labels to feed back into the supervised system. This creates a powerful feedback loop for continuous improvement.
Conclusion
In this lesson, we bridged the gap between raw data and actionable decisions in our bot management system.
Key Takeaways:
- Feature Engineering is Crucial: It's the process of transforming raw behavioral data into a structured format (feature vectors) that exposes the differences between bots and humans.
- Heuristics are Fast and Interpretable: Simple
if-thenrules provide a highly efficient first line of defense against known bot patterns and are excellent for their low false-positive rates. - ML is Powerful and Adaptive: Machine learning models can uncover complex patterns and are more resilient to evolving threats, but they require a significant investment in data and infrastructure (MLOps).
- Hybrid is Best: The most robust systems layer heuristics for speed and certainty with ML for nuance and adaptability. They create a feedback loop where each component strengthens the others.
Preview of the Next Lesson:
We now have a system that produces a bot score for each user. But what do we do with that score? Simply blocking every user with a non-zero score would be too aggressive. In our next lesson, we will address the outcome: "Compare challenge mechanisms (e.g., CAPTCHA, proof-of-work) and design risk-based escalation logic to apply them." We'll explore how to translate a risk score into an appropriate action, from silently logging to issuing an interactive challenge.