You must enforce a global limit of 1,000 requests a second per customer across 10 gateway nodes, and a shared counter store on every request is too slow. What are your options?
Sketch your own answer first, then compare it with the one below.
A strong answer
The simplest local scheme gives each node a tenth, 100 a second. It needs no coordination, and it is wrong whenever the load balancer is uneven. If a customer's traffic lands 300 a second on one node, that node rejects 200 while the other nine sit under their shares. The customer sees rejections at 300 requests a second even though they are far below 1,000.
The next step is to let nodes borrow. Each node holds a local allowance and asks a coordinator for a larger slice when it runs low, in chunks, say 50 tokens at a time. The coordinator hands out the global budget, and the store is hit once per chunk instead of once per request. The limit becomes approximate, off by up to the tokens held locally and unspent. With 10 nodes holding 50 each, the overshoot is bounded by 500 in the worst case, so keep chunks small relative to the limit.
A third option is to make the balancing exact by routing each customer to one node by hashing their key. The counter is local and exact. The price is that a node failure or rebalance resets or splits counts, and a very large customer can overload their node.
Whichever you choose, state the error you accept and what happens when the coordinator is unreachable. Falling back to the even split is a sensible degraded mode.
Sources
- Betsy Beyer, Chris Jones, Jennifer Petoff, Niall Richard Murphy (2016). Site Reliability Engineering, chapter 21, Handling Overload.