Skip to content

Rate Limit over Sliding Window not working? #1224

@MysticRyuujin

Description

@MysticRyuujin

Detailed description of the problem

I've followed numerous tutorials about how to implement a "sliding window" rate limit but I can't seem to make any of them work.

I'm trying to limit users of an API to "X requests per second" over a sliding window of time, I don't care how many times they call the API, so long as they don't go over X requests per second. However, I can only seem to get it to work over some max number of calls, regardless of the rate at which they make the calls.

I've tried a few configs, Example:

  stick-table type ip size 1m expire 10s store gpc0,http_req_rate(10s)
  tcp-request content track-sc1 src
  acl is_rate_abuse src_http_req_rate gt 200
  http-request deny deny_status 429 if is_rate_abuse

With this config, if I ran requests at 1 RPS, as soon as it hit 200 it throws a 429, even if those 200 requests took 200 seconds...
image

Using Locust, I set it to run 1 user, sending a request every 1 second. As soon as it hit 200 total requests, it started failing. This, to me, makes no sense given that this is suppose to be the request rate over 10 seconds, not over the life of address in the stick-table?

And here's me doing it with 5 "users" at 1 RPS (so 5 RPC total)
image

I will fully admit that the above code may be incorrect, however, I've tried every single example I could find online to make this work. Some of them are vastly different configs but they all come down to the same basic principle of tracking http_req_rate() and src_http_req_rate and nothing I've done has made it so that X/req/sec works. As soon as I hit the number defined in src_http_req_rate it starts sending 429s.

Expected behavior

The http_req_rate should be the rate over a 10 second window, and the src_http_req_rate should be the request rate over that same window?

Steps to reproduce the behavior

  1. Run HAProxy with a sliding window rate limiter
  2. Test that rate limiter?
  3. Profit

Do you have any idea what may have caused this?

No

Do you have an idea how to solve the issue?

No

What is your configuration?

frontend http-in
  bind *:80
  # Rate Limit
  stick-table type ip size 1m expire 10s store gpc0,http_req_rate(10s)
  tcp-request content track-sc1 src
  acl is_rate_abuse src_http_req_rate gt 200
  http-request deny deny_status 429 if is_rate_abuse

Output of haproxy -vv and uname -a

I'm running this in Docker but it's

haproxy version is 2.2.11-1

If HAProxy crashed: Last outputs and backtraces

(paste your output here)

Additional information (if helpful)

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: duplicateThis issue or pull request already exists.type: bugThis issue describes a bug.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions