JavaScript · Advanced · Ticket 2Ticket #38 · from Hannah Cole, Senior Engineer
  1. Example
  2. Warm-up 1
  3. Warm-up 2
  4. Ticket
Brief
NEW TICKET#38 · Public API

Sliding-window rate limit

Hannah Cole · Senior Engineer
Opened today

“Someone's script is hammering our invoices API. We'll allow at most limit requests in any windowMs milliseconds, per API key. Use a sliding window: when a request arrives at time t, count the allowed requests from the last windowMs (later than t − windowMs). Under the limit, it's allowed; otherwise it's refused. A refused request doesn't count against later ones, or a client who keeps retrying would be locked out for good. Can you write allowed?”

YOUR PLAN
  1. 1Watch an example2 min
  2. 2Warm-up 11 min
  3. 3Warm-up 21 min
  4. 4Fix the ticket26 min
BY THE END YOU CAN
  • Keep a list of what has been let through
  • Count only the recent items with a cutoff
  • See why fixed buckets let bursts through at the edges
About 30 minutes
Four short steps. Nothing is timed, and you can leave any time.