DEV Community

Levi Velázquez
Levi Velázquez

Posted on

Tips for improving your Django app performance

Django

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 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 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

  1. 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.
  2. 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
  3. If you are using Django templates, you can use cache over them as well. Here is a good explanaiton
  4. 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.
  5. 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.

Oldest comments (5)

Collapse
 
priteshusadadiya profile image
Pritesh Usadadiya

This is awesome tips Levi Thank you for putting this together.

Collapse
 
levivm profile image
Levi Velázquez

:)

Collapse
 
theodesp profile image
Theofanis Despoudis • Edited

If you want better performance avoid the Django ORM completely or use it for 3rd party Django Packages. Also avoid using any template engines or keep it simple. Use it as an REST API service

Collapse
 
levivm profile image
Levi Velázquez

Well, it depends, you should use the ORM if your query is simple, it doesn't make sense to use raw SQL if you want just to perform a simple select, raw SQL is for complex queries. So, I don't think that we need to avoid ORM usage at all, that is the good part about using a framework, to avoid all the overhead of doing everything manually.

Collapse
 
steelwolf180 profile image
Max Ong Zong Bao • Edited

Awesome article, there is hardly a lot of Django articles but I really like it cause it is really good in the tips and tricks for deployment.