Rate Limiter
Two algorithms behind one interface. An atomic Lua script fixed a distributed increment/expiry race; quota leasing cut P99 from 1ms to 0.2ms.
- Built two rate-limiting algorithms (sliding-window counter, token bucket), each with in-memory and Redis-backed implementations behind one interface for config-level strategy swaps
- Fixed a distributed race between counter increment and expiry with a single atomic Redis Lua script, verified under 1,000 concurrent goroutines with Go’s race detector to stop windows silently doubling under load
- Added cost-weighted quota so a single call can consume multiple units — the token-cost billing model used by OpenAI and Stripe
- Designed a layered limiter enforcing per-user → per-tenant → global limits in sequence, short-circuiting at the first denial and returning the tightest remaining quota
- Built an adaptive limiter that tunes throughput with an AIMD control loop (as in TCP congestion control), using a lock-free atomic multiplier to avoid mutex contention on the hot path
- Cut Redis round trips ~85% with per-node quota leasing, dropping P99 decision latency from ~1ms to ~0.2ms; held zero 5xx across 28,440 requests at 300 rps while restarting Redis 4x mid-run
