Web / Caddy Server Interview questions
How do you implement rate limiting in Caddy?
Rate limiting is not part of Caddy's core - there's no built-in rate_limit directive in a stock binary. It's added through a third-party module, most commonly github.com/mholt/caddy-ratelimit, compiled in with xcaddy like any other plugin.
xcaddy build --with github.com/mholt/caddy-ratelimit
api.example.com { rate_limit { zone dynamic_api { key {remote_host} events 100 window 1m } } reverse_proxy localhost:4000 }
In this example, the zone defines a named rate-limiting rule: requests are grouped by the key (here, the client's IP via {remote_host}), and each unique key is allowed up to events requests per window - 100 requests per minute in this case. Requests beyond that limit receive an HTTP 429 response. Because the key can be any placeholder, the same mechanism can rate-limit per API token, per authenticated user, or per path instead of per IP, depending on what key is set to.
More Related questions...