How do I rate limit outbound calls to a third-party API in Go without breaking a bursty worker fleet?
Question
We run a payment-integration service written in Go: a pool of workers pulls jobs off a queue and calls a payment provider's API. The provider caps us at 50 requests per second and returns 429 the moment we go over. The trouble is that the workers are bursty; when a spike hits the queue, dozens of goroutines hammer the provider at once and we blow the limit. I want to pin outbound requests to the 50 req/s ceiling, but without dropping queued jobs — they can be delayed, just not lost. What's the right way to build this in Go?
Answer
Short answer
If you’re in a single process, use golang.org/x/time/rate with one token bucket shared by every worker, and block with Wait(ctx) instead of dropping.
Context
If your fleet spans multiple machines/pods, an in-process limiter won’t help — you’ll need a Redis-backed shared limiter.
Why
-
One shared limiter, not one per worker. The classic mistake is giving each worker its own limit; then N workers × the per-worker limit multiplies the fleet’s total rate past the ceiling. The provider only sees the sum of the whole fleet, so create a single
*rate.Limiteronce and share the same instance across all goroutines.rate.Limiteris already goroutine-safe. -
Wait, notAllow. Since you want “delay, don’t drop,” the right call islimiter.Wait(ctx). With no token available it blocks and applies backpressure; the job stays queued rather than being lost.Allow()returnsfalseand drops the request when there’s no token — not what you want. If a job is time-sensitive, put a deadline onctx; when it expiresWaiterrors and you let the job retry.
What to do
-
Set burst modestly. In
NewLimiter(limit, burst), burst governs the initial spike. On a cold fleet a burst of 50 can fire all 50 in the first 100 ms; since the provider’s cap is usually a sliding window, even that trips a 429. Keep burst at or just below the limit. -
Leave headroom and treat 429/Retry-After as authoritative. A client-side counter is optimistic; between clock skew and rounding, target ~45 rather than 50. The real source of truth is the provider’s counter: when a 429 comes back, honor the
Retry-Afterheader and wait that long. The client-side limiter is your first line of defense; 429 has the last word.// ONE limiter shared by every worker goroutine lim := rate.NewLimiter(rate.Limit(45), 45) // ~10% headroom under the 50 cap func callProvider(ctx context.Context, req *http.Request) (*http.Response, error) { if err := lim.Wait(ctx); err != nil { // blocks, doesn't drop return nil, err } return httpClient.Do(req) } -
Distributed case: Redis. If you run the service as several horizontally-scaled pods, each pod’s in-process limiter multiplies the ceiling by N. Then you need a shared token bucket in Redis (GCRA via a Lua script, or plain INCR + EXPIRE over a window). You pay a network hop per call, but that’s the cost of a truly global limit.
Bottom line: personally, in a single process I’d use a shared x/time/rate limiter with Wait(ctx), target ~10% under the cap, and always honor 429 + Retry-After. If the work scales horizontally I’d move the same logic into a Redis-backed bucket. That way the burst isn’t dropped, just smoothed out across the provider’s capacity.
Compiled from questions I have run into in the field or been asked.
Related Reading
Comments
Sign in with your GitHub account to join the discussion. Comments are stored in GitHub Discussions.