Nginx/Gunicorn taking too long to respond

djangogunicornnginx

I have a Django app setup to serve static files via Nginx and the upstream running through Gunicorn to run the Django app with a PSQL database residing on a dedicated server.
The problem is that the site is taking too long to respond even for the home page which does not require a trip to the database.
One important detail to note is that when I restart the supervisor service (which is responsible for two processes, one being gunicorn and another celery), I get fast responses for a maximum of 3 requests.
After those 3 requests, the application becomes unresponsive again.

As for configuration, Nginx is configured with timeouts (all 3: connect, read and send) set to 300.
Gunicorn has the same timeout and is configured to have 3 workers running.

The application is running on a VPS with a single CPU and 1GB memory.
Below is the output of iostat for basic stats.

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           1.49    0.00    0.44    0.03    3.21   94.83

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
xvda              0.74         3.23        12.79    1023355    4054257'

What could be causing this behavior?

Best Answer

As mentioned in the comments by Tero Kilkanen, it turns out that the problem was the code itself.

I was hammering the server with requests in order to achieve some semblance of a realtime application and this in turn made the entire server unavailable to serve other requests.
Looking at the server stats did not help elucidate the problem since all metrics showed the server to be available for work.

So if you encounter such a problem, look at your Nginx logs for multiple requests within a short interval.

Related Topic