Let’s admit it, Django rules. It is simple, trustworthy, fast and it is written in Python. What else do you need?
However, we tend to abuse its easiness and we quickly forget that an application grows; and with it the amount of code processed as well as the amount of data saved on the database. This affects directly the application’s response time. That is why our application is fast in the beginning and then one day you check it and it is very slow for Django standards.
But it is not Django’s fault, it is our fault. We tend to think “Django ORM is so cool, let’s use it everywhere all the time with no considerations” or ” Django views rules, I may calculate everything for my view and send the information on the context and the app is still fast” or simply “Do not worry, I will improve that code on the next release” and we never commit with that refactor.
For these reasons, and considering that we are the main reason why the software fails, let’s check some considerations to make our Django app faster.
One thing that we usually forget is that each query to the database is expensive; especially if you are using filters and if your database has a lot of information. For this reason, they are the main slowness factor on a Django app. The problem is that we use them carelessly because they are usually just a couple of lines and because we are not dealing with the database directly but through Django’s ORM; even if we wrote Python, it is running SQL, and SQL may be very slow.
That is why it is important to debug and keep track of your queries. One awesome way to do it, is to use Django Debug Toolbar library. It has a SQL panel that not only show you the amount of queries made to the database and their execution time; but gives you a detailed report of what does each query do, how many times a query is repeated, what are the most expensive queries, etc. If you are using Pydanny’s Cookie Cutter, this library is already installed for local instances.
On the other hand, If your app is already on production, you may use a monitoring system such as The Elastic Stack. These kind of tools may give you more detailed information, based on your site’s visits such as the slowest view in the app, its most visited view, its average loading time, etc. This may allow you to optimize the views that really matter first and prioritize the work to ensure that your visitors have a neat experience on your site.
Using these tools, I was able to reduce the queries in a very heavy view from 1500 to 31 in less than an hour working on it without losing information; reducing the load time from several seconds to less than one second. It was amazing!
Here are some tricks to reduce the queries in your app:
There are certain processes that do not require to be executed synchronously that may take time and slow our app. For example, sending an email to the user after he/she performs an action, calculating values for a report or updating some information inside the database. As well, there are some processes that may take too long to finish that will affect our app directly.
For all these kind of actions, you may use Celery. According to its documentation:
Celery is a task queue with focus on real-time processing, while also supporting task scheduling.
http://docs.celeryproject.org/en/latest/index.html
With celery, you may create tasks. A task is simply a method that may be executed asynchronously. What celery does is to create several workers (4 by default) that will listen to the task query. Every time a task is added to the task query, the first available worker will get it and execute it. If several tasks are added at the same time, each worker will get one until they are all busy. Once one of them finishes, it will get the next task in the query until it is clean. The workers run simultaneously, so several tasks may be running at once. As well, being an asynchronous process, there is no control on the order the tasks finish. The first sent to the task query may be the last one to end its execution.
The workers run in a separate service from your app, so your app does not get affected by their running time. Even if a task raises an error, your app will not be affected by it (being a good tool to isolate error prone processes). For this reason, you may execute as tasks all those processes that you do not care where are going to be run.
As well, for very slow processes like data analysis, you may use celery along with a REST API or websockets, consuming these with a JS framework such as Vue. It is better for a user to see a loading message on the screen that nothing at all. As well, considering that celery is asynchronous, you may start displaying the results that are already calculated on the page without waiting for all the processes to finish.
Almost all our Django applications use Celery, you should give it a try!
One of the first rules of coding is the DRY principle, Don’t repeat yourself. You should always avoid coding functionalities or objects that already exist; especially when you are working with a framework such as Django. I bring this up because usually the code we repeat is slower than the one that comes with the library core. As well, using the tools given by the library allows you to check for performance consideration inside the documentation, better practices, etc.
I know this recommendation is kind of obvious, but it is impressive how many times I have found handmade code that was already implemented on the Django core. Check Django’s documentation or browse the web before coding; you may be pleasantly surprised.
If you have a method with a result that will not change in a lot of time you may consider caching it. Django provides a low level cache that allows you to store specific data in a cache layer without caching the whole page.
Here is a small example:
from django.core.cache import cache cache.set('my_key', 'hello, world!', 30) cache.get('my_key') cache.clear()
With this cache, you may calculate a heavy value once in a specific period of time reducing the response time on all the other requests. It is very useful if you do not need to return up to date information, if you are using an external API whose result updates once a day or once a week, etc. You may even mix it with celery and update the cache once the task finishes.
Please consider that if you are going to save a lot of information on the cache, you may consider creating a model to hold the information on the database instead. Using timestamp and celery processes may be a better way if you need to use the data for other analysis and processes. It depends on your requirements.
Keeping your code clean and organized is not only a good way to ease the development process but may impact directly on your app timing. Here are some considerations that I have found that are directly related to the app performance:
I know that you may be very worried after reading this blog post. But take it easy! go and check you app and start improving its performance. One of the best things of python and Django is that they are very versatile so refactoring your code to improve your app will be much easier than you thought. So, happy coding!