Q: How do you implement sliding-window rate limiting and DDoS mitigation using HAProxy stick-tables?
Master HAProxy interview questions on stick-table rate limiting: track-sc0, 429 denies, abuser tables, tarpit, peers sync and CDN client IP handling.
Want to master this scenario in a live sandbox? KodeKloud's Istio Service Mesh & Advanced Kubernetes Networking Course covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: Among haproxy interview questions on edge protection, stick-tables come up constantly because they let one process count requests per client in memory, with no external Redis or sidecar. When I get this question I frame it as three jobs: track every client, decide when a client is abusive, and make the decision survive a failover. I run a request-rate counter over a 10 second window, deny with 429 above a threshold, move repeat offenders into a longer penalty table, and sync the tables between both load balancers so an attacker cannot just wait for a failover. I also call out the client IP trap behind a CDN, because that is where most real deployments quietly break.
Define the Stick-Table and Track Every Client by Source IP
A stick-table is a fixed-size in-memory hash keyed here by client IP. I store http_req_rate over 10 seconds, which HAProxy computes as a sliding window of counters, plus conn_cur for concurrent connections. Size 1m holds about a million addresses, and expire 10s drops idle entries so memory stays bounded. The track-sc0 rule must run early so later rules can read the counters.
frontend fe_https
bind :443 ssl crt /etc/haproxy/certs/site.pem
mode http
stick-table type ip size 1m expire 10s store http_req_rate(10s),conn_cur
http-request track-sc0 src
default_backend be_app
Deny Over the Threshold with a 429 and Cap Concurrent Connections
Now I act on the counters. A client above 100 requests per 10 seconds gets a 429, and a client holding more than 50 open connections is cut off too. I keep the thresholds in the config as plain numbers so a reviewer can see them. I always test the limit against my own IP before shipping, because a typo here locks out real users.
frontend fe_https
http-request deny deny_status 429 if { sc_http_req_rate(0) gt 100 }
http-request deny deny_status 429 if { sc_conn_cur(0) gt 50 }
# Quick test from a client:
# for i in $(seq 1 150); do curl -s -o /dev/null -w '%{http_code}\n' https://site.example.com/; done | sort | uniq -c
# 100 200
# 50 429
Escalate Repeat Offenders to an Abusers Table and Tarpit
A flat 429 still lets a botnet hammer you at the limit forever. I add a second table that remembers who tripped the limit, using a general purpose counter, and keep them blocked for ten minutes. For scrapers I prefer tarpit, which holds the connection open for timeout tarpit before answering, so the attacker burns sockets instead of me.
backend st_abusers
stick-table type ip size 100k expire 10m store gpc0
frontend fe_https
http-request track-sc1 src table st_abusers
http-request sc-inc-gpc0(1) if { sc_http_req_rate(0) gt 100 }
http-request tarpit deny_status 429 if { sc_get_gpc0(1) gt 0 }
defaults
timeout tarpit 5s
Sync Tables Across an Active-Active Pair with a Peers Section
Each HAProxy keeps its own table, so an attacker behind a DNS round robin gets double the budget. A peers section replicates entries between nodes. The table must be declared in a backend with the peers keyword, and the peer names must match each node hostname or the -L name.
peers lbpair
peer lb1 10.0.1.11:10000
peer lb2 10.0.1.12:10000
backend st_rate
stick-table type ip size 1m expire 10s store http_req_rate(10s),conn_cur peers lbpair
frontend fe_https
http-request track-sc0 src table st_rate
Inspect, Clear, and Fix Client IP Behind a CDN
The runtime socket shows live counters and lets me unblock a customer without a reload. The CDN problem is separate: behind a CDN, src is the edge address, so every user shares one bucket. I either accept the PROXY protocol from a trusted load balancer, or read X-Forwarded-For only when the connection comes from a known CDN range, taking the last entry that the CDN appended.
echo "show table st_rate" | socat stdio /run/haproxy/admin.sock
# 0x7f..: key=203.0.113.9 use=1 exp=8200 http_req_rate(10000)=137 conn_cur=3
echo "clear table st_rate key 203.0.113.9" | socat stdio /run/haproxy/admin.sock
echo "clear table st_abusers" | socat stdio /run/haproxy/admin.sock
# Behind a CDN: trust the header only from CDN ranges
acl from_cdn src -f /etc/haproxy/cdn-ranges.lst
http-request set-var(txn.cip) req.hdr_ip(x-forwarded-for,-1) if from_cdn
http-request set-var(txn.cip) src if !from_cdn
http-request track-sc0 var(txn.cip) table st_rate
# Or from an upstream L4 balancer:
# bind :443 ssl crt /etc/haproxy/certs/site.pem accept-proxy
- I track each client IP in a stick-table that stores a 10 second request rate and the current connection count.
- A deny rule returns 429 when the rate passes 100, and repeat offenders move to a ten minute abusers table or get tarpitted.
- A peers section replicates the tables so both active-active nodes enforce one shared limit.
- Behind a CDN I take the client IP from PROXY protocol or from X-Forwarded-For, trusted only from CDN source ranges.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Using src behind a CDN without changing the tracked key Every visitor shares the CDN edge address, so one noisy user trips the limit for everyone, or the limit never trips because traffic spreads across many edge nodes.
- Trusting X-Forwarded-For from any client Attackers can send their own header and rotate the value each request, which gives them an unlimited supply of fresh counters.
- Skipping the peers section on an active-active pair Each node counts alone, so the effective limit doubles, and after a failover the standby has no memory of who was abusive.
- Setting a huge expire value on the rate table Idle entries stay in memory and the table fills with stale keys, which evicts active entries and makes counts unreliable.