After years working with Django, I would like to know some tools/techniques previously in order to improve the performance and spot the bottlenecks.
So, an important topic is performance, let's share tips about how to get Django faster/improve performance.
- Deploy your app using nginx + uWsgi/gunicorn.
- Serve static files using AWS S3(or any other kind of repo). If your portal is gonna be worldwide or huge, you can put a CDN in front. CloudFront could be an option. Here there is a tutorial about it.
- Take time to create your database indexes. Understood your most complex queries and try to create indexes for them.
- Install Django Debug Toolbar in order to inspect your DB queries. It is a requests profiler. You can check CPU Time, request time, it has plenty of options.
- Use select_related() and preselect_related(). Those Django's built-in function has been a life-changer to me. They help you to speed up your queries a lot. Why ? check this tutorial. Trust me, this gonna blow up your mind.
- Use caching. Django by default allows you to set a cache manager. You can use memcached or redis. If you have a lot of repetitive queries, returning the same over and over again. It doesn't make sense to execute the whole request. You can cache the response in Django. This is a good starting point How to cache using Redis in Django
- If you are using Django templates, you can use cache over them as well. Here is a good explanaiton
- Use any task manager (Celery) for non-blocking/async tasks, for example, if you need to send an email, you should delegate that task to celery.
- If you are going to handle throusand requests per minute, use this performance booster Varnish. It adds a cache layer but a greater level. It could be set on top of Nginx and it's able to cache the WHOLE request without hitting your application server. If you are wondering the difference between caching using Redis or Varnish. Note redis performs cache at application level, your application server process the whole request, use your cache and create a new HTTP response containing your data. Varnish get the request, check given rules and it returns the previous cached request avoiding nginx/application server overhead due to request processing.