0
0
Expressframework~10 mins

Rate limiting with express-rate-limit - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Rate limiting with express-rate-limit
Client sends request
express-rate-limit middleware
Check request count for IP
Send 429
When a client sends a request, the middleware checks how many requests that IP made. If the limit is exceeded, it sends a 429 error. Otherwise, it lets the request continue.
Execution Sample
Express
import rateLimit from 'express-rate-limit';

const limiter = rateLimit({
  windowMs: 60000,
  max: 3
});

app.use(limiter);
This code limits each IP to 3 requests per 60 seconds using express-rate-limit middleware.
Execution Table
StepRequest NumberIP Request CountCondition (count > max?)ActionResponse
1111 > 3? NoAllow request200 OK
2222 > 3? NoAllow request200 OK
3333 > 3? NoAllow request200 OK
4444 > 3? YesBlock request429 Too Many Requests
💡 At step 4, the request count exceeds max (4 > 3), so the request is blocked with 429.
Variable Tracker
VariableStartAfter 1After 2After 3After 4
IP Request Count01234
Key Moments - 2 Insights
Why does the 4th request get blocked even though the max is 3?
Because the middleware counts requests starting from 1, so when the count reaches 4, it exceeds the max of 3, triggering the block as shown in execution_table step 4.
Does the rate limit reset automatically?
Yes, after the windowMs time (60 seconds here), the count resets to 0, allowing new requests. This is implied by the windowMs setting in the code sample.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the IP Request Count at step 3?
A4
B2
C3
D1
💡 Hint
Check the 'IP Request Count' column at step 3 in the execution_table.
At which request number does the condition become true and block the request?
A4
B3
C2
D1
💡 Hint
Look at the 'Condition' column in execution_table where it changes from No to Yes.
If max was changed to 5, what would happen at request number 4?
ARequest 4 would be blocked
BRequest 4 would be allowed
CRequest 4 would cause an error
DRequest 4 would reset the count
💡 Hint
Compare the 'max' value in the code sample and the condition logic in execution_table.
Concept Snapshot
express-rate-limit middleware limits requests per IP.
Set windowMs (time window) and max (max requests).
Middleware counts requests per IP.
If count > max, sends 429 error.
Otherwise, request proceeds normally.
Full Transcript
Rate limiting with express-rate-limit works by counting how many requests a client IP makes within a set time window. The middleware checks this count on each request. If the count exceeds the max allowed, it blocks the request with a 429 Too Many Requests response. Otherwise, it lets the request continue. The count resets after the time window expires. This helps protect servers from too many requests from one client.