Ajax call is significantly slower on live server

In my app, there are a lot of ajax calls. A user may do 50 ajax calls per session, so speed is very important. The ajax call queries the database and returns a list of records.

Doing the ajax call on my local development server takes about 400 ms. The identical ajax call on the live website takes anywhere between 1.2 - 1.8 seconds.

I checked the droplet console - there is no stress on the CPU, RAM or disk. Everything is operating at very low levels.

Why is it so much slower on the live server? What could be causing this? Everything else is the same, except the ajax call takes an extra second on the live server.

Also, this started happening only yesterday, after I added some additional records and made some minor changes to the code. Before yesterday, I did not notice any major speed differences between development and production.

I’m using Django 1.11 on Ubuntu 16.04 with Gunicorn & Nginx.


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

This comment has been deleted

@moisey Thanks for your detailed response.

I did not know about indexes before. I just added them but it only improved the performance for my development server, the performance is still bad in production, in fact I think it is slightly worse now.

Ajax calls are just really inconsistent in production. When making a smaller query, it takes 500ms to 1.5 seconds. A larger query takes 5 - 20 seconds! Not joking, 20 seconds. The same ajax call in development takes about 500-700ms.

Here is a link to one of those super long ajax calls - This one took 22 seconds for me.

If the problem was in the code, shouldn’t the call times be similarly slow for both development and production? Yet it is only slow and inconsistent for production.

So then maybe it is the indexing setup. I just set it up yesterday, after I identified the performance discrepancy between development/production, so that seems unlikely.

Looking at the chart, there is abnormally high disk usage, even if it’s just 2 or 3 people browsing the site at a time. Is this a normal amount of disk usage?

I’m a newb at this, but it seems to me something is wrong with the server. The code being executed by my development/production environments are identical.

There are a couple of things to note. First off a 400ms database call, even on a local environment is actually a pretty slow and long query. You should really want these queries to be executing at less than 100ms and it’s unclear if that is a single ajax call that is 400ms, or if its the combination of all ajax calls.

The second part is that if you are dealing with slow queries what you can do is turn on slow query log on your database which will create a log file of all slow queries so you can drill down deeper and see which specific queries are slow.

Often times when you have a mismatch like this it may indicate that you have indexing setup on your local environment, but you maybe missing the indexes on your table/column on the database which would cause a significant change in query execution time. So you want to check your database tables to see that you have the same indexes setup between your local and production environments.

The last part is you mentioned everything was fine until you made a code change. Nine out of ten times the code change is what caused the problem. If you aren’t seeing any significant load in CPU or RAM, or any other system resources, and if the queries are always slow, as opposed to periodically slow, then you want to make sure that the code change isn’t at fault. So revert back to before you made the changes and try again.