Overview
We have implemented a configurable rate limiting system to protect the API from:- DDoS Attacks: Using a global rate limiter.
- Brute Force Attacks: Using a stricter rate limiter on authentication endpoints.
Configuration
Rate limits are configured inpkg/config/config.go and can be overridden via environment variables.
Implementation Details
1. Global Rate Limiter
Applied to all routes viae.Use() in main.go.
- Purpose: Prevent a single IP from flooding the server.
- Limit: Moderate (20 RPS / 50 Burst).
- Identifier: Real IP Address.
2. Auth Rate Limiter
No code uses this limiter now.
RATE_LIMIT_AUTH_RPS and RATE_LIMIT_AUTH_BURST are still
loaded into cfg.RateLimit.AuthRPS / AuthBurst, but nothing reads them, and sign-in is
handled by Bun’s Better Auth, which has its own limits. Only the global and per-route
limiters below are active./api/v1/auth/* routes (/login, /register).
- Purpose: Prevent credential stuffing and brute force attacks.
- Limit: Strict (2 RPS / 5 Burst).
- Identifier: Real IP Address.
Middleware
Implementation located in:internal/api/http/middleware/rate_limit_middleware.go.
RateLimitMiddleware(redis, name, rps, burst) builds an IPRateLimiterStore: a token
bucket in Redis (rate_limit:ip:<name>:<ip>, one Lua script using Redis’s clock, see
internal/shared/infrastructure/cache/ip_rate_limit.go), so every machine and replica
shares one count. Each Redis call has a 150 ms timeout. On a Redis error the store counts
in per-process memory (Echo’s memory store, same rate and burst), skips Redis for 5 s, and
logs a warning at most once a minute. It never returns an error, because Echo would turn
that into a 429.
The IP is c.RealIP(): X-Gremlin-Client-IP when a Cloudflare Worker sends it with the
right CLIENT_IP_PROXY_SECRET, otherwise CLIENT_IP_HEADER (Fly-Client-IP) (ADR-0073).
Which Redis each deployment uses is described in
Redis Infrastructure.
Response Headers
When a request is rate limited, the server responds with:- Status Code:
429 Too Many Requests - Body:
{"error": "Too Many Requests"}