AlgoMaster Logo

Design Thread-Safe Rate Limiter

medium9 min readUpdated August 31, 2026
Listen to this chapter
Unlock Audio

Design a thread-safe fixed-window rate limiter. Each client may make at most limit allowed requests during one window.

The constructor receives a clock function that returns the current logical time in milliseconds. The judge controls this clock, so you should not read the system clock or create timer threads.

Windows are globally aligned. The current window is:

For example, with windowMillis = 10, times 0 through 9 belong to window 0, times 10 through 19 belong to window 1, and so on.

Implement allow(clientId). It returns true and consumes one permit when that client has used fewer than limit permits in the current window. Otherwise, it returns false. Every client has an independent counter.

Calls to allow may happen concurrently on the same limiter. The window lookup, optional reset, limit check, and increment must form one atomic operation.

The judge creates the limiter, supplies the clock, and starts all worker threads. Your implementation should contain only the rate-limiter state and synchronization.

The judge also preloads the standard concurrency, callback, and collection APIs for each supported language. You do not need to add import, include, or using statements.

Example 1:

Input:

Output:

Explanation: Client 7 consumes both permits in window 0. Time 10 begins window 1, so its counter resets.

Example 2:

Input:

Output:

Explanation: Each client receives its own permit. Requests from client 1 do not consume client 2's capacity.

Constraints

  • 1 <= limit <= 1_000
  • 1 <= windowMillis <= 1_000_000
  • 0 <= clientId <= 1_000_000
  • The logical clock is monotonic and returns a non-negative value.
  • At most 1_000 distinct clients are used.
  • At most 20_000 calls to allow are made.

Starter Code

Understanding the Problem

For every client, the limiter needs to remember:

  • The last window in which that client made a request.
  • The number of permits consumed in that window.

When allow observes a different window ID, the old count no longer matters and must be reset before checking the limit.

The complete read-modify-write sequence must be atomic. If two threads both read a count of limit - 1 before either increments it, both may return true, exceeding the configured limit. A window transition introduces a similar race: a delayed operation from an older window must not overwrite newer-window state.

Synchronization Strategy

Maintain:

  • The immutable limit and windowMillis values.
  • The supplied logical clock.
  • A hash map from client ID to (windowId, count).
  • One mutex protecting all map access and counter changes.

For allow(clientId):

  1. Acquire the mutex.
  2. Read the clock and calculate the current window ID.
  3. Find the client's state.
  4. If the client is new or its stored window differs, store the current window with count zero.
  5. If the count equals the limit, return false.
  6. Otherwise, increment the count and return true.

Holding the mutex across the clock observation, reset, check, and increment prevents both over-admission and stale-window overwrites.

Correctness

Every access to a client's window and count occurs while holding the same mutex, so each allow call has one atomic position relative to every other call.

When the stored window equals the current window, allow increments only if the count is below limit. Therefore, no client can receive more than limit permits in one window.

When the window changes, the operation resets that client's count before applying the same limit rule. Thus, every new aligned window provides exactly a fresh capacity of limit permits.

Each client ID maps to separate state. Updating one client's count cannot consume or reset another client's permits.

Code

Complexity Analysis

Let k be the number of distinct clients seen by the limiter.

  • allow: O(1) expected time.
  • Space complexity: O(k).