Skip to content

Repository files navigation

Flex-Limiter

A configurable rate-limiting engine with pluggable algorithms (token bucket, leaky bucket, sliding window).

Start Redis:

docker-compose up -d limiterstore

Rate Limiting Formulas

Token Bucket

Gives some flexibility by allowing bursts when idle, while still limiting how fast requests can go on average.

tokens = min(capacity, tokens + (now - last_ts) * refill_rate)
allow request if tokens >= 1, then tokens -= 1

Leaky Bucket

Keeps traffic smooth and predictable by allowing requests at a steady speed and pushing back on bursts.

level = max(0, level - (now - last_ts) * leak_rate) + 1
allow request if level <= capacity

Sliding window counter(2-bucket approximation)

Limits requests by looking at how many came in over the last few seconds, so traffic is smoothed out instead of resetting suddenly at fixed time boundaries.

window_start = now - window_size
curr_bucket_index = floor(now / bucket_size)
effective_request_count = curr_bucket_count + (overlap / bucket_size) * prev_bucket_count
allow request if effective_request_count < requests_allowed

Test Scenarios

  • Normal requests: 5 requests within limit
  • Burst test: 10 rapid requests (5 succeed, 5 blocked)
  • Time-based recovery: Exhaust limit, wait, then request succeeds
  • User isolation: Limits applied independently per user

Test Outcome

BehaviorLeaky BucketToken BucketSliding Window Counter
Burst handlingBlocks immediatelyAllows short burstsSmoothly limits bursts
After waitingFew requests allowedMany requests allowedRequests allowed gradually
Traffic shapeSteady, flatSpiky after idleSmooth, time-distributed
Best useProtect backend systemsUser-facing APIsAccurate API rate limiting

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages