Skip to content
API Flood Protection: Emergency Mitigation, Multi-Layer Defense & Rate Limiting Algorithms

API Flood Protection: Emergency Mitigation, Multi-Layer Defense & Rate Limiting Algorithms

This article provides a comprehensive guide to handling API abuse, covering emergency mitigation within three minutes, a multi-layer defense architecture, comparison of four rate limiting algorithms, practical implementations using Redis+Lua token bucket, Sentinel, and Guava Bloom filter, cache protection strategies, idempotency design, and a simulated interview scenario with code examples for distributed rate limiting and replay attack prevention.

Rain's Path to Java Mastery
Rain's Path to Java Mastery
Rain's Path to Java Mastery

Emergency Mitigation (First 3 Minutes)

When an API is flooded, immediate actions are required to keep the system alive:

Rapid Scale-out : Increase service instances to maximum using cloud provider auto-scaling to absorb peak traffic.

Circuit Breaker & Degradation : Non-core interfaces return default values or fallback pages; core interfaces enable circuit breaker protection.

Traffic Cleaning : Integrate CDN/WAF to filter obvious malicious requests (high-frequency access from same IP or device ID).

Temporary Ban : Block anomalous IPs, user IDs, or device IDs for 15 minutes to 24 hours.

Multi-Layer Defense System (Long-Term Stability)

Single-layer protection is insufficient; a defense-in-depth approach is essential.

Multi-layer defense architecture diagram
Multi-layer defense architecture diagram

Four Rate Limiting Algorithms Comparison

Understanding the trade-offs of each algorithm is critical for selection and interviews.

Fixed Window Counter : Counts requests per unit time; simple and performant but suffers from boundary spikes (up to 2x traffic at window switch). Suitable for low-requirement scenarios.

Sliding Window Counter : Splits time window into smaller buckets and slides; solves boundary issue at cost of slightly more complex implementation and storage. Fits most business scenarios.

Leaky Bucket : Requests enter a bucket and leak at fixed rate; forces smooth processing but cannot handle bursts (overflow drops requests). Ideal for message queue peak shaving.

Token Bucket : Generates tokens at fixed rate; request consumes a token. Handles bursts while limiting average rate. Preferred for internet API rate limiting.

Industry standard: Redis+Lua distributed token bucket or frameworks like Sentinel/Resilience4j.

Advanced Optimization Techniques

Distributed Rate Limiting : Gateway enforces global limits; application layer enforces per-interface limits to avoid single-point failure.

Hotspot Isolation : Deploy hot interfaces on dedicated clusters to prevent whole-service collapse.

Anti-Crawler Strategies : Device fingerprinting, User-Agent validation, Cookie verification, dynamic tokens.

Cache Protection : Bloom filter for cache penetration, hot keys never expire, mutex lock for cache breakdown.

Canary Release : Route 10% traffic to new interface, observe stability before full rollout.

Monitoring, Alerting & Postmortem

Real-time Monitoring : Track QPS, latency, error rate, CPU/memory/disk.

Multi-level Alerts : Gradient thresholds (warning → critical → emergency) via SMS, phone, email.

Postmortem Analysis : After attack, analyze source, method, defense gaps; produce report and improve strategies.

Summary

API anti-flooding is a systematic engineering problem. Core philosophy: stop bleeding first, then layer defenses, finally continuously optimize via monitoring and postmortems . Choose algorithms and strategies per business scenario to balance security and user experience.

Core Code Implementations

1. Redis+Lua Distributed Token Bucket (Industry Preferred)

Uses Lua script for atomic Redis operations, avoiding concurrency counting errors; single instance achieves 100k+ QPS.

Step 1: Token Bucket Core Lua Script

-- rate limit key (e.g., rate_limit:user:123:api:/order)
local key = KEYS[1]
-- bucket capacity (max burst)
local capacity = tonumber(ARGV[1])
-- token generation rate (per second)
local rate = tonumber(ARGV[2])
-- requested tokens (usually 1)
local requested = tonumber(ARGV[3])
-- current timestamp (ms)
local now = tonumber(ARGV[4])

-- remaining tokens in bucket
local remaining = tonumber(redis.call('hget', key, 'remaining') or capacity)
-- last refill time
local last_refill = tonumber(redis.call('hget', key, 'last_refill') or now)

-- tokens generated since last refill
local delta = math.max(0, now - last_refill) * rate / 1000
-- update remaining tokens (capped at capacity)
local new_remaining = math.min(capacity, remaining + delta)
-- update last refill time
local new_last_refill = now

