10

I have a server with several domains and applications running, all through Apache. All is well at the moment but I have plans to develop some very performance-intensive web application (using C++ with CPPCMS), starting with my server for testing, maybe getting a separate server only for this application once it gets ready.

Any way, I've heard a lot about NGinx, that seems to be more performant than Apache, so I was asking myself if it was worth working with it for that new project. It's not clear in my mind because I don't know what kind of performance bottleneck NGinx does fix exactly.

I'm not a Apache power-user, I'm a poor linux admin and I don't develop web apps much (but I have notions). I'm mostly dedicated to writing software so the web server part is sometimes very obscure to me. Each time I have to configure a website through apach, I need a lot of time browsing in the doc to make sure I don't break everything.

That being said, I think I'm getting a lot better on this side but still need advice. I've seend some nginx configuration files around, and that seems much more understandable than the Apache ones, but maybe I'm wrong?

From informations I gathered, NGinx would be the best choice when you want load-balancing, so if you have your application spread on several machines, right? As I'm thinking my application for scalling (and performance), it looks like it's what I need, but maybe I need to know more things about when it's interesting to move from Apache to NGinx. Is it worth switching to NGinx for all my current apps too? How much does it cost? (I mean, is it expensive on time to switch from one to the other?) Can I use Apache and NGinx both on the same machine without any problem?

Side note : Please don't urge me to use interpreted languages instead of C++, it's not related to the question. See the CPPCSM rationale page to see what kind of application can benefit fromt it. I perfectly understand the drawbacks (compared to apps in Ruby and Python, that I already use for less power-hungry webapps) and I'm fine with it.

Klaim
  • 215
  • 2
  • 8

3 Answers3

10

Several points:

The major difference between Apache and Nginx or Lighttpd (that I personally very like) is architecture:

  1. Apache handles one connection per-process or per-thread (depending on mod-XYZ)
  2. Nginx and Lighttpd are single threaded handle multiple connections in single event loop.

As a result:

  1. Nginx and Lighttpd scales much better under high number of simultaneous connections, let's say with 1000 connections Apache would almost die as it would require 1000 processes in mod-prefork or 1000 threads in mod-worker.

    If you are planning to use Comet technologies where each connection requires long polling HTTP connection then Apache would not be acceptable as it does not scales well.

  2. Nginx and Lighttpd consume less memory as each connection require +- two sockets (HTTP and FastCGI), intermediate memory buffer and some state, while Apache would need entire thread including stack and other things.

From my personal experience in benchmarks I did Lighttpd (and I assume Nginx as well) is slightly faster with FastCGI backend then Apache but this is for low amount of connections.

Now another point is when you want to do some load-balancings using FastCGI connections.

In traditional architecture there is

                               |
                              HTTP
                               |
         Balancer (Nginx/Lighttpd/Cherooke/something-else)
            /      |           |            |      \
         HTTP    HTTP         HTTP        HTTP     HTTP
         /         |           |            |         \
 Apache+PHP   Apache+PHP   Apache+PHP   Apache+PHP   Apache+PHP   
  Server 2    Server 3     Server 4      Server 5     Server 6

Because Apache handles pools of processes each of them running mod-PHP (or other modes)

When you using CppCMS that handles pools on its own you can do something different:

                               |
                              HTTP
                               |
         Balancer (Nginx/Lighttpd/Cherooke/something-else)    
                           Server 1
            /      |           |            |      \
         FCGI    FCGI         FCGI        FCGI     FCGI
         /         |           |            |         \
    CppCMS App  CppCMS App  CppCMS App    CppCMS App  CppCMS App
     Server 2    Server 3     Server 4      Server 5     Server 6

So basically you do not need another indirection level because CppCMS handles process, thread and connection pool for you. While PHP/Ruby/Perl need some Apache mod-XYZ or handle its own FastCGI connector.

Artyom
  • 754
  • 2
  • 7
  • 16
  • +1 Nice complete explaination by the author of CppCMS ;) Ok now I see better the overall problem. So the case where you have only one server (to start with) is HTTP->Balancer->FCGI->CPPCMS, if I understand correctly? What do you think about the advice from Jesper Mortensen's comment, saying that FastCGI is'nt fast? – Klaim May 25 '11 at 12:30
  • @Klaim: Look at the excellent drawings above -- in this architecture, FastCGI is used as interconnect between multiple servers, each running a multi-threaded CppCMS instance. The relative speed of FastCGI matters a great deal less in this case; your alternatives would be HTTP, AJP and other network-able protocols which are also not 'fast'. This answer should probably be marked as accepted, and your question edited, because this isn't really about when nginx is worth it. –  May 25 '11 at 16:03
  • @Jesper Mortensen> I agree, but what modification on the question do you suggest precisely? – Klaim May 25 '11 at 16:04
