Distributed rate limiter

A rate limiting service in Go that runs as one binary, speaks HTTP and gRPC, and keeps limiting sensibly even when its own dependencies fail.

The problem

Rate limiting looks simple until it is shared. Many service nodes have to agree on one limit per tenant and resource, rules change while traffic flows, and the store that holds the counts can fail. A limiter that fails open lets abuse through; one that fails closed takes the product down with it.

This service is a complete reference implementation of that problem, built to run on its own and integrate with any system.

Clients call stateless rate limit nodes over HTTP or gRPC. Nodes keep limiter state in Redis and fall back to a local, ownership-aware limiter when Redis is unhealthy. Rules flow from a database through an outbox and pub/sub into an atomic rule cache on each node.ClientsHTTP or gRPCService node 1statelessService node 2statelessService node 3statelessShared limiter statein Redis: buckets and windowsLocal fallbackownership awareif Redis failsRulesRules DBOutboxPub/subRule cache, per node
Stateless nodes share limiter state, fall back locally when it fails, and receive rules from a control plane.

What it does

  • Stateless nodes. Any node can answer any request; limiter state is shared.
  • Several algorithms per rule: token bucket, fixed window and sliding window.
  • Hot rule updates. Rules are stored in a database, published through an outbox and propagated by pub/sub invalidation, with a full sync worker as a safety net. No restarts.
  • Lock-free reads. Each node keeps rules in atomic snapshots, so the hot path never waits on a lock.
  • Batch evaluation for high-throughput clients, and identical semantics over HTTP and gRPC.

Failing gracefully

When the shared store is unhealthy, nodes fall back to a local limiter. The fallback is ownership aware and scaled by region, so a cluster of nodes does not quietly multiply the limit by the number of nodes. A degrade controller moves the system between normal, degraded and emergency modes based on health signals, and exposes which mode it is in.

Operating it

The service ships with metrics, tracing, health and readiness endpoints, configuration by file, environment or flags, benchmarks, and a chaos testing harness. It runs as a single binary, in Docker, or with a local Compose setup.