DrawCode Algorithm Visualizer • Rate Limiting • Easy

Fixed Window Counter

Tags: Rate Limiting, Counter, System Design, API Gateway, Simple

1. One-Liner

The Fixed Window Counter divides time into fixed-size windows (e.g., 1 minute) and limits how many requests are allowed per window. When the window changes, the counter resets.


2. The Problem It Solves

Rate limiting protects APIs from abuse. Fixed Window is one of the simplest approaches: count requests in the current time bucket. When the bucket (window) expires, start fresh.


3. The Core Idea

Imagine a whiteboard that gets wiped every minute:

  • You can write up to 5 tally marks per minute.
  • When the clock hits the next minute, someone erases the board.
  • You start counting from zero again.
  • That's Fixed Window: fixed time chunks, count per chunk, reset on boundary.


    4. How It Works

    StepWhat Happens
    1. Get timeGet current timestamp
    2. Calc windowwindow_id = now / window_size
    3. Check windowDid we cross into a new window?
    4. ResetIf new window, set count = 0
    5. Check limitIs count >= limit?
    6a. YES → DenyWindow full, reject request
    6b. NO → AllowIncrement count, allow request

    5. Key Properties

    PropertyValue
    Edge burst?Yes — 2x burst possible at window boundary
    MemoryO(1) — window_id + count
    SimplicityVery simple to implement
    AccuracyLower than sliding window

    Implementation Example (PYTHON)

    import time
    
    class FixedWindowCounter:
        def __init__(self, window_size, limit):
            self.window_size = window_size
            self.limit = limit
            self.current_window = 0
            self.count = 0
    
        def try_allow(self):
            now = time.monotonic()
            window_id = int(now // self.window_size)
            if window_id != self.current_window:
                self.current_window = window_id
                self.count = 0
            if self.count >= self.limit:
                return False   # DENY
            self.count += 1
            return True    # ALLOW

    Interactive Visualizer Workspace

    Explore step-by-step interactive animations, memory state tracking, and live multi-language execution in DrawCode.

    Launch Interactive Visualizer