Performance
From Django In Production
Django can handle a lot of traffic, but to do that you need to follow a few basic rules. Below follows a checklist of things to make sure you are doing as your Django site gets busier.
Contents |
Don't serve static media through Django
The first and easiest thing you should do to improve your server's throughput is to serve static media from a different web server. In fact, many see this as an obligatory step before releasing to the web. Even if it is a different server instance on the same machine you will still get improvements. The key thing is not using the overhead of Django and Python just to serve a file directly from disk.
Setting up a lightweight server and proxy
The simplest way to achieve this is to put a lightweight web server such as nginx or Lighttpd at the front of your web stack, serve any static files directly through that, and proxy any requests for Django through to Apache (or your Django server of choice) listening on another port.
Serving media from separate machine
Serving media from a lightweight server like nginx or Lighttpd is a reasonable solution, but there is the added complexity of dealing with different web servers and setting up the reverse proxy. Even better would be to serve all your static files of separate machines entirely, and this is a pretty easy thing to achieve with services such as Amazon's S3.
Many people outsource the serving of their static media to a third-party CDN enabling them to focus on other things. You take advantage of the highly scalable and redundant systems they provide and you have far less server configuration to worry about.
Separate out the database server
Moving your database onto a different machine is a relatively simple thing to do with Django. Change the DATABASE_HOST in your settings.py file to the IP of your remote database machine and Django will being querying to the new database.
Remember that you will need to allow connections to your database from outside of localhost, which may mean making changes to your firewall, such as iptables, and adding users for your applications hostname. For more information read, Setting up Django to connect to a remote database.
Monitor database queries
If you're not careful Django can end up hitting your database way more than it needs to to render a page. For anything more than the most simple views you should watch your query count very carefully. There are a couple of excellent tools for doing this:
- django-logging provides a whole raft of information output in your HTML, including details on individual and total time for database queries.
Use memcached
Django supports various caching backends, but the most effective and most recommended is memcached. Use it for anything but the most simple and lightweight Django sites.
Turn off KeepAlive
If your Django server is not serving media (which it shouldn't be!) you should turn off KeepAlives. KeepAlive
is a mechanism of HTTP which allows multiple requests to be sent across the same TCP connection. For example, if your server is delivering a whole bunch of images to one web page, then it makes sense to reuse the connection to the client for all those requests.
However, your Django server should only be serving one request per page to the user. In this scenario it is better for the server to drop the connection as soon as possible to allow it to move on to serving requests to other users.

