69

What to use for a medium to large python WSGI application, Apache + mod_wsgi or Nginx + mod_wsgi?

Which combination will need more memory and CPU time?
Which one is faster?
Which is known for being more stable than the other?

I am also thinking to use CherryPy's WSGI server but I hear it's not very suitable for a very high-load application, what do you know about this?

Note: I didn't use any Python Web Framework, I just wrote the whole thing from scratch.
Note': Other suggestions are also welcome.

4 Answers4

78

For nginx/mod_wsgi, ensure you read:

http://blog.dscpl.com.au/2009/05/blocking-requests-and-nginx-version-of.html

Because of how nginx is an event driven system underneath, it has behavioural characteristics which are detrimental to blocking applications such as is the case with WSGI based applications. Worse case scenario is that with multiprocess nginx configuration, you can see user requests be blocked even though some nginx worker processes may be idle. Apache/mod_wsgi doesn't have this issue as Apache processes will only accept requests when it has the resources to actually handle the request. Apache/mod_wsgi will thus give more predictable and reliable behaviour.

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
  • could you please add some fresh inputs about the Apache 2.4 new event based mpm? – regilero Dec 18 '13 at 18:05
  • 2
    Apache's event based MPM is very different to nginx. nginx is full async. With the event based MPM in Apache, async is mainly only used when handling sockets in keep alive mode and some other things I cannot remember. Even with event MPM, requests are still handed off to be dealt with by separate threads from a thread pool, so there is still a limited resource. Because of the keep alive sockets now maintained by async, there is a chance of over commit on resources, but there are means to deal with that and the situation wouldn't be as bad as async. Anyway, can't remember now how exactly works. – Graham Dumpleton Dec 18 '13 at 19:42
  • Hi, can we use it now? – Max Jun 23 '15 at 09:34
  • What user3526 probably meant was that now, after a few years have passed since your blog post, does nginx still have these 'issues'? – Toby Jan 12 '16 at 20:17
  • 2
    It is nothing to do with nginx specifically, it works how it works. It was the idea of embedding a blocking WSGI application within the nginx processes which was bad. This hasn't changed. As far as I know that clone of mod_wsgi for nginx died a long time ago. – Graham Dumpleton Jan 13 '16 at 02:20
15

The author of nginx mod_wsgi explains some differences to Apache mod_wsgi in this mailing list message.

akaihola
  • 26,309
  • 7
  • 59
  • 69
14

The main difference is that nginx is built to handle large numbers of connections in a much smaller memory space. This makes it very well suited for apps that are doing comet like connections that can have many idle open connections. This also gives it quite a smaller memory foot print.

From a raw performance perspective, nginx is faster, but not so much faster that I would include that as a determining factor.

Apache has the advantage in the area of modules available, and the fact that it is pretty much standard. Any web host you go with will have it installed, and most techs are going to be very familiar with it.

Also, if you use mod_wsgi, it is your wsgi server so you don't even need cherrypy.

Other than that, the best advice I can give is try setting up your app under both and do some benchmarking, since no matter what any one tells you, your mileage may vary.

Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
Chuck
  • 856
  • 5
  • 4
7

One thing that CherryPy's webserver has going for it is that it's a pure python webserver (AFAIK), which may or may not make deployment easier for you. Plus, I could see the benefits of using it if you're just using a server for WSGI and static content.

(shameless plug warning: I wrote the WSGI code that I'm about to mention)

Kamaelia will have WSGI support coming in the next release. The cool thing is that you'll likely be able to either use the pre-made one or build your own using the existing HTTP and WSGI code.

(end shameless plug)

With that said, given the current options, I would personally probably go with CherryPy because it seems to be the simplest to configure and I can understand python code moreso than I can understand C code.

You may do best to try each of them out and see what the pros and cons of each one are for your specific application though.

Jason Baker
  • 192,085
  • 135
  • 376
  • 510