6

Nginx, speaking very (very) generally, can obtain much higher throughput than Apache thanks to a different architectural approach to the problem of serving pages to the web. Nginx was also built primarily as a reverse proxy, and it fills that role exceptionally well (this is the load-balancing bit you alluded to); Apache, on the other hand, was built to serve web pages, and later gained the ability to proxy.

That said, there are almost certainly areas will Apache will consistently outperform Nginx, while there are others where Nginx will just as consistently outperform Apache.

The short answer is that if Apache is working for you, there's no need to switch. (And I'm saying this as a former Apache user who has become a fully converted Nginx disciple.) Only when traffic to your server starts reaching levels where Apache is becoming your bottleneck (this is on the order of some thousands of simultaneous connections, but will vary wildly based on your server specs and other server load), or if you're trying to run Apache in a resource-poor environment where it can barely fit, does switching to Nginx give you solid benefit.

That said, if you want to switch to Nginx (which I do encourage!), then go for it. Will you see any benefit? 9 times out of 10: No, you won't. But you mentioned that you like the configuration file language of Nginx better, so if it makes you feel more comfortable to configure Nginx than Apache, well, that is a benefit for you! (Personally, I find Apache configurations easier to read in general, but that could be because I spent many, many years reading them, and only a few short months have been spent on Nginx!)

On a side note: You mentioned your desire to build a web app in C++. For the sake of your sanity, I strongly urge you to instead use a higher-level language like PHP, Python, or even Java. Then profile your code and consider creating C++-based modules to address specific bottlenecks (Python and PHP both allow this rather easily; don't know about Java). If you're worried about overall performance, consider this: EVE Online, the single largest unsharded MMORPG in the world, is built on a variant of Python (Stackless Python), with only key components (e.g. the graphics libraries) written in C++. If Python can handle that, surely it can handle your web app?

Kromey
  • 3,621
  • 4
  • 24
  • 30
  • +1 Thanks. About C++, see my comment on Jesper Mortensen answer. Also, even if Python is used on the server-side of Eve Online (I know about this, a lot) AFAIK, it manage only gameplay code (that is big), and some other parts are in fact in C++. Graphics code is on the client side so C++ is alsmot mandatory for such types of graphic performance. As I said, see the CPPCMS Rationale page about why I did chose it. If I would follow your advice, I would have to write my application twice, while I already know it's very power-hungry. Also, I understand the drawbacks and I'm fine with it. – Klaim May 24 '11 at 17:08
3

Nobody can really answer the "when should I switch" part -- it will depend on your load, the performance of your own application code, etc.

NGinx, that seems to be more performant than Apache

nginx uses a single process (or a very small number of worker processes) to handle all client connections using evented I/O. Apache has several "Multi-Processing Modules" available, but they all lean more towards many many processes / many threads. As a result, Apache will generally consume more RAM and CPU than nginx for basic HTTP connection handling. You can get an overview of the different connection handling approaches on Kegel's C10K page.

very performance-intensive web application (using C++ with CPPCMS)

I would strongly suggest to consider doing the basic webapp in a higher-level language (Python, or maybe Ruby, Scala), and use a messsage queue to send work tickets to worker machines which handle the "performance-intensive" tasks asynchronously.

NGinx would be the best choice when you want load-balancing,

nginx is a good load balancer; but there are many options in that space.

Can I use Apache and NGinx both on the same machine without any problem?

Yes. Just run them on different IP port numbers and/or IP addresses.

  • "I would strongly suggest to consider doing the basic webapp in a higher-level language" > I didn't say it was a basic webapp - it's not even trivial. If you see the rationale for using CPPCMS, the author the few points cases where it might useful, and I'm in those cases. I've considered other alternatives but found them more expensive than going with C++, at least in the kind of webapp I'm writting. So it's not part of the question. But I understand you advice and gives the same to people asking me if they should use C++ for web apps. I also plan to use Python for another more simple app. – Klaim May 24 '11 at 17:00
  • Anyway, +1 for the details. – Klaim May 24 '11 at 17:13
  • @Klaim: If you wish to realize the claimed "order of magnitude" performance improvement over a well-tuned C# / Java implementation, then see if you can run CppCMS & application code in-process with your webserver -- i.e. as a plugin to nginx / Apache. The CppCMS pages seem to show FastCGI as connection between webserver and CppCMS; and actually FastCGI isn't ... fast. –  May 24 '11 at 17:35
  • > Interesting idea. Most of the advice around is that you should use fastcgi because it's faster than the alternatives. I'll check if I can do this, maybe see with the CPPCMS author why he thinks it's the fastest. – Klaim May 24 '11 at 18:45