How can I serve my django website from multiple machines, that is how can I make it distributed?

What you are trying to do is implement distributed system concept for your application. For that you need to understand the concept of Distributed system and system design. Django has nothing to do with scaling your website to multiple servers. Its all now in the hand of distributed system technologies and software. Whatever I have explained in this answer gives you a basic and comprehensive understanding of how a distributed system works.

I can give you a brief explanation as to how you can achieve this. I assume you have two servers where you want a request to come in and be served by any one of the server.(Just like how any distributed system works).

First of all you should be familiar with the concept of load balancers, web servers, application servers, database servers.

When we had one server everything was a lot easier, we had one server where we set up our web server(eg apache), we needed some interface so that our web server and the web framework can talk to each other so we installed mod_wsgi apache module that can communicate with Django over the WSGI interface specification. From thereon we just had to edit our apache conf file and we were ready to go.

Now, we came to know that a single web server isn’t enough to handle those requests. We bought one more server to reduce the load from one server. The very first thing that you need to do is copy the same Django website code to another server. So that both will be running the same front end code and our application server(Django) can process the request. Now when a request comes in we need to forward it to any one of the server, to achieve this we configure a load balancer to send a request to both of them one by one (or there are other algorithms that you can use). Now whenever a request comes in it is the load balancer that will now receive the request and forward it to any one of our server. You should be having web servers(eg apache) on both of your machine because the task of load balancer here is just to direct the request to any one of the server. A load balancer is a server (typically) - that sits in front of a group of application servers and manages the traffic between them. Incoming web traffic passes through to a load balancer which distributes that amongst the web and application servers.

For better understanding lets understand this diagram,

enter image description here

There are two load balancers in this but they are in an active/passive pair - also known as HA (High Availablity) pair. Lets treat them as one (as the other is a failover). The other one only comes up whenever the first one fails. Behind them are two web servers. These could be Apache web server. The load balancer has the internal IP’s of the the application servers. The load balancer accepts incoming requests and forwards them to a server which is prepared to accept them. A load balancer just performs the function of receiving the initial requests and making sure that it gets answered by a web server. The Web server is a piece of software that responds to a request with information.The app server takes a request made and processes it, and returns a reply, through a web server as intermediary. Examples of app servers can be things like Ruby/Rails, PHP, Django etc. These servers are where the ‘code’ for the application exectutes. Caching, forward and reverse proxy are all things performed before the web server layer to reduce stress on the web server. One such technology is HAProxy which is free, open source software that provides a high availability load balancer and proxy server for TCP and HTTP-based applications that spreads requests across multiple servers.

Now coming to the database part, You can host your database in any one of the machine(using MYSQL server) and make both the servers connected to the same database using your database address. You can have a centralised database from which your application framework will be reading the data. The data will be served by the database server(eg MYSQL) and all the concurrency and availability concept will be taken care by it in case request from both the server to access the same data comes at the same time.

Also note that your load balancer will also be at any one of the machine running to receive the request and forwarding it to any one of your server.

When we have multiple servers, one of the question again arises that :

When your website is served by only one web server, for each client-server pair, a session object is created and remains in the memory of the web server. All the requests from the client go to this web server and update this session object. If some data needs to be stored in the session object over the period of interaction, it is stored in this session object and stays there as long as the session exists. If your website is served by multiple web servers which sit behind a load balancer, the load balancer decides which actual (physical) web-server should each request go to. For example, if there are 3 web servers A, B and C behind the load balancer, it is possible that www.mywebsite.com/index.jsp is served from server A, www.mywebsite.com/login.jsp is served from server B and www.mywebsite.com/accoutdetails.php are served from server C.

Now, if the requests are being served from (physically) 3 different servers, each server has created a session object for you and because these session objects sit on three independent boxes, there's no direct way of one knowing what is there in the session object of the other.

There are multiple ways to maintain the same server for a session :

  • By storing the session :

You have to store the sessions in a common storage where both your app servers can access, You can store the sessions in files ( mounted by NFS so accessible to both servers ) this is usually not recommended as it is slow. You can store the sessions in DB . Best method is to use a Redis/Memcached type of setup to store your sessions and retrieve them and they are fast and they can be shared between lot of nodes.

  • Using sticky sessions

If the load balancer is instructed to use sticky sessions, all of your interactions will happen with the same physical server, even though other servers are present. Thus, your session object will be the same throughout your entire interaction with this website. A router or load balancer with sticky-session support can assign a single server to a particular user, based on their HTTP session or IP address. The assigned server is remembered by the router for a certain amount of time, ensuring that all future requests for the same session are sent to the same server.

Note : If anyone finds any information in the answer that may not be in accordance with the actual concept please suggest an edit or reply in comments as that will help in mutual understanding of the distributed system concepts.


The step from single server setup to a distributed (highly available) is not a very simple one and is not so much related to Django, but much more to general server infrastructure. However, there are a lot of resources that can help you started.

Considering you mentioned DigitalOcean, I believe the following tutorial on their website will guide you in the right direction: Building for Production: Web Applications — Overview.

Before going through that article completely, make sure to read 5 Common Server Setups For Your Web Application as well. It gives a great overview of common server setups, from single server to your desired state.