Surviving Infinite-Cardinality Attacks Without External Caches
Traditional defenses against API abuse often rely on external caching systems to track request frequencies. However, these systems struggle with the concept of “infinite cardinality” – scenarios where unique request identifiers are virtually limitless. This is particularly problematic for edge reverse proxies or ingress gateways that face public APIs. Automated scrapers, credential stuffing bots, and runaway agent loops can overwhelm these systems by generating an astronomical number of unique requests, effectively creating a “cardinality bomb” that exhausts resources or leads to inaccurate rate limiting.
The core challenge lies in tracking the frequency of events when the set of possible events is unbounded. Standard hash maps or counters require memory proportional to the number of unique items seen, which is infeasible for infinite cardinality. This is where advanced data structures, specifically fixed-memory sketches, become crucial.
The solution involves implementing algorithms that can estimate frequencies and recency within a bounded memory footprint. This allows an edge proxy to maintain state about incoming requests without needing to store every single unique identifier. The focus shifts from exact counts to probabilistic estimations that are accurate enough for practical rate limiting and abuse detection.
Fixed-Memory Sketches for Edge Defense
The key innovation is the application of fixed-memory probabilistic data structures, such as Count-Min Sketch or HyperLogLog, adapted for real-time stream processing at the network edge. These sketches work by hashing incoming data points (e.g., API request identifiers, user agents, IP addresses) into a fixed-size array of counters. While collisions are possible, the probabilistic nature allows for highly accurate estimations of frequencies and, with modifications, recency.
For recency-weighted frequency, a technique called Interpreted Decay can be employed. This method allows for calculating recency-weighted frequencies on-the-fly. By observing the formula count >> age, where count is the frequency and age represents the recency of the event, we can derive a recency-weighted count. This calculation can be performed within a single 64-bit word, drastically reducing memory overhead. This approach avoids the need to store timestamps for every request, which would be prohibitively expensive.
Furthermore, these sketches can be implemented efficiently using atomic operations, such as Compare-And-Swap (CAS), to handle concurrent access from multiple cores without locks. This lock-free, zero-allocation approach is critical for achieving sub-nanosecond operation latencies required at the network edge. Techniques like bit-chipping eviction lotteries can be used to manage the sketch's memory, ensuring that older or less frequent items are probabilistically removed to make space for new data.

Defending Against Specific Attack Vectors
This approach offers robust defense against several common API abuse scenarios:
- Infinite-Cardinality Attacks: By using fixed-memory sketches, the proxy can track the rate of unique identifiers (e.g., generated session IDs, user tokens) without running out of memory. Even if an attacker generates billions of unique IDs, the sketch’s memory usage remains constant.
- Credential Abuse: For login endpoints, the system can track the frequency of specific username or token attempts originating from various IP addresses. A high rate of failed attempts from a single source, even with different credentials, can be flagged.
- Runaway Agent Loops: Bots or scrapers that continuously make requests, potentially with slight variations to avoid simple IP blocking, can be detected. The sketch can identify anomalous patterns in the frequency and recency of requests, even if the specific identifiers change.
The surprise here is that effective defense against potentially unbounded request volumes can be achieved with deterministic, bounded memory structures. Traditionally, handling such scale would necessitate distributed caching layers or complex state management, adding latency and operational overhead. The fixed-memory sketch approach integrates directly into the edge proxy, processing requests in-line with minimal performance impact.
Implementing the Solution
The implementation of these fixed-memory sketches at the edge proxy involves several key components:
- Hashing Function: A fast, high-quality non-cryptographic hash function is needed to map request attributes to sketch indices.
- Sketch Data Structure: A Count-Min Sketch or a similar structure to store frequency estimates. For recency, modifications or a secondary sketch might be employed.
- Atomic Updates: Lock-free mechanisms (e.g., CAS loops) to ensure thread-safe updates to the sketch counters.
- Rate Limiting Logic: The logic that queries the sketch to determine if a given request rate exceeds predefined thresholds. This logic must account for the probabilistic nature of the sketch, potentially using confidence intervals.
- Eviction Strategy: A mechanism to manage the sketch's fixed memory, ensuring that stale data does not indefinitely occupy space.
The operational benefit is significant: reduced infrastructure costs by eliminating external caches, lower latency due to in-line processing, and more accurate rate limiting that adapts to dynamic traffic patterns without being susceptible to cardinality bombs. This allows developers to deploy robust API defenses at the network edge, directly within their ingress infrastructure.
