Backend

A pragmatic guide to rate limiting

Glowing vertical bars rising and falling like a traffic chart

Rate limits protect your service from bursts, buggy clients and the occasional bad actor. The algorithm matters less than choosing the right key and returning a response clients can act on.

Pick the key first

  • Per API key for authenticated traffic
  • Per IP address only as a coarse outer layer
  • Per route for expensive endpoints like search or exports

Token bucket in a few lines

limiter := rate.NewLimiter(rate.Limit(10), 20) // 10 per second, bursts of 20

func handler(w http.ResponseWriter, r *http.Request) {
    if !limiter.Allow() {
        w.Header().Set("Retry-After", "1")
        http.Error(w, "too many requests", http.StatusTooManyRequests)
        return
    }
    // ...
}

Always send 429 Too Many Requests with a Retry-After header. Well-behaved clients will back off on their own, and the rest will show up clearly in your metrics.

A neon green padlock between rows of thin lines
Limits are a security control as much as a reliability one.

In a fleet of servers, a per-process limiter allows N times the intended rate. Share state, or divide the limit by the number of instances.