Overview
Argos Mesh implements a sophisticated rate limiting mechanism through the TrafficAnalyzer component in the Sentinel service. This prevents abuse by limiting the number of requests from a single IP address within a time window.The rate limiter uses Redis for distributed state management, making it scalable across multiple Sentinel instances.
Rate Limiting Algorithm
The system employs a sliding window counter algorithm with the following parameters:- Limit: 50 requests per IP address
- Window: 10 seconds
- Action: Automatic IP ban for 10 minutes when threshold exceeded
When a
ProductSoldInternalEvent is received from RabbitMQ, the Sentinel service extracts the IP address and begins analysis.String key = RATE_PREFIX + ip; // "rate:ip:192.168.1.100"
Long currentCount = redisTemplate.opsForValue().increment(key);
if (currentCount != null && currentCount == 1) {
redisTemplate.expire(key, Duration.ofSeconds(WINDOW_SECONDS));
}
Implementation Details
TrafficAnalyzer Component
The core rate limiting logic resides inTrafficAnalyzer.java:
sentinel/src/main/java/com/argos/sentinel/service/TrafficAnalyzer.java
Redis Key Structure
The rate limiter uses a simple key-value pattern:| Key Pattern | Example | Value | TTL |
|---|---|---|---|
rate:ip:<address> | rate:ip:192.168.1.100 | Counter (Integer) | 10 seconds |
The atomic
increment operation ensures thread-safety even with concurrent requests from the same IP.Integration with Event Processing
The rate limiter is invoked by theSalesListener component for every product sale event:
sentinel/src/main/java/com/argos/sentinel/service/SalesListener.java:26-43
Behavior Examples
Normal Traffic
Scenario: User makes 30 requests in 10 seconds
- Requests 1-30: Processed normally
- Counter resets after 10 seconds
- No ban triggered
Abusive Traffic
Scenario: Bot makes 51 requests in 5 seconds
- Requests 1-50: Processed
- Request 51: Limit exceeded
- IP banned for 10 minutes
- Alert sent to security queue
Configuration Parameters
You can customize the rate limiting behavior by modifying these constants:Performance Considerations
Why Redis?
Atomic Operations
Redis
INCR is atomic and thread-safe, preventing race conditions in high-concurrency scenarios.Distributed State
Multiple Sentinel instances can share the same Redis, ensuring consistent rate limiting across the cluster.
Auto-Expiration
Redis TTL automatically cleans up old counters without manual intervention.
High Performance
In-memory operations provide sub-millisecond latency for counter increments.
Monitoring Rate Limiting
To monitor rate limiting activity in Redis:Next Steps
IP Blocking
Learn how banned IPs are managed in the blacklist
DDoS Protection
Understand the complete DDoS mitigation strategy