Tips for improving your Django app performance
Tags: python, django, webdev, discuss
Published: 2019-03-11 01:34:44

---
title: Tips for improving your Django app performance
published: true
description: Tips for improving Django performance
tags: [python, django, webdev, discuss]
---
![Django](https://thepracticaldev.s3.amazonaws.com/i/ctlp5wefnneg8kxjs3w8.gif)

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. 

My list

1. Deploy your app using nginx + uWsgi/gunicorn. 
2. 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](https://www.sumologic.com/blog/devops/content-distribution-amazon-cloudfront/) there is a tutorial about it. 
3. Take time to create your database indexes. Understood your most complex queries and try to create indexes for them. 
4. Install [Django Debug Toolbar](https://github.com/jazzband/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.

![Debug](https://camo.githubusercontent.com/577d78f4c1fdb33761f2c8e541909c117a67f8ee/68747470733a2f2f7261772e6769746875622e636f6d2f6a617a7a62616e642f646a616e676f2d64656275672d746f6f6c6261722f6d61737465722f6578616d706c652f646a616e676f2d64656275672d746f6f6c6261722e706e67) 
5. 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](https://hackernoon.com/all-you-need-to-know-about-prefetching-in-django-f9068ebe1e60). Trust me, this gonna blow up your mind. 
5. 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](https://realpython.com/caching-in-django-with-redis/)
6. If you are using Django templates, you can use cache over them as well. Here is a good [explanaiton](https://hackernoon.com/are-you-using-djangos-cached-template-loader-9e1145a7d610)
7. 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. 
8. If you are going to handle throusand requests per minute, use this performance booster [__Varnish__](https://varnish-cache.org/). 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.