Recommended resources
The freely available resources this lesson will direct you to.
Hello! Welcome back.
In our last lesson, we architected a high-throughput pipeline to produce a clean, structured stream of identity audit logs. This gives us a reliable source of truth for every critical action in our identity system. Now, we'll build on that foundation to proactively find threats within that data.
Today's learning outcome is to design an anomaly detection system that consumes identity audit logs to identify suspicious behavior like impossible travel. We will move beyond simple log review and architect a system that can automatically hunt for and flag potentially malicious activity in real-time. This is a classic distributed systems problem that intersects with data science, making it a perfect topic to explore from first principles.
The Architectural Blueprint for Anomaly Detection
At its core, an anomaly detection system identifies events that deviate from an established norm. In the context of identity security, these deviations often signal an account compromise. Our system will operate on the stream of audit logs we designed previously, analyzing them in real-time.
There are two primary approaches to this, which are often used together:
- Rule-Based Detection: This involves defining explicit, deterministic rules for known-bad patterns. These are precise but can only catch what you already know to look for.
- Machine Learning-Based Detection: This uses statistical methods, often unsupervised, to learn a model of "normal" behavior and flag outliers. This is powerful for discovering novel or subtle attack patterns.
A robust system combines both. Let's look at the overall architecture.

The architecture we will design mirrors this pattern, using Kafka as our input stream and a dedicated processing service to run our detection logic.
Designing a Fraud Detection System: From Requirements to Architecture
To build a solid foundation, let's explore an article that lays out the architecture of a very similar system: a real-time fraud detection pipeline. The principles and components are directly transferable to detecting identity fraud.
Please read the section titled 'Processing Topology'. Pay close attention to the five logical stages of the Flink pipeline: Validate & Enrich, Feature Computation, Rule Engine, Model Scorer, and Score Fusion. This forms the blueprint for our system.
As the article illustrates, a modern anomaly detection pipeline is a multi-stage stream processing application. We'll focus on the core detection stages: the Rule Engine for explicit patterns and the Model Scorer for ML-based detection.
Part 1: Rule-Based Detection - The "Impossible Travel" Use Case
Let's start by architecting a solution for the specific "impossible travel" scenario. This is a classic example of a stateful, rule-based check.
The core idea is simple: if two consecutive successful logins for the same user occur from locations that are physically impossible to travel between in the elapsed time, it's a major red flag.
Detecting Impossible Travel, with Google SecOps: Part 1
This article from the Google SecOps community provides a fantastic, detailed walkthrough of implementing an impossible travel detection rule. While it uses a specific query language (YARA-L), the underlying logic is universal and serves as a perfect design specification.
Read the article from the beginning up to 'Setting the Alert Threshold and Finalizing the Rule'. Focus on the logical steps: Capturing two distinct login events for the same user. Calculating the time duration between them. Calculating the geographic distance using their coordinates. Calculating the required speed and comparing it to a threshold.
Based on this logic, we can design the rule engine component.
Architectural Design for Impossible Travel
- Input: The engine consumes the enriched
login_successevents from our audit log topic in Kafka. Each event must containuser_id,event_timestamp, and enrichedgeo_location(latitude and longitude from the source IP). - State Management: The system must remember the previous login for every user. A low-latency key-value store is ideal for this. The
user_idserves as the key. For each user, we store a small object containing thetimestampandlocationof their last successful login. Redis is an excellent choice for this due to its speed. - Processing Logic (per event):
- On receiving a login event for
user_A, the engine queries Redis foruser_A's last login data. - If no prior login exists: This is the user's first observed login. The engine stores the current event's
timestampandlocationin Redis under the keyuser_Aand takes no further action. - If a prior login exists:
- Calculate the time difference: \Delta t = \text{current_timestamp} - \text{last_timestamp}.
- Calculate the geographic distance () between the current and last locations using the Haversine formula. The
math.geo_distancefunction in the article is an implementation of this. - Calculate the required speed: .
- Apply the rule: If exceeds a plausible speed (e.g., > 1000 km/h, which is faster than commercial air travel), generate an
ImpossibleTravelalert. - Finally, update Redis with the current event's
timestampandlocation.
- On receiving a login event for
This stateful design, where we process each event in the context of past events for the same user, is fundamental to behavioral analysis.
How to Detect Fraud in Real Time Using Kafka
For a practical look at implementing this kind of logic, let's watch a short segment that demonstrates a real-time fraud detection service using Kafka and Redis.
Watch the section from 18:59 to 20:37. This part of the video explains the logic for detecting an 'unusual location change'. Notice how it fetches the last known location from a key-value store, compares it to the current transaction's location, and then updates the store. This is the exact pattern we'd use for impossible travel.
Part 2: Machine Learning-Based Detection - Finding the "Unknown Unknowns"
Rules are great for known patterns, but adversaries are creative. What about a user logging in at 3 AM for the first time, or from a new device, or with an unusual frequency? These aren't explicitly "impossible," but they are suspicious. This is where machine learning comes in.
We will design an unsupervised anomaly detection system. Instead of being taught what is "bad," the model learns what is "normal" for each user and flags significant deviations.
Bot Detection | ML System Design Problem Breakdown
This video provides an excellent and deep system design breakdown for a bot detection system. The principles of framing the problem, feature engineering, modeling, and evaluation are directly applicable to detecting suspicious user behavior in our identity system.
First, watch the 'Problem Framing' and 'High-Level Design' sections (04:07 - 21:03). Pay close attention to how the speaker defines the business objective (minimizing impact subject to false positive constraints) and lays out the core components, including classifiers, calibration, and loops for finding new types of anomalies.
Architecting the ML Detection Component
Following the structure from the video, our ML detection system will have several key stages.
1. Feature Engineering
Our stream processor (e.g., Flink) will consume the audit logs and compute a rich set of behavioral features for each user in real-time. This involves stateful operations over time windows.
Bot Detection | ML System Design Problem Breakdown
Now, let's dive into the 'Data' section of the same video to see what kinds of features we can create.
Watch from 21:03 to 29:35. The video discusses 'activity patterns', 'content level signals', and 'network' features. Let's translate these to our identity use case:
Examples of features we could engineer from identity audit logs include:
- Activity Patterns:
- Login frequency (count of logins in last 1h, 24h, 7d).
- Time-of-day patterns (is the login happening outside the user's typical 9-5 window?).
- Session duration statistics (mean, std dev).
- Network & Environment Features:
- IP address reputation and novelty (is this a new or risky IP for this user?).
- Number of distinct countries/cities logged in from in the last 30 days.
- User-agent/device fingerprint novelty.
- Real-time Behavioral Features:
- Number of failed logins preceding a success.
- Time since last password reset or MFA change.
2. Model Selection and Architecture
Since we lack a large, labeled dataset of "compromised" vs. "safe" events, unsupervised models are the best fit. They learn a baseline of normal behavior and score events based on their deviation from that baseline.
Bot Detection | ML System Design Problem Breakdown
The final sections of the 'Bot Detection' video provide a fantastic overview of modeling strategies, including a sophisticated architecture and a deep dive into two key unsupervised algorithms.
Please watch the 'Modeling' section (29:35 - 40:55) and the 'Deep Dive' on unsupervised detection (49:16 - 59:23). In the 'Modeling' section, focus on the late fusion architecture. This is a powerful pattern where different types of features (e.g., sequence features, graph features, dense features) are processed by specialized model branches before being combined. In the 'Deep Dive', focus on the intuition behind Isolation Forests (how many random splits does it take to isolate a data point?) and Autoencoders (how well can the model reconstruct an event after compressing it?).
- Isolation Forest: This algorithm builds a forest of random trees. The intuition is that anomalous points are "few and different" and will therefore be easier to isolate, meaning they will have a shorter average path from the root of the trees. It's fast and works well with high-dimensional data.
- Autoencoder: This is a type of neural network trained to reconstruct its own input. It first compresses the input into a low-dimensional representation (encoding) and then tries to rebuild the original from that compression. If the model is trained on "normal" data, it will have a high reconstruction error when it sees an anomalous event, which it hasn't learned to represent effectively.
3. Score Fusion and Alerting
Our system now produces multiple risk signals: a deterministic flag from the "impossible travel" rule, a score from the Isolation Forest, and a reconstruction error from the Autoencoder. The Score Fusion component, as described in the Designing a Fraud Detection System article, combines these into a single, unified risk score.
A simple weighted sum is a common starting point:
This final score is then compared against thresholds to make a decision:
- Low Score: No action.
- Medium Score: Flag for analyst review (e.g., create a low-priority ticket).
- High Score: Trigger an immediate alert and potentially an automated response, like forcing MFA on the user's next login.
Conclusion
In this lesson, we designed a sophisticated, two-pronged anomaly detection system that builds directly upon the audit logging pipeline from our previous session. We combined the precision of rule-based engines for known threats like "impossible travel" with the discovery power of unsupervised machine learning to find novel attacks.
Key Takeaways:
- Hybrid Approach: A robust anomaly detection system leverages both deterministic rules (for precision and compliance) and ML models (for discovering unknown patterns).
- Stateful Stream Processing: Behavioral detection is inherently stateful. Systems must maintain per-entity state (e.g., last login location per user) using tools like Redis or Flink's managed state.
- Feature Engineering is Key: The quality of an ML detection system is heavily dependent on the quality of the features engineered from the raw data stream.
- Unsupervised Models for Security: In the absence of large labeled datasets, unsupervised models like Isolation Forests and Autoencoders are powerful tools for learning a "normal" baseline and detecting deviations.
- Score Fusion for Decisioning: Combining signals from multiple detectors into a single risk score allows for nuanced, threshold-based alerting and response.
Preview of the Next Lesson:
So far, we have focused on architectures for detecting suspicious activity after it has occurred. In our next lesson, we will shift our focus to preventing unauthorized access from the start. We will begin our module on Zero Trust by tackling the outcome: Design the end-to-end connection flow for a ZTNA service, contrasting the data and control planes with a traditional VPN architecture. This will be a fascinating look at how modern security is moving away from perimeter-based trust and towards a model of "never trust, always verify."