-- check if enough tokens
if new_remaining >= requested then
    new_remaining = new_remaining - requested
    -- store new state, set expiry to avoid cold keys
    redis.call('hmset', key, 'remaining', new_remaining, 'last_refill', new_last_refill)
    redis.call('expire', key, 3600)
    return 1 -- allow
else
    return 0 -- reject
end

Step 2: Java Utility Class

@Component
public class RedisRateLimiter {
    @Autowired
    private StringRedisTemplate redisTemplate;

    // preload Lua script to avoid recompilation
    private final DefaultRedisScript<Long> rateLimitScript;

    public RedisRateLimiter() {
        rateLimitScript = new DefaultRedisScript<>();
        rateLimitScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("rate_limit.lua")));
        rateLimitScript.setResultType(Long.class);
    }

    /**
     * Try to acquire token
     * @param key unique rate limit identifier (interface+IP+UID)
     * @param capacity bucket capacity
     * @param rate token generation rate (per second)
     * @return true=allow, false=rate limited
     */
    public boolean tryAcquire(String key, int capacity, int rate) {
        List<String> keys = Collections.singletonList(key);
        Long result = redisTemplate.execute(
            rateLimitScript,
            keys,
            String.valueOf(capacity),
            String.valueOf(rate),
            "1", // 1 token per request
            String.valueOf(System.currentTimeMillis())
        );
        return result != null && result == 1;
    }
}

2. Sentinel Annotation-based Rate Limiting (Spring Cloud Preferred)

Annotation-driven, low intrusion; dynamic rule adjustment via console without restart; built-in circuit breaking, degradation, hot parameter limiting.

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    // define rate limit resource, specify block handler
    @SentinelResource(
        value = "createOrder",
        blockHandler = "createOrderBlockHandler",
        fallback = "createOrderFallback"
    )
    @PostMapping("/create")
    public ResponseEntity<OrderVO> createOrder(@RequestBody OrderDTO orderDTO) {
        return ResponseEntity.ok(orderService.createOrder(orderDTO));
    }

    // handler when rate limited
    public ResponseEntity<OrderVO> createOrderBlockHandler(OrderDTO orderDTO, BlockException e) {
        return ResponseEntity.status(429).body(OrderVO.error("System busy, please try later"));
    }

    // fallback for business exceptions
    public ResponseEntity<OrderVO> createOrderFallback(OrderDTO orderDTO, Throwable e) {
        return ResponseEntity.status(500).body(OrderVO.error("Order creation failed, please try later"));
    }
}

3. Guava Bloom Filter for Cache Penetration Prevention

Extremely space-efficient (1M entries ~1MB), O(1) lookup, blocks 99%+ malicious penetration requests.

@Component
public class BloomFilterService {
    // initialize Bloom filter: expect 1M entries, 0.01% false positive rate
    private final BloomFilter<String> orderIdFilter = BloomFilter.create(
        Funnels.stringFunnel(Charset.defaultCharset()),
        1000000,
        0.0001
    );

    // on startup, load all valid order IDs into filter
    @PostConstruct
    public void init() {
        List<String> allOrderIds = orderMapper.getAllValidOrderIds();
        allOrderIds.forEach(orderIdFilter::put);
    }

    // check if order ID might exist
    public boolean mightContain(String orderId) {
        return orderIdFilter.mightContain(orderId);
    }

    // add new order ID to filter
    public void addOrderId(String orderId) {
        orderIdFilter.put(orderId);
    }
}

Core Technical Challenges & Solutions (Interview Highlights)

Distributed Rate Limiting Atomicity : Plain Redis get+set fails under concurrency. Solution: Redis+Lua atomic script or Redisson distributed limiter. Lua executes all logic atomically via Redis single-threaded nature.

Hot Key Spike : Sudden million-level access to a hot item overwhelms cluster. Solutions: 1) Dedicated cluster for hot interfaces. 2) Local cache (Caffeine) + Redis multi-level cache. 3) Pre-warm hot keys. 4) Sentinel hot parameter limiting. Local cache absorbs 90%+ hot requests, protecting Redis and DB.

Cache Penetration / Breakdown / Avalanche : Penetration: Bloom filter + empty value caching. Breakdown: Mutex lock + hot keys never expire. Avalanche: Randomize expiry + circuit breaker. Layered defense solves all three.

Malicious Requests Bypassing Frontend : Attackers call backend directly. Solutions: 1) Gateway request signature verification (Timestamp+Nonce+Sign). 2) Device fingerprinting. 3) Dynamic token (per-request refresh). 4) User behavior analysis (anomaly → ban). Intercepts at request legitimacy layer.

Rate Limiting Granularity Balance : Global too coarse hurts legitimate users; per-user too fine consumes memory. Solution: Multi-level architecture: Gateway global limit (100k QPS), Application per-interface limit (10k QPS), User-level limit (10 QPS). Progressive layers ensure stability while minimizing false positives.

Service Cascade Failure : Downstream failure drags upstream. Solutions: 1) Service isolation (thread pool / semaphore). 2) Circuit breaker degradation (Sentinel/Hystrix). 3) Timeout controls on all interfaces. Fault isolation prevents propagation, preserves core services.

Supplementary Summary

API anti-flooding is essentially an attack-defense confrontation with no silver bullet. Core principle: intercept maximum malicious traffic at minimum cost while preserving genuine user experience .

Real Interview Simulation

Scenario: Coupon API Flooded by Scripts

Candidate first classifies attack type: same-user high-frequency, multi-IP scraping, or request replay. Assumes logged-in user scripting high-frequency coupon grab. Defense layers:

Frontend Layer (First Filter)

Debounce/throttle: button disabled 2s after click.

One-time page_token: issued on load, consumed on submit.

Slider CAPTCHA for high-risk actions.

Frontend only deters casual attackers; core defense in backend.

Gateway Layer (Hard Threshold)

Stateless filtering via Nginx+Lua or Spring Cloud Gateway+Redis:

IP token bucket: 20 req/s per IP, excess returns 429.

Dynamic blacklist: repeated limit triggers → auto-ban IP for 1 hour.

UA/device fingerprint check: missing browser traits → reject.

if (redis.incr("ratelimit:ip:" + ip) > 20) {
    response.setStatus(429);
    return;
}
redis.expire("ratelimit:ip:" + ip, 1, TimeUnit.SECONDS);

Business Layer (Precise Control)

a) User/Interface Distributed Rate Limiting : Sentinel or Redisson RRateLimiter, e.g., same userId + coupon interface → 1 req/s. Excess returns friendly message.

b) Idempotency Design (Anti-Replay Core) : Client must send global unique requestId or business idempotent key (userId+couponId+activityId). Backend uses Redis SETNX for lightweight lock:

String key = "idem:submit:" + userId + couponId;
boolean ok = redis.setIfAbsent(key, "1", 5, TimeUnit.SECONDS);
if (!ok) return "Do not submit repeatedly";

Database unique index as final safeguard.

c) Risk Control Escalation : User hits limit → graphic CAPTCHA; repeated CAPTCHA failure → short account ban + report to risk system; deeper: Flink real-time scoring on device, behavior, history → high risk directly rejected without CAPTCHA. IP rotation defeated by account/device fingerprint binding.

Cost Confrontation Philosophy

Pure blocking hurts legitimate users. Anti-flooding is cost confrontation : raise attacker cost while barely affecting real users:

New user coupon requires verified phone number.

Suspicious behavior → manual review instead of instant reward.

High-frequency access → salted degradation (return cached data with hidden key info).

Account credit system: low credit = fewer privileges.

Attacker calculates ROI and quits.

Candidate's Complete Funnel Summary

Five-layer funnel: Frontend debounce → Gateway IP limit → Business idempotency + user-level rate limit → Risk CAPTCHA → Database unique index . Layered filtering, progressive pressure, cost-confrontation mindset balancing security and experience.

Top Technical Code Highlights

1. Gateway: IP Blacklist + Dynamic Token Bucket (Redis+Lua)

Lua script atomically executes check-count-ban, eliminating race windows.

-- ratelimit.lua: IP rate limit + auto-ban
local ip = KEYS[1]
local limit = tonumber(ARGV[1]) -- requests per second
local ban_seconds = tonumber(ARGV[2]) -- ban duration
local blacklist_key = "blacklist:" .. ip
local rate_key = "rate:" .. ip

-- 1. check blacklist
if redis.call("exists", blacklist_key) == 1 then
    return -1 -- banned
end

-- 2. sliding window count (simplified token bucket)
local current = redis.call("incr", rate_key)
if current == 1 then
    redis.call("expire", rate_key, 1) -- 1 second window
end

-- 3. exceed limit → blacklist
if current > limit then
    redis.call("set", blacklist_key, 1)
    redis.call("expire", blacklist_key, ban_seconds)
    return -1
end
return 1 -- allow

Highlights: fully atomic server-side; token bucket via incr+expire for high performance; auto-ban without external scheduler.

2. Business Layer: User+Interface Distributed Rate Limiting (Redisson RRateLimiter)

Redisson provides built-in token bucket with cluster support, no custom wheel.

@Service
public class RateLimitService {
    @Autowired
    private RedissonClient redissonClient;

