Rate limiting strategies

Flask-Limiter delegates the implementation of rate limiting strategies to the limits library.

The strategy can be selected by setting the flask_limiter.Limiter.strategy constructor argument or the RATELIMIT_STRATEGY config.

Note

For more details about the implementation of each strategy refer to the limits documentation for Rate Limiting Strategies.

Fixed Window

This strategy is the most memory‑efficient because it uses a single counter per resource and rate limit. When the first request arrives, a window is started for a fixed duration (e.g., for a rate limit of 10 requests per minute the window expires in 60 seconds from the first request). All requests in that window increment the counter and when the window expires, the counter resets

See the Fixed Window documentation in the limits library for more details.

To select this strategy, set flask_limiter.Limiter.strategy or RATELIMIT_STRATEGY to fixed-window

Moving Window

This strategy adds each request’s timestamp to a log if the nth oldest entry (where n is the limit) is either not present or is older than the duration of the window (for example with a rate limit of 10 requests per minute if there are either less than 10 entries or the 10th oldest entry is atleast 60 seconds old). Upon adding a new entry to the log “expired” entries are truncated.

See the Moving Window documentation in the limits library for more details.

To select this strategy, set flask_limiter.Limiter.strategy or RATELIMIT_STRATEGY to moving-window

Sliding Window

This strategy approximates the moving window while using less memory by maintaining two counters:

  • Current bucket: counts requests in the ongoing period.

  • Previous bucket: counts requests in the immediately preceding period.

A weighted sum of these counters is computed based on the elapsed time in the current bucket.

See the Sliding Window Counter documentation in the limits library for more details.

To select this strategy, set flask_limiter.Limiter.strategy or RATELIMIT_STRATEGY to sliding-window-counter