[Fixed]-Django, low requests per second with gunicorn 4 workers

1πŸ‘

It really depends on how long it takes to do a memcached request and to open a new connection (django closes the connection as the request finishes), both your worker and memcached are able to handle much more stress but of course if it takes 5/10ms to do a memcached call then 50 of them are going to be the bottleneck as you have the network latency multiplied by call count.

Right now you are just benchmarking django, gunicorn, your machine and your network.

Unless you have something extremely wrong at this level this tests are not going to point you to very interesting discoveries.

What is slowing doing your app is very likely to be related to the way you use your db and memcached (and maybe at template rendering).

For this reason I really suggest you to get django debug toolbar and to see whats happening in your real pages.

If it turns out that opening a connection to memcached is the bottleneck you can try to use a connection pool and keep the connection open.

0πŸ‘

You could investigate memcached performance.

$ python manage.py shell
>>> from django.core.cache import cache
>>> cache.set("unique_key_name_12345", "some value with a size representative of the real world memcached usage", timeout=3600)
>>> from datetime import datetime
>>> def how_long(n):
        start = datetime.utcnow()
        for _ in xrange(n):
            cache.get("unique_key_name_12345")
        return (datetime.utcnow() - start).total_seconds()

With this kind of round-trip test I am seeing that 1 memcached lookup will take about 0.2 ms on my server.

The problem with django.core.cache and pylibmc is that the functions are blocking. Potentially you could get 50 times that number in the round trip for HTTP request. 50 times 0.2 ms is already 10 ms.

If you were achieving 1200 req/s on 4 workers without memcached, the average HTTP round-trip time was 1/(1200/4) = 3.33 ms. Add 10 ms to that and it becomes 13.33 ms. The throughput with 4 workers would drop to 300 req/s (whuch happens to be in the ballpark of your 157 number).

Leave a comment