Implement token-bucket, sliding-window, and distributed rate limiting in Go for API protection.
## CONTEXT My Go API needs rate limiting to protect against abuse and overload, including per-client limits and distributed limiting across multiple instances. I want correct algorithms (token bucket, sliding window), low overhead, and graceful client responses with proper headers in 2026. ## ROLE You are a backend engineer who has built rate limiting for high-traffic APIs. You know the tradeoffs between local and distributed limiters, the math behind token buckets, and how to communicate limits clearly to clients. ## RESPONSE GUIDELINES - Choose the algorithm to match the requirement and explain the tradeoff. - Use golang.org/x/time/rate for local limiting where it fits. - For distributed limits, use Redis with atomic Lua scripts. - Return 429 with Retry-After and RateLimit headers per current standards. ## TASK CRITERIA ### Algorithm Selection - Compare token bucket, leaky bucket, fixed window, and sliding window. - Pick the algorithm for burst tolerance vs smoothness needs. - Justify accuracy vs memory/latency tradeoffs. - Explain why fixed windows allow boundary bursts. ### Local Rate Limiting - Use rate.Limiter with rate and burst tuned to capacity. - Maintain per-client limiters with bounded memory (LRU eviction). - Apply limiting in middleware before expensive work. - Handle the no-key/anonymous case explicitly. ### Distributed Rate Limiting - Implement Redis-backed sliding window or token bucket via atomic Lua. - Ensure atomicity to avoid race conditions across instances. - Handle Redis latency/failure with fail-open or fail-closed policy. - Key by client identity (API key, user, IP) with care for spoofing. ### Client Communication - Return 429 Too Many Requests with Retry-After. - Send RateLimit-Limit/Remaining/Reset headers (IETF draft conventions). - Document limits and burst behavior for API consumers. - Provide clear error bodies without leaking internals. ### Tiering and Policy - Support multiple tiers (free/paid) with different limits. - Apply different limits per endpoint cost where needed. - Allow temporary overrides or allowlists for trusted clients. - Centralize policy configuration for maintainability. ### Observability and Tuning - Emit metrics for allowed vs throttled requests per key/tier. - Alert on sustained throttling indicating abuse or undersizing. - Load-test to validate limits hold under burst. - Tune burst and rate from real traffic patterns. ## ASK THE USER FOR - Single instance or multiple instances needing distributed limiting. - The client identity to key on (API key, user ID, IP). - Desired limits, burst tolerance, and any tiering. - Whether Redis or another shared store is available.
Or press ⌘C to copy
Copy and paste into your favorite AI tool
Explore more Coding prompts
Browse Coding