0
0
Rest APIprogramming~10 mins

Fixed window algorithm in Rest API - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Fixed window algorithm
Start
Request arrives
Check current window timestamp
Is current time inside window?
NoReset window start time and count
Increment request count
Reject request
No
Allow request
End
This flow shows how each request is checked against a fixed time window and a request limit to decide if it is allowed or rejected.
Execution Sample
Rest API
window_start = 0
limit = 3
count = 0
current_time = 1
if current_time >= window_start + 10:
    window_start = current_time
    count = 0
count += 1
if count > limit:
    print('Reject')
else:
    print('Allow')
This code checks if a request fits in the current fixed window and allows or rejects it based on the count limit.
Execution Table
Stepcurrent_timewindow_startcount beforeCondition (current_time >= window_start + 10)Actioncount afterDecisionOutput
11001 >= 0 + 10 -> FalseNo reset1count=1 <= 3Allow
22012 >= 0 + 10 -> FalseNo reset2count=2 <= 3Allow
33023 >= 0 + 10 -> FalseNo reset3count=3 <= 3Allow
44034 >= 0 + 10 -> FalseNo reset4count=4 > 3Reject
5110411 >= 0 + 10 -> TrueReset window_start=11, count=01count=1 <= 3Allow
61211112 >= 11 + 10 -> FalseNo reset2count=2 <= 3Allow
72111221 >= 11 + 10 -> TrueReset window_start=21, count=01count=1 <= 3Allow
💡 Execution stops after processing all requests in the example.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7
current_time-1234111221
window_start00000111121
count01234121
Key Moments - 3 Insights
Why do we reset the window_start and count when current_time >= window_start + window_size?
Because the fixed window has ended, so we start a new window from the current time and reset the count to track requests in the new window. See execution_table row 5.
Why does the count increase even if the request is rejected?
The count increments before checking the limit to track all requests in the window. If count exceeds limit, the request is rejected. See execution_table row 4.
What happens if requests come after the window resets?
The window_start updates to the new time, count resets to zero, and counting starts fresh. Requests are allowed until the limit is reached again. See execution_table rows 5 and 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What is the count after incrementing?
A1
B4
C3
D0
💡 Hint
Check the 'count after' column in execution_table row 4.
At which step does the window_start reset to 11?
AStep 6
BStep 3
CStep 5
DStep 7
💡 Hint
Look at the 'Action' column in execution_table for when reset happens.
If the limit was increased to 5, what would be the decision at step 4?
AAllow
BReject
CReset window
DError
💡 Hint
Check the 'Decision' column and compare count to limit in execution_table row 4.
Concept Snapshot
Fixed window algorithm limits requests in fixed time windows.
Track window start time and count requests.
If current time exceeds window, reset count and start time.
Increment count for each request.
Reject if count exceeds limit.
Allows simple rate limiting per fixed interval.
Full Transcript
The fixed window algorithm controls how many requests are allowed in a fixed time period. When a request comes, the system checks if the current time is still inside the current window. If not, it resets the window start time and count. Then it increments the count for the new request. If the count goes over the allowed limit, the request is rejected. Otherwise, it is allowed. This process repeats for each request, ensuring no more than the limit number of requests happen in each fixed window period.