Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Behavioral DDoS Detection & Adaptive Rate Limiting

Hello! Welcome back to our course on designing security products.

In our last lesson, we designed a robust SYN proxy and flow analysis system. This provided us with a stateful Layer 4 defense capable of terminating TCP connections, filtering protocol anomalies, and protecting our origin servers. However, this defense is largely blind to what happens inside those legitimate connections. An attacker can establish a valid TCP session and then launch a devastating attack using perfectly formed application-layer requests.

This brings us to the focus of today's lesson: moving up the stack to Layer 7. We will address the learning outcome: Design Layer 7 DDoS detection using behavioral baselining and adaptive rate limiting to distinguish floods from legitimate traffic. We will architect a system that can understand the behavior of application traffic, differentiate a malicious flood from a legitimate surge (like a flash sale), and apply intelligent, dynamic controls.

1. The Challenge of Layer 7 DDoS Attacks

While Layer 3/4 attacks are about overwhelming network capacity with malformed or high-volume packet floods, Layer 7 attacks are more subtle and often more effective. They target application resources (CPU, memory, database connections) by mimicking legitimate user behavior.

Examples include:

  • HTTP GET/POST Floods: Repeatedly requesting a heavy, database-intensive page or submitting a form.
  • API Abuse: Bombarding specific API endpoints to exhaust application logic or backend resources.
  • "Low-and-Slow" Attacks: Sending requests at a slow rate that appears normal for a single user, but when multiplied across thousands of bots, it slowly exhausts server resources.

A simple, static rate limit (e.g., "100 requests per IP per minute") is a blunt instrument. It can't distinguish an attack from a sudden, legitimate traffic spike. It also doesn't account for the fact that not all requests are equal; a request to a static homepage is cheap, while a complex search query is expensive. We need a more intelligent approach.

2. Behavioral Baselining: Learning What's "Normal"

To spot abnormal behavior, we must first define what's normal. This is the core principle of behavioral baselining. Instead of relying on static rules, we design a system that continuously learns the typical patterns of our application's traffic and flags significant deviations as potential attacks.

This process involves a multi-layered architecture, which you can explore in the following paper.

Behavior-Based DDoS Detection for Multi-Vector Attacks

The paper 'Behavior-Based DDoS Detection' provides a solid theoretical framework for the system we want to build. It outlines a five-layer architecture for collecting, processing, and analyzing traffic data to detect anomalies.

Please read the 'Architecture Overview' section, focusing on the description of the five layers, from 'Data Collection' to the 'Behavior Modeling Layer'. Pay close attention to the machine learning algorithms mentioned: DBSCAN and Isolation Forest. We'll be discussing their roles next.

As the paper describes, our system architecture will follow this logical flow:

  1. Data Collection Layer: Our reverse proxy (which we can build upon the SYN proxy from our last lesson) is the perfect vantage point. It can log rich L7 data for every request: source IP, user ID, requested URL, HTTP method, headers (like User-Agent), response latency, and status codes.
  2. Preprocessing & Feature Extraction: The raw logs are streamed to a processing pipeline that cleans the data and extracts key features for analysis.
  3. Behavior Modeling Layer: This is the heart of the detection engine. It uses machine learning models to build a multi-dimensional picture of "normal."

Given your interest in first principles, let's briefly touch on the intuition behind the algorithms mentioned in the paper:

  • Clustering (e.g., DBSCAN): Imagine plotting all your traffic on a multi-dimensional graph (dimensions could be request rate, URL entropy, etc.). Normal traffic from legitimate users will form dense clusters. DBSCAN is excellent at finding these clusters. Any data point that falls far outside these dense regions is considered an outlier—an anomaly.
  • Anomaly Detection (e.g., Isolation Forest): This algorithm works by trying to "isolate" individual data points. Anomalous points are, by definition, different and few. Therefore, they are much easier to separate from the rest of the data than normal points, which are numerous and similar. The algorithm builds random decision trees, and the average number of splits it takes to isolate a point becomes its anomaly score.

By combining these techniques, the system can build a dynamic baseline that understands, for example, that a high request rate to the login page is normal, but a high rate to a specific user's private settings page from many different IPs is highly anomalous.

This entire feedback loop is visualized well in the following diagram.

Adaptive Security Architecture for Layer 7 DDoS Protection
This diagram shows a closed-loop architecture where an 'Edge Engine' sends logs to a 'Big Data Cluster'. An 'AI Engine' analyzes this data to create behavioral baselines and ML models, which are then pushed back to the edge in near real-time to enable adaptive protection.

3. Adaptive Rate Limiting: The Dynamic Response

Once our behavioral model calculates an anomaly score for a given client (e.g., an IP address), we need to act. This is where adaptive rate limiting comes into play. Unlike a static limit, an adaptive limit is a dynamic policy that changes based on real-time data.

The core idea is to use the anomaly score and other system health metrics (like origin server CPU utilization or response time) to continuously adjust the allowed traffic rate.

Adaptive Rate Limiting for Microservices

This paper, 'Adaptive Rate Limiting for Microservices,' explains the 'why' and 'how' of moving from static to adaptive limits. It introduces the concept of a feedback loop that uses real-time system metrics to tune traffic controls.

