Respecting Boundaries: Precise Rate Limiting in Go
Traffic spikes are a double-edged sword. On one hand, you’re busy! On the other, those spikes can overwhelm your services or exceed your downstream quotas. Whether you're protecting your own databa...

Source: DEV Community
Traffic spikes are a double-edged sword. On one hand, you’re busy! On the other, those spikes can overwhelm your services or exceed your downstream quotas. Whether you're protecting your own database from an unexpected burst or respecting a third-party API’s strict 100 requests-per-second (RPS) limit, you need a precise way to shape your traffic. Enter the Token Bucket Rate Limiter in Resile. The Problem: Unbounded Traffic In a distributed environment, your clients don't know about each other. If 50 different microservice instances all decide to call a downstream API at the same time, the aggregate traffic can easily exceed the capacity of the target system. When you exceed these limits, you'll often see: HTTP 429 (Too Many Requests): Downstream services start rejecting you. Cascading Latency: The target system slows down for everyone because it's processing too many requests at once. Cost Overruns: Many cloud providers and SaaS APIs charge significant premiums for exceeding agreed-upo