API Rate Limiter 00:00:01 One caller asks for the last token A partner API needs room for short bursts and a bound on sustained demand. Put a token bucket in front of the expensive operation. The units are requests for one identified customer in this exercise. It carries an API key or a signed token. we need to know whose bucket to use. Tokens and time of last refill. A caller never seen before starts with a full bucket. Allowed, 74 left, no wait. With an empty bucket the gateway answers 429 and a wait. The backend is never called. Cap tokens at the bucket's capacity, use a reliable time basis and make spending a decision with one owner. Otherwise a burst of concurrent callers can spend the same credit more than once. 00:00:49 Estimate a rolling minute Fixed-minute counters have a sharp boundary. A sliding-window counter blends the current interval with an estimate of the part of the preceding interval still relevant to the latest minute. It is 24 seconds past the minute. 80 requests. A new caller has none. Twenty-four seconds have elapsed in the current minute. Sixty percent of the previous minute remains relevant; eighty times point six plus forty-five gives an estimate of ninety-three. After admitting this request the estimate rises to ninety-four against the sample limit of one hundred. That weighting assumes traffic was spread through the old interval. A concentrated burst can make the estimate inaccurate. Decide how much error the customer's policy can tolerate. 00:01:33 Put two gateways against one remaining credit The load balancer sends two requests for the same caller to different gateways. Local independent counters would both see an allowance. A shared atomic store operation can serialize the spend. The load balancer sends them to different gateways. Two scripts wait for the same primary. Two tokens become one. The first script takes it. The second sees none and refuses with a wait. Test the case with exactly one token remaining. One caller proceeds, while the other gets a bounded retry suggestion. The invariant belongs at the update, not at an earlier network read. 00:02:09 Lease a few tokens to a busy gateway A very hot caller makes a network round trip for every admission. Grant a small batch of credits to a gateway, then let it consume those already-issued tokens locally. The first request of this caller at gateway 1. Nine remain in memory. The next nine requests make no network call. After three seconds unused tokens are thrown away. The caller loses a little; the limit holds. Local leasing can strand unused credits at a failed gateway and reduce available burst flexibility. Bound lease lifetime, prevent reissuing outstanding credits, and measure the resulting fairness. 00:02:46 What if the counting shard stops answering? For a public read, a bounded local fallback may keep useful traffic moving. For an operation whose strict limit protects a critical resource, admitting without evidence can be the wrong choice. After 2 ms the gateway stops waiting. The gateway stops calling this shard. 99 other shards work as before. Login answers 503 for these callers instead. A few trial calls succeed and the circuit closes. Counts may be a moment old. Choose failure behavior per operation and measure the fallback's budget. Shard the counting service so one backend timeout need not remove rate control from the whole API.