Please read the following sections: 'Introduction' and 'The Need for Adaptive Rate Limiting': These sections articulate the limitations of static limits and introduce the core concept of adaptive rate limiting driven by real-time system health. 'Results and Explanation' through 'Conclusion': Focus on the 'Traffic Surge (DDoS)' scenario in the tables. This demonstrates empirically how an adaptive system outperforms a static one in terms of throughput and response time during an attack.

As the paper demonstrates, an adaptive system can intelligently throttle traffic during an attack to preserve service for legitimate users, whereas a static system is quickly overwhelmed.

The key is to translate the anomaly score into a concrete action. A common and highly effective mechanism for implementing this is the token bucket algorithm.

4. Architecting the Adaptive Rate Limiter

The token bucket algorithm provides two main parameters we can control:

  • Bucket Size (Burst): The number of requests a client can make in a short burst.
  • Refill Rate (Sustain): The steady-state rate of requests a client can make over time.

In an adaptive system, these parameters are not fixed. They are determined by our behavioral detection engine.

  • A client with a low anomaly score gets a large bucket and a high refill rate.
  • A client with a high anomaly score gets a small bucket and a very low refill rate.

To dive into the system design of a distributed rate limiter that can implement this, please watch the following video.

Design a Distributed Rate Limiter w/ a Ex-Meta Staff Engineer: System Design Breakdown

This video, from a former Meta engineer, breaks down the system design of a distributed rate limiter from first principles. It covers the core algorithm, scaling with a distributed cache like Redis, and handling concurrency issues—all essential for building our enforcement layer.

Please watch the following segments: The Token Bucket Algorithm (22:37 - 30:10): Focus on understanding how the bucket size controls burstiness and the refill rate controls sustained load. This is the core mechanism we will adapt. Implementation with Redis (30:10 - 35:43): Pay close attention to how Redis is used for shared state and how Lua scripting is used to solve race conditions atomically. This is a critical implementation detail for a distributed system. Scalability & High Availability (39:25 - 47:30): This covers sharding Redis to handle high request volumes and using replication to ensure fault tolerance.

The video provides the blueprint for our enforcement component. Now, let's integrate this with our behavioral detection system.

Rate Limiter System Design
This diagram shows the core components of a rate-limiting system. Client requests are checked by middleware, which uses Redis for state and a set of rules. Legitimate requests go to the API servers, while rate-limited ones are either dropped or queued.

Our adaptive architecture enhances this model:

  1. Rules are Dynamic: The "Rules" in the diagram are not static. Our AI/ML engine continuously analyzes traffic and pushes updated policy configurations (e.g., token bucket parameters for different anomaly scores) to a configuration store like ZooKeeper or etcd, as mentioned in the video.
  2. Middleware Subscribes to Updates: The rate limiter middleware subscribes to these updates. When it receives a new policy, it updates its in-memory configuration.
  3. Enforcement via Redis: For each incoming request, the middleware fetches the client's current token count and last refill time from the sharded Redis cluster. It then applies the dynamically configured token bucket logic (using an atomic Lua script) to decide whether to allow or block the request.

This architecture creates a powerful separation of concerns:

  • The Data Plane (Real-Time): The rate limiter middleware operates at extremely high speed. Its job is simple: look up a client's state in Redis, apply a simple mathematical rule, and forward or drop the request.
  • The Control Plane (Near-Real-Time): The complex, computationally-intensive work of behavioral analysis happens asynchronously in the big data/AI backend. This plane is responsible for providing the intelligence that guides the data plane's actions.

Conclusion

In this lesson, we have designed a sophisticated Layer 7 DDoS detection and mitigation system. We moved beyond static defenses to an intelligent, adaptive architecture that learns from traffic behavior.

Key Takeaways:

  • Layer 7 DDoS attacks mimic legitimate user traffic to exhaust application resources, requiring deeper inspection than L3/4 defenses.
  • Behavioral baselining uses machine learning (e.g., clustering, anomaly detection) to dynamically define "normal" traffic and identify malicious deviations.
  • Adaptive rate limiting provides a dynamic response, using anomaly scores and system health to adjust traffic controls in real-time.
  • The token bucket algorithm is a flexible and efficient mechanism for implementing adaptive rate limits, where the bucket size and refill rate are dynamically controlled.
  • A robust system architecture separates the high-speed data plane (enforcement) from the intelligent control plane (analysis), allowing for both scalability and sophisticated detection.

Preview of the Next Lesson:

We now have distinct, powerful defenses for both Layer 3/4 and Layer 7 attacks. However, modern attacks are often multi-vector, combining volumetric floods with subtle application-layer tactics. How do we build a system that can handle both simultaneously and efficiently?

In the next lesson, we will focus on integrating these capabilities. The learning outcome will be: Architect a multi-layer traffic scrubbing pipeline that sequences inspection stages and fast-paths clean traffic. We will design a system that intelligently applies the right defense at the right stage, ensuring maximum protection with minimal latency.

Can't find a good explanation? Sign up and we'll make it for you

Sign up