Rate Limiting 00:00:01 Spend a bucket of tokens A client is allowed a burst of a hundred requests. It sends them all immediately, then keeps sending more. What would allow a short burst while still imposing a limit on the rate it can sustain? The client starts with a burst of 100 tokens. 15 tokens are taken: 100 becomes 85. 85 becomes 70. The client is quiet for 1.5 seconds. 15 tokens come back: 40. Requests 6 and 7 pass: 25, then 10. Request 8 needs 15 and only 10 are there. The wait is 5 divided by 10 equals 0.5 s. Retry-After counts whole seconds, so it says 1. Capacity sets the largest stored burst, while refill sets the longer-run rate. Time idle cannot accumulate more than the bucket's capacity under this design. 00:00:55 One request can meet several limits Free plan: 3 calls a minute, 10,000 tokens a minute, 1 dollar a day. It needs about 4,000 tokens. One of three calls is used. Good. 5,000 plus 4,000 is 9,000. Good. 40% is spent. Good. Only now are all three reduced, in one step. Another 4,000 tokens would make 13,000. Refused, and the call count stays at 2. A per-user, per-tenant and global limit protect different resources. Specify whether a refused request consumes any of the other budgets and make the decision consistently. 00:01:32 Explain a rate-limited response What a polite client does with the answer. Even a normal answer says how much is left: 1. The client uses it. Nothing is left. Another call is refused with a waiting time of 20 seconds. All workers with this key wait 20 seconds plus a little jitter. The window has renewed and the call passes. 59 calls in the next 59 seconds: one a second from here on. A retry hint can help a cooperative caller avoid immediately repeating the request. A client should allow for jitter and a deadline because the suggested time does not reserve future capacity. 00:02:08 Keep the highest-value work alive All plans get full service. Free is past 70%, so it gets a smaller model. Pro starts at 85%. Free gets cached answers only. Pro is reduced. Enterprise is full until 95%. Pro is past 95% and gets cached answers. Enterprise is reduced. Everyone is refused with 429 and a waiting time. Degrade or reject lower-priority work before expensive processing when the contract allows it. Returning a fabricated success for a write that was never accepted would merely hide the overload. 00:02:44 Price a scarce dependency Calls differ in price, so reserve money before each call. The estimate for this call is 8% of the day's budget. It is reserved first. The real price was 5%. The difference goes back. At 50, 75 and 90% the owner gets a message. The next estimate would pass 100%. No call is made and no money is spent. Limit by cost where requests use very different amounts of downstream work. A request-count allowance alone lets one expensive operation consume the resource intended for many cheap ones. 00:03:18 A background job must share capacity 600 calls with a limit of 60 a minute. The script sends 600 at once. 60 pass and 540 are refused. One call a second: 600 seconds, which is 10 minutes, and no refusals. At 80% of the limit, 48 a minute, it takes 12.5 minutes and people can still use the key. A nightly batch should have a bounded share and respond to throttling. Watching accepted, rejected and successfully completed work distinguishes effective protection from customers simply giving up. Express how much work can arrive at once, how quickly the allowance replenishes, and who shares that allowance. Those choices determine which caller actually feels a refusal.