Design a rate limiter 00:00:00 Make a prediction You will spend one token atomically, compute refill from elapsed time, and keep one effective budget per key. A full bucket holds three tokens and no time passes. Four immediate requests arrive for the same key. How many should succeed? Three. Each accepted request spends one token. The fourth sees zero and is refused under a non-waiting policy. The check and spend must be one atomic operation for that key. 00:00:31 The dripping barrels at the mountain hut A hut's barrel gains one cup of water per second up to a painted line. Two walkers both seeing one cup cannot both take it. A second bucket for the same walker would incorrectly double the allowance. Capacity limits bursts; refill restores capacity over elapsed time. Synchronize accounting for the same identity and define how bucket creation and expiry preserve that identity. A rate limiter decides whether a caller may go on now. It counts for each key against one or more rules, and answers yes, or no with a time to wait. Every service has something that can be used up: a database, a phone line for texts, money. Without a limit one caller, by mistake or on purpose, takes it from all others. 00:01:20 Spend a token only once A bucket starts full with three tokens and refills one per second. Three immediate requests consume its capacity; a fourth is refused. Two seconds later, add at most two tokens according to elapsed time, capped at the bucket capacity. Use a monotonic clock for elapsed time. Checking and consuming must be one protected operation per bucket. Two threads should never both spend the same final token. At time zero, consume both tokens, then try a third request at the same instant. It must fail or wait. Advance the controlled clock by one refill interval and try again. Separating policy time from wall-clock calls makes this test reproduce the exact state transition. 00:02:07 Protect key lifecycle as well as counters Idle customers' buckets may be removed to limit memory. If one thread removes a bucket while another still uses it, a replacement bucket can give the same customer fresh capacity. Design lookup, use and eviction to preserve one effective budget per key. Pause a request holding the old bucket while the cleaner removes it and another request creates a new bucket. Both holders can now grant from different balances. Guard the key lifecycle as well as the numeric updates, or capacity can double without any individual counter becoming negative. If a wall clock moves backwards after a time adjustment, naive refill arithmetic can subtract tokens or grant too many later. Use a suitable monotonic elapsed-time source. A fixed-window counter is simpler when boundary bursts are acceptable. Discuss the allowed burst behaviour before choosing. Guava RateLimiter for Java. Gives out permits at a steady rate and makes callers wait. The cost of a large request is paid by the next one. Its source keeps the time of the next free ticket and works the stored permits out when asked. golang.org/x/time/rate. A token bucket with three ways in: allow or refuse, reserve a time, or wait. A reservation can be cancelled. 00:03:31 Try an interview follow-up When may you delete a key? When its meters are as good as new, because a missing key and a full one give the same answers. Not after a time without calls: a slow rule would be reborn full, and waiting for the cleaner becomes a free refill. An account's limit is reduced from ten tokens to three while it already holds seven. What balance should its next request start from? First define when the policy becomes effective. If the new cap applies immediately, settle refill to the change point and clamp the balance to three. Make policy updates and spending observe one consistent state so a stale caller cannot spend the old allowance. 00:04:16 What to take with you A key, a rule that all keys share, a meter for each key, an answer with a wait. The meter keeps credit and the time when it was true, and time does the rest. A token is worth one period, so all numbers are whole. Three things are one step under the lock of a stripe: ask and spend, look and create, decide and delete. Several rules are all asked before any is charged. A meter that is as good as new can be deleted, because a missing key gives the same answer.