Nginx – Is it better to serve a django site using nginx + apache or nginx + tornado

apache-2.2djangonginxtornado

I have a django site which is ajax intensive currently running on apache mod_wsgi with nginx as a reverse proxy.

Now is it better to retain my current setup or is it better to run my django site on tornado with nginx as a reverse proxy and load balancer?

The reason I'm asking is that I've read somewhere something like mod_wsgi wasn't designed for async stuff. I'm not really sure about that part though but It got me thinking that maybe tornado with nginx will be good for my setup since its non-blocking.

I need your advice. Thanks.

Best Answer

Django sits on top of the WSGI interface and that interface is by definition blocking. That you are therefore not using any async mechanisms in your web application, what benefits you get from using an async web server is greatly reduced. Any benefits are going to be more to do with the alternate server being lightweight and not carrying so much overhead and nothing to do with fact that async is used. To get the most from an async server you would need to ditch Django and write to the async APIs of the specific async server you are using.

It should also be highlighted that the initial benchmarks that were released about Tornado in comparison to Django on top of Apache/mod_wsgi were flawed. They claimed that Tornado was 4 times faster. That may have been true for a simple async web application written to Tornado APIs, compared to a Django hello world application, but if you compare it instead to a basic WSGI hello world program, performance isn't that much difference. Thus, performance of Django running on top of Tornado with a WSGI adapter isn't that much different to Django on top of Apache/mod_wsgi and when I tested them on MacOS X, Tornado was actually slower by a little bit.

If you still feel that going with a more lightweight server will bring you some benefits or may be easier to manage, I would suggest gunicorn instead of Tornado.