Skip to main content
hard

Cloudflare Rate Limiter

Also known as: Rate Limiter Models

Cloudflare Rate Limiter (Part 1: Fixed Window) RateLimiter(max_requests, window_seconds) - allow(timestamp) -> bool

A rate limiter is a clock plus a counter. I build fixed window, sliding window and token bucket, and say what each lets through.

Examples

You write

RateLimiter: allow(timestamp: int)

  1. Example 1

    Input
    RateLimiter(2,10): allow(1), allow(2), allow(9), allow(10), allow(10)
    returns
    ["true","true","false","true","true"]
  2. Example 2

    Input
    RateLimiter(1,5): allow(0), allow(4), allow(5), allow(9), allow(10)
    returns
    ["true","false","true","false","true"]

The pattern behind it

Design

I reach for it when

  • The statement describes a class with methods, not one function.
  • Each method needs to be fast, and the structure decides how fast.

Say this before you type. Say each method, the cost you want from it, and the structure that earns that cost.

All 27 Design problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Fixed Windowmedium
2Part 2
3Part 3
4Part 4

Each locked part opens in the editor once the part before it passes.