Rate‑Limiting with Traefik: A Practical Guide for Protecting Sensitive Endpoints
Traefik’s Rate Limiting Middleware lets you protect critical endpoints like /login with a declarative, edge‑side solution. Learn how to configure, validate, and fine‑tune limits in Docker‑Compose or Kubernetes, and understand the trade‑offs and practical steps for real‑world deployments.
30 May 2026, 10:33 UTC

Why Rate Limiting Matters in a Micro‑Service Stack
Micro‑service deployments often expose dozens of HTTP endpoints to the public internet. When an attacker or a misbehaving client floods an endpoint, the downstream service can exhaust CPU, memory, or database connections. Traditional firewalls can’t guard against application‑level abuse, and adding custom logic to every service is error‑prone. Traefik’s built‑in Rate Limiting Middleware gives teams a declarative, edge‑side solution that requires no code changes.
Traefik’s Rate‑Limiting Middleware in a Nutshell
The middleware implements a token‑bucket algorithm. For each matched route or host you configure:
- average – the average number of requests allowed per period.
- burst – the maximum number of requests that can exceed the average in a short window.
- period – the time window (e.g., 60s) over which the average is calculated.
429 Too Many Requests. The middleware is stateless across nodes, so it works out of the box in a horizontally scaled Traefik cluster.
Configuring Rate Limiting in Docker‑Compose
Below is a minimal example that protects the /login endpoint. The configuration is split into two parts: the IngressRoute that matches traffic and the Middleware that enforces the limit.
# docker-compose.yml snippet
services:
traefik:
image: traefik:v2.6
command:
- --providers.docker
- --entrypoints.web.address=:80
ports:
- "80:80"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
login-service:
image: myapp/login:latest
labels:
- "traefik.enable=true"
- "traefik.http.routers.login.rule=PathPrefix(`/login` )"
- "traefik.http.routers.login.entrypoints=web"
- "traefik.http.routers.login.middlewares=login-rate-limit"
- "traefik.http.middlewares.login-rate-limit.rateLimit.average=5"
- "traefik.http.middlewares.login-rate-limit.rateLimit.period=60s"
- "traefik.http.middlewares.login-rate-limit.rateLimit.burst=2"
Run docker-compose up -d and the middleware is active immediately. No code changes are required in login-service.
Configuring Rate Limiting in Kubernetes
When using Traefik as an Ingress controller, the same logic applies but the configuration lives in Kubernetes CRDs.
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: login
spec:
entryPoints:
- web
routes:
- match: PathPrefix(`/login`)
kind: Rule
services:
- name: login-service
port: 80
middlewares:
- name: login-rate-limit
middlewares:
- name: login-rate-limit
rateLimit:
average: 5
period: 60s
burst: 2
Apply the manifest with kubectl apply -f login-route.yaml. Traefik will automatically pick up the new middleware.
Validating the Middleware in Production
To confirm the middleware is working, perform the following steps:
- Send six rapid requests to
/loginusingcurl -s -o /dev/null -w "%{http_code}\n" http://your‑ingress/loginorab -n 6 -c 6 http://your‑ingress/login/. - Verify that the first five requests return
200and the sixth returns429. - Check Traefik’s Prometheus metrics. A metric like
traefik_http_middleware_rate_limit_hits_totalwill increment for each request;traefik_http_middleware_rate_limit_rejections_totalshould increase for the 429s. - Look at the Traefik dashboard (
/dashboard/) – the middleware will appear under the route’s details with hit counts. - Review
traefik.log. When a request is rejected, a log line containingrateLimitand429appears.
These checks give you confidence that the limit is enforced and that legitimate traffic is still served.
Trade‑Offs and Practical Limitations
- Per‑Request Overhead – The middleware adds a small latency (microseconds) to each request. In a high‑throughput environment this can accumulate, but it is negligible compared to the cost of a DDoS attack.
- Threshold Sensitivity – Setting
averageorbursttoo low can trigger 429s for normal users, especially if they use mobile data or intermittent connectivity. Start conservative and increase gradually. - Version Requirement – The rate‑limit middleware was introduced in Traefik v2.4. Deploying an older instance will silently ignore the configuration, leaving the endpoint unprotected.
- Statelessness vs. Global Limits – Token buckets are local to each Traefik node. In a multi‑node cluster, the limit is effectively multiplied by the number of nodes unless you enable the
redisormemcachedstore for shared state (a more advanced setup).
Fine‑Tuning for Real‑World Traffic
Start with a baseline of average=5 and burst=2 for a login endpoint. Monitor 429s over a 24‑hour period. If you see spikes during peak hours, increase average to 10. If legitimate users are still being throttled, raise burst to 4. Use Traefik’s Prometheus metrics to plot hit and rejection rates against time of day.
For endpoints that are less critical, you can set a higher average or disable rate limiting entirely. Traefik’s declarative approach makes it easy to toggle the middleware on or off by editing the YAML and re‑deploying.
Actionable Takeaway
Rate limiting is a first‑line defense against abusive traffic. By attaching Traefik’s rateLimit middleware to sensitive routes, you gain:
- No code changes in your services.
- Centralized, observable limits via Prometheus and the dashboard.
- Fine‑grained control per host or path.
Deploy the middleware, monitor 429 responses, iterate on average, period, and burst, and you’ll protect your services while keeping the user experience smooth.
Diagram
| RateLimit Middleware | Traefik Edge | Backend Service | Client |
|---|---|---|---|
| Token‑bucket logic | Ingress traffic | Handles request | Sends request |
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.