0

Hi I am facing extreme inconsistency in Throttling mechanism in Django rest framework. I have the api endpoint to throttle 10 requests/sec but when I send 20 requests at the same time using Apache Benchmark the api should throttle 10 requests whereas it throttles 4, 6 and it is inconsistent. sometimes it does not even throttle any requests.

class StatusApiThrottle(CustomeRateThrottle):
    rate = '10/s'
    scope = 'status'

    def allow_request(self, request, view):  # independent throttle for GET request on view
        if request.method == "POST":
            return True
        return super().allow_request(request, view)

cache configuration


CACHES = {
"default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient"
        },
        "KEY_PREFIX": "myapp"
    }
}

Run the app using gunicorn

gunicorn app --workers 2 --threads=2

Testing using Apache benchMark


$ ab -n 20 -c 20 -v 1 http://127.0.0.1:8080/api/v1/access/842b1012-53ef-4e98-9a20-484e681c3789

result #1

Complete requests:      20
Failed requests:        6

result #2

Complete requests:      20
Failed requests:        3


jebaseelan ravi
  • 185
  • 2
  • 10
  • What is the output for "Time taken for tests"? Are you sure all 20 requests are processed within 1 second? – Iain Shelvington Jul 07 '22 at 10:55
  • Hi @lain. Thanks for your response. `Time taken for tests: 0.105 seconds` – jebaseelan ravi Jul 07 '22 at 10:56
  • This is a known issue it seems https://github.com/encode/django-rest-framework/issues/5181 – Iain Shelvington Jul 07 '22 at 11:00
  • I think it might be hard to get perfectly accurate throttling without unacceptable performance hits in the general case, and approximate is still pretty good. If someone spams the site you'll still get them cut off, but this isn't google level api usage monitoring. We've had good success with the simple method used, and I agree with `tom` in the issue report itself, and there are a lot of good responses there too. – Andrew Jul 07 '22 at 18:00

0 Answers0