Defending SMS APIs from Abuse: Multi-Layer Protection with Redis+Lua Token Bucket & Bloom Filters
A comprehensive guide to protecting SMS interfaces from malicious flooding, covering emergency response, multi-layer defense (frontend to third-party), business logic optimization, monitoring, and production-ready code for distributed token buckets, Bloom filters, and sliding-window circuit breakers.
Phase 1: Emergency Stop-Loss (Golden 30 Minutes)
Immediate actions to contain damage:
Immediate Degradation : Switch SMS interface to "test mode" via config center (returns success but sends nothing) or directly circuit-break the endpoint.
IP Blacklist at Nginx : Block abnormal IP ranges at the Nginx layer, intercepting 90%+ of malicious traffic.
Gateway Rate-Limit Fallback : Enable global rate limiting at the gateway (e.g., 100 req/s) to prevent service collapse.
Temporary Shutdown : If above measures fail, take the SMS sending feature offline to protect core business availability.
Phase 2: Multi-Layer Protection System (Full-Chain Interception)
Build a stereoscopic defense net from frontend to backend, gateway to business. The article includes an architecture diagram (image preserved).
Frontend Layer : Graphic CAPTCHA, SMS countdown, button disable — blocks 80% ordinary flooding; difficulty ★☆☆☆☆.
Nginx Layer : IP rate limit, Referer check, User-Agent filter — blocks 15% crawler traffic; difficulty ★★☆☆☆.
Gateway Layer : Token bucket rate limit, API signature, IP black/whitelist — blocks 4% malicious attacks; difficulty ★★★☆☆.
Business Layer : Phone frequency limit, device fingerprint, behavior risk control — blocks 0.9% advanced attacks; difficulty ★★★★☆.
Third-Party Layer : Platform built-in risk control, daily send quota — last line of defense; difficulty ★☆☆☆☆.
Phase 3: Business-Side Optimization (Root Cause Fix)
Frequency Limits : Same phone ≤1 request/60s, ≤5/24h, ≤20/month.
Scenario Isolation : Separate limits for registration, login, password reset to avoid cross-impact.
Verification Upgrade : Force slider CAPTCHA or voice verification for anomalous IPs/phones.
Cost Sharing : Introduce paid mechanism for high-risk ops (e.g., bulk send) to raise attacker cost.
Number Validation : Integrate carrier number databases to filter virtual numbers, IoT cards, and risky numbers.
Phase 4: Monitoring & Post-Mortem
Real-Time Monitoring : Dashboard for send volume, success rate, failure rate, anomalous IP requests with threshold alerts.
Log Audit : Record IP, phone, device info, timestamp for every send request to enable traceback.
Attack Analysis : Regularly analyze attack patterns, update risk rules, form a closed loop.
Stress Testing : Simulate malicious flooding to validate defense effectiveness.
Core Technical Details
Rate-Limit Algorithm : Token bucket (supports burst traffic) over leaky bucket; distributed atomic implementation via Redis+Lua.
Cache Design : Phone frequency limits use Redis INCR + EXPIRE with auto-reset expiration.
Anti-Flood Tricks : Redis HyperLogLog for daily distinct phone counting (memory-efficient); Bloom filter for fast invalid-phone filtering.
Canary Release : New risk rules validated on small traffic before full rollout.
Interview Q&A Highlights
Q: How to ensure rate-limit accuracy in distributed environments?
A: Redis+Lua for atomic operations; adopt eventual consistency allowing tiny error margin.
Q: Attacker uses proxy IP pools?
A: Multi-dimensional risk control combining device fingerprint, phone, behavior — not just IP.
Q: Redis goes down?
A: Redis cluster for HA; keep a local in-memory fallback rate-limit rule as last resort.
Core Code Implementations
1. Redis+Lua Distributed Token Bucket (Core, Interview Favorite)
Highlight : Lua guarantees atomicity, avoids concurrent counting drift; supports burst traffic; performance 10x+ over distributed locks.
-- Token bucket Lua script (KEYS[1]=limit key, ARGV[1]=capacity, ARGV[2]=tokens/sec, ARGV[3]=current timestamp)
local key = KEYS[1]
local capacity = tonumber(ARGV[1])
local rate = tonumber(ARGV[2])
local now = tonumber(ARGV[3])
-- Get remaining tokens and last refresh time
local bucket = redis.call('HMGET', key, 'tokens', 'last_refresh')
local tokens = tonumber(bucket[1]) or capacity
local last_refresh = tonumber(bucket[2]) or now
-- Calculate new tokens generated
local delta = math.max(0, now - last_refresh) * rate
tokens = math.min(capacity, tokens + delta)
-- Try to acquire token
if tokens >= 1 then
redis.call('HMSET', key, 'tokens', tokens - 1, 'last_refresh', now)
redis.call('EXPIRE', key, 3600) -- 1 hour expiry
return 1 -- success
else
return 0 -- fail
end// Java caller
@Service
public class RateLimitService {
@Autowired
private StringRedisTemplate redisTemplate;
private static final String LUA_SCRIPT = "above Lua script";
public boolean tryAcquire(String key, int capacity, int rate) {
long now = System.currentTimeMillis() / 1000;
Long result = redisTemplate.execute(
new DefaultRedisScript<>(LUA_SCRIPT, Long.class),
Collections.singletonList(key),
String.valueOf(capacity),
String.valueOf(rate),
String.valueOf(now)
);
return result != null && result == 1;
}
}2. Phone Frequency Limit (Redis Atomic Ops)
Highlight : INCR + EXPIRE implements sliding window, avoids multi-thread concurrency issues; per-scene isolation.
@Service
public class SmsFrequencyService {
@Autowired
private StringRedisTemplate redisTemplate;
private static final Map<String, int[]> SCENE_LIMIT = Map.of(
"register", new int[]{1, 60, 5, 86400}, // register: 1/60s, 5/24h
"login", new int[]{1, 60, 10, 86400}, // login: 1/60s, 10/24h
"reset", new int[]{1, 60, 3, 86400} // reset: 1/60s, 3/24h
);
public boolean checkFrequency(String phone, String scene) {
int[] limit = SCENE_LIMIT.get(scene);
if (limit == null) return false;
String minuteKey = "sms:freq:" + scene + ":minute:" + phone;
String dayKey = "sms:freq:" + scene + ":day:" + phone;
// 1-minute check
Long minuteCount = redisTemplate.opsForValue().increment(minuteKey);
if (minuteCount == 1) redisTemplate.expire(minuteKey, limit[1], TimeUnit.SECONDS);
if (minuteCount > limit[0]) return false;
// 24-hour check
Long dayCount = redisTemplate.opsForValue().increment(dayKey);
if (dayCount == 1) redisTemplate.expire(dayKey, limit[3], TimeUnit.SECONDS);
return dayCount <= limit[2];
}
}3. Bloom Filter for Fast Invalid Phone Filtering
Highlight : Extreme space efficiency — 10M entries in ~12MB; O(1) query.
@Component
public class PhoneBlacklistFilter {
private BloomFilter<String> bloomFilter;
@PostConstruct
public void init() {
// Expected 10M entries, 0.01% false positive rate
bloomFilter = BloomFilter.create(Funnels.stringFunnel(StandardCharsets.UTF_8), 10_000_000, 0.0001);
List<String> blacklist = phoneBlacklistMapper.selectAll();
blacklist.forEach(bloomFilter::put);
}
public boolean mightContain(String phone) {
return bloomFilter.mightContain(phone);
}
public void addBlacklist(String phone) {
bloomFilter.put(phone);
phoneBlacklistMapper.insert(phone);
}
}4. API Signature Verification (Prevent Illegal Calls)
Highlight : Timestamp + nonce anti-replay; signature algorithm anti-tamper; client secret dynamic update.
@Service
public class ApiSignService {
@Value("${sms.api.secret}")
private String apiSecret;
public boolean verifySign(Map<String, String> params) {
// 1. Timestamp within 5 minutes
long timestamp = Long.parseLong(params.get("timestamp"));
if (System.currentTimeMillis() - timestamp > 5 * 60 * 1000) return false;
// 2. Nonce replay check
String nonce = params.get("nonce");
if (redisTemplate.hasKey("sms:nonce:" + nonce)) return false;
redisTemplate.opsForValue().set("sms:nonce:" + nonce, "1", 5, TimeUnit.MINUTES);
// 3. Generate and compare signature
String clientSign = params.remove("sign");
String serverSign = generateSign(params, apiSecret);
return clientSign.equals(serverSign);
}
private String generateSign(Map<String, String> params, String secret) {
List<String> keys = new ArrayList<>(params.keySet());
Collections.sort(keys);
StringBuilder sb = new StringBuilder();
for (String key : keys) {
sb.append(key).append("=").append(params.get(key)).append("&");
}
sb.append("secret=").append(secret);
return DigestUtils.md5DigestAsHex(sb.toString().getBytes()).toUpperCase();
}
}Technical Challenges & Solutions (Interview Bonus)
Distributed rate-limit counting inaccurate : Multi-node concurrent requests cause over-counting → Redis+Lua atomic ops; all check-and-update in one Lua script, avoids race conditions.
Proxy IP pools bypass IP rate limit : Attackers rotate millions of proxy IPs → Multi-dimensional joint risk control: IP + phone + device fingerprint + behavior (click interval, swipe trajectory).
Redis downtime breaks anti-flood : Single point of failure collapses entire defense → Cluster + local fallback: 1. Redis master-slave + Sentinel cluster; 2. Local in-memory fallback rule (e.g., 10 req/s per instance).
CAPTCHA cracked by solving platforms : Image CAPTCHA recognition cost ~$0.001 → Dynamic CAPTCHA upgrade: 1. Force slider CAPTCHA for anomalous users; 2. Voice CAPTCHA for high-risk; 3. Add noise lines/dots to images.
Redis CPU bottleneck under high concurrency : 100k+ req/s saturates Redis CPU → Multi-level cache + sharding: 1. Local cache hot data (e.g., 5-min rate-limit results); 2. Redis sharded by business; 3. Redis Pipeline for batch ops.
False positives blocking legitimate users : Over-strict risk rules hurt UX → Canary + whitelist: 1. New rules 10% traffic canary; 2. Whitelist for internal testers & veteran users; 3. Appeal channel for quick recovery.
Virtual/IoT numbers for bulk registration : Attackers use cheap virtual numbers → Carrier number validation: 1. Integrate three major carrier databases; 2. Filter 170/171/162 virtual segments; 3. High-risk numbers require real-name auth.
SMS API CC attack : Hundreds of thousands req/s cause avalanche → Full-chain rate limit + circuit break: 1. Nginx IP limit (10 req/s); 2. Gateway global limit (1000 req/s); 3. Sentinel circuit break & degrade.
SMS send chain timeout : Third-party slow response blocks threads → Async send + timeout control: 1. Thread pool async send; 2. 5s third-party timeout; 3. Retry max 2 times.
Attack traceability difficult : Cannot identify real attacker identity → Full-chain log audit: 1. Log IP, UA, device fingerprint, timestamp for all requests; 2. Store in ELK for fast search; 3. Auto-generate attack reports for anomalies.
Interview Ultimate Bonus Answer
When asked "What else can be optimized?":
Introduce AI Risk Control : ML models analyze behavior features to auto-detect anomalies, more precise than rule-based.
SMS Channel Fallback : Auto-switch to backup channel when primary is under attack, ensuring business continuity.
Cost Optimization : Differentiate verification by risk level — high-risk users get SMS, low-risk get CAPTCHA — reducing SMS spend.
Real Interview Simulation
The article includes a detailed mock interview dialogue covering:
Layer 1: Behavioral CAPTCHA (slider/click) with ticket verification via third-party (Geetest, NetEase Yidun) — highest ROI, blocks 80%+ scripts.
Layer 2: Gateway IP rate limit via Redis+Lua sliding window/token bucket (e.g., 5 req/min per IP); can use Kong/APISIX plugins.
Layer 3: Business multi-dimensional risk control combining phone, user ID, device fingerprint, IP-phone correlation (e.g., 1 IP requesting >3 phones in window → blacklist). Device fingerprint hard to forge at scale.
Layer 4: Channel-level circuit break & cost guard — per-minute send threshold triggers degrade; daily budget monitoring (80% alert, 100% hard stop); core vs marketing channel isolation.
Post-mortem: Full-context logging (IP, device fingerprint, user ID, phone, CAPTCHA result) to ELK/ClickHouse; offline analysis feeds blacklists back into live system for continuous improvement.
Best cost-performance layer: Behavioral CAPTCHA — low cost, quick integration, kills 80%+ automation, but defense-in-depth still required.
Code Deep-Dive: Two Technical Highlights
Highlight 1: Declarative Multi-Dimensional Rate Limit (Redis+Lua+Custom Annotation)
Encapsulate @RateLimit annotation for flexible dimension (phone, IP, user ID) and threshold; Redis+Lua ensures atomicity.
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RateLimit {
String key(); // SpEL support, e.g. "#mobile" "#ip"
int limit(); // max requests in window
int window(); // window size in seconds
String message() default "Too frequent, try later";
}@Aspect
@Component
public class RateLimitAspect {
@Autowired
private StringRedisTemplate redisTemplate;
private static final String LUA_SCRIPT = "local key = KEYS[1] " +
"local limit = tonumber(ARGV[1]) " +
"local window = tonumber(ARGV[2]) " +
"local current = redis.call('INCR', key) " +
"if current == 1 then " +
" redis.call('EXPIRE', key, window) " +
"end " +
"if current > limit then " +
" return 0 " + // over limit
"else " +
" return 1 " + // allow
"end";
@Around("@annotation(rateLimit)")
public Object around(ProceedingJoinPoint pjp, RateLimit rateLimit) throws Throwable {
String dynamicKey = parseSpel(rateLimit.key(), pjp);
String redisKey = "rate_limit:" + rateLimit.key() + ":" + dynamicKey;
Long result = redisTemplate.execute(
new DefaultRedisScript<>(LUA_SCRIPT, Long.class),
Collections.singletonList(redisKey),
String.valueOf(rateLimit.limit()),
String.valueOf(rateLimit.window())
);
if (result == 0) {
throw new BusinessException(rateLimit.message());
}
return pjp.proceed();
}
// SpEL parsing omitted, Spring provides utilities
}@RestController
public class SmsController {
// Phone dimension: 1 per 60s
@RateLimit(key = "#mobile", limit = 1, window = 60, message = "Code sent, wait a bit")
// IP dimension: 5 per 60s (can combine or configure separately at gateway)
@RateLimit(key = "#request.remoteAddr", limit = 5, window = 60, message = "Too frequent")
@PostMapping("/send-sms")
public Result sendSms(@RequestParam String mobile, HttpServletRequest request) {
// ... send logic
}
}Highlights : Atomicity via Lua prevents over-counting; declarative annotation keeps business code clean; dimensions stack freely; Redis handles 10k+ QPS per instance easily.
Highlight 2: SMS Channel Sliding Window Circuit Breaker
Before calling provider, real-time check if channel overloaded. Lightweight thread-safe sliding window counter, zero external deps.
public class SlidingWindowCircuitBreaker {
private final int windowSize;
private final int maxRequests;
private final AtomicInteger[] buckets;
private volatile long startTime;
private final AtomicInteger totalCount = new AtomicInteger(0);
public SlidingWindowCircuitBreaker(int windowSize, int maxRequests) {
this.windowSize = windowSize;
this.maxRequests = maxRequests;
this.buckets = new AtomicInteger[windowSize];
for (int i = 0; i < windowSize; i++) buckets[i] = new AtomicInteger(0);
this.startTime = System.currentTimeMillis() / 1000;
}
public synchronized boolean tryAcquire() {
long now = System.currentTimeMillis() / 1000;
if (now - startTime >= windowSize) resetWindow(now);
slideWindow(now);
if (totalCount.get() >= maxRequests) return false; // circuit open
int index = (int) (now % windowSize);
buckets[index].incrementAndGet();
totalCount.incrementAndGet();
return true;
}
private void slideWindow(long now) {
long expiredSlots = now - startTime;
for (long i = 0; i < expiredSlots && i < windowSize; i++) {
int idx = (int) ((startTime + i) % windowSize);
int oldVal = buckets[idx].getAndSet(0);
totalCount.addAndGet(-oldVal);
}
startTime = now;
}
private void resetWindow(long now) {
for (AtomicInteger bucket : buckets) bucket.set(0);
totalCount.set(0);
startTime = now;
}
}@Service
public class SmsService {
// 1-min window, max 100 sends, then circuit break
private SlidingWindowCircuitBreaker breaker = new SlidingWindowCircuitBreaker(60, 100);
public void sendSms(String mobile, String content) {
if (!breaker.tryAcquire()) {
throw new SmsCircuitBreakException("SMS channel busy, try later");
}
// call provider...
}
}Highlights : Pure in-memory, zero external deps, ultra-low latency; synchronized sufficient for low-frequency control; second-precision sliding window avoids burst traffic blowing up the channel.
Technical Difficulties Summary
Automated script breakthrough : Headless browsers + solving platforms call API directly → Behavioral CAPTCHA + secondary verification, force human-machine distinction → Client CAPTCHA ticket verification.
Concurrent counting inaccuracy : Multi-instance/thread simultaneous increment causes over-count → Redis+Lua atomic ops, single-threaded counting → @RateLimit + Lua script.
Proxy IP pool bypass : Massive proxy IPs render IP rate limit useless → Multi-dimensional combo: device fingerprint + phone + user ID joint risk control → Multi-dim annotation stacking, risk rule engine.
SMS channel cost avalanche : Even if internal rate limit breached, SMS spend can explode → Channel-level circuit break + daily cost monitor + core/marketing channel isolation → Sliding window circuit breaker, cost alerts.
Rigid risk rules : Attack/defense evolves, hard-coded rules can't respond fast → Dynamic rule engine loading, offline log analysis feeds blacklists → Drools/custom rule engine + ELK analysis pipeline.
Distributed consistency : Rate-limit state must be unified across cluster → Centralized Redis counters, Lua guarantees read-write atomicity → All limit keys in Redis.
These solutions interlock, ensuring rigid real-time protection while retaining flexible post-hoc learning and dynamic adjustment , with relatively few production pitfalls.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