    /**
     * Try acquire token
     * @param userId user ID
     * @param apiKey interface identifier
     * @param permits tokens to consume
     * @return allowed or not
     */
    public boolean tryAcquire(String userId, String apiKey, long permits) {
        String key = "limit:" + apiKey + ":" + userId;
        RRateLimiter limiter = redissonClient.getRateLimiter(key);

        // initialize: 1 token per second, max bucket 5 (allow small burst)
        limiter.trySetRate(RateType.OVERALL, 1, 1, RateIntervalUnit.SECONDS);

        return limiter.tryAcquire(permits);
    }
}

Highlights: Redisson distributed objects naturally support multi-instance; trySetRate only initializes once; adjustable bucket capacity handles bursts, reduces false kills.

3. Idempotent Interceptor: Custom Annotation + AOP (Anti-Replay Weapon)

Declarative annotation adds idempotency to any interface with zero intrusion.

// Annotation definition
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Idempotent {
    /** Idempotent key prefix */
    String prefix();
    /** Key expiry seconds */
    int expire() default 5;
    /** SpEL expression to extract business unique identifier from args */
    String keySpEL();
}
// AOP aspect implementation
@Aspect
@Component
public class IdempotentAspect {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Around("@annotation(idempotent)")
    public Object around(ProceedingJoinPoint joinPoint, Idempotent idempotent) throws Throwable {
        // 1. Parse SpEL to get business idempotent key
        String key = parseSpEL(idempotent.keySpEL(), joinPoint);
        String redisKey = "idem:" + idempotent.prefix() + ":" + key;

        // 2. SETNX atomic placeholder
        Boolean success = redisTemplate.opsForValue()
            .setIfAbsent(redisKey, "1", Duration.ofSeconds(idempotent.expire()));

        if (Boolean.FALSE.equals(success)) {
            return ApiResult.fail("Do not submit repeatedly");
        }

        // 3. Execute original business
        try {
            return joinPoint.proceed();
        } catch (Exception e) {
            // On business exception, delete key to allow safe retry
            redisTemplate.delete(redisKey);
            throw e;
        }
    }
}
// Business usage, extremely simple
@Idempotent(prefix = "claimCoupon", keySpEL = "#req.userId + ':' + #req.couponId")
public Result claimCoupon(ClaimReq req) {
    // core coupon logic
}

Highlights: declarative programming, zero business code pollution; SpEL flexible key composition; exception deletes key enabling safe retry, balancing anti-replay and availability.

Technical Pitfalls & Solutions

Distributed Rate Limiting Consistency vs Performance : Centralized Redis counting with Lua ensures accuracy but may bottleneck. Solution: two-level rate limiting — local pre-filter (Guava RateLimiter) rejects 99% invalid requests; only passed requests hit Redis for precise counting. If Redis fails, local limiter degrades gracefully, sacrificing precision for availability.

Idempotent Key Granularity Chaos : Align with product on "business uniqueness" (e.g., coupon idempotent key = userId+couponId+activityId). Use SpEL for custom keys, database unique index as double insurance.

Blacklist False Positives : Don't ban on single threshold exceed; use "suspected threshold" within time window. Combine with CAPTCHA soft challenge for legitimate users to self-unblock. Use device fingerprint instead of pure IP to reduce NAT collateral damage.

Hardcoded Anti-Flood Rules Requiring Restart : Introduce configuration center for dynamic threshold, ban duration, risk control switches; lightweight rule engine (e.g., QLExpress) for ops configurability.

Deep Dive: Two-Level Rate Limiting (Consistency vs Performance)

Classic CAP trade-off. Every request hitting Redis for incr is precise but risks Redis bottleneck. Two-level architecture:

L1 Local : Guava RateLimiter per instance does coarse-grained filtering, discarding 99% junk.

L2 Remote : Only L1-passed requests go to Redis for accurate counting, drastically reducing Redis load.

if (!localLimiter.tryAcquire()) {
    return fastFail("System busy"); // local reject
}
if (!redisLimiter.tryAcquire(userId, apiKey)) {
    return fastFail("Operation too frequent");
}
// normal business processing

If Redis fails, local limiter still provides degraded protection.

Overlooked Pitfall: Client-Generated RequestId Untrustworthy for Replay Protection

If client generates RequestId, attacker can send new IDs each time bypassing idempotency. Solution: server-signed anti-replay token:

Client requests antiReplayToken (server-issued, contains timestamp+signature).

Submission includes token; backend verifies signature and expiry, one-time consumption.

Even if script forges RequestId, without valid token replay fails. Often missed in interviews but critical in production.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Distributed Systemsbackend developmentRedissystem designSentinelIdempotencyinterview preparationrate limitingBloom filterAPI protection
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.