Technology Battles: What's the clamor all about? Node.js vs PHP

Since the industry has seen a growth in distribution of mobile arm architecture products ( i.e. iphone, ipad, android products), there has been an explosion of server side frameworks and software that promises to feast the unending desire of these little buggers to consume real-time content everywhere they go.

In such a mobile, ad-hoc, and spontaneos ecosystem of client devices, it makes sense to use more javascript (to create a full user interface experience rather than "web pages"), because it works everywhere from desktops to iphones. An obvious implication of using ample javascript (if it is actually doing usefull work rather than creating bubbles on the page) is there will be an increase in the frequency and volume of clients' requests to the server. A page that was just loaded before now is loading ten other things when the user is playing with it. Now for an application that doesn't get many hits may work fine with any server side technology, but when the user base grows the number of http requests to the web server grow by an expensive factor with the emergence of new client side technologies. The number of http requests have a direct implication on the amount of memory and processing being consumed on the server, so to not unneccesarily waste our resources and keep buying new servers we always have keep an eye out for better ways to do things. In some cases older pre-fork process for each request models might not even be able to scale to our requirements even after throwing bigs chunks of money at the infrastructure.

"Node.js vs PHP" is a debate whose emergence can be associated directly to such resource implications on the server of having many little devices running javascript in the hands of people trying to enjoy your content (be glad). PHP and its frameworks have been around for years, it is reputable techonology and is being used everywhere, and there is alot of re-usable secure code available for it. Node.js on the other hand is a relatively newer framework, which can be basically looked at as the Google v8 javascript engine plus a built-in TCP/IP stack that can allow you to implement network services such as a webserver (v8-cgi can be used to accomplish the same task http://code.google.com/p/v8cgi/ with apache, however nodejs does solve concurrency issues out the box), but there is hope that Google V8-CGI might solve these issues aswell as they are rolling newer innovations. We normally use NodeJS in conjuction with NGINX as a reverse proxy to be able to handle lots of requests with caching, be able to serve static files without having to bother with nodeJS, and avoid same-domain-policy issues on browsers.

The main benefit of using Node.js for an application is that it provides non-blocking IO due to the inherent nature of javascript, and hence is thread safe (or can be made thread safe easily) meaning there is no need to a have a seperate operating system process for each web request and waste all that memory and processing power on waiting on IO operations as your kernel blocks the processeses causing lots of context switching overhead. PHP working with Apache on the other hand will use OS processes to complete each web request. But keep in mind apache gives you the option to use different Multi Processor Modules (MPM: http://httpd.apache.org/docs/2.0/mpm.html), but since all PHP libraries are not thread safe they may need to be either modified or a different MPM must be used. The prefork MPM works great but when the number of http requests grow or are anticipated to grow because of ample use of javascript on the client side it is imporant to weight out all competent technologies available before starting a project.

Maciej has performed excellent benchmark testing between Apache/PHP and Node.js, which can be found at: http://zgadzaj.com/benchmarking-nodejs-basic-performance-tests-against-apache-php

Do keep in mind he is using the prefork MPM that gets shipped with Ubuntu, which is the slowest, other mpm make use of threads and events rather than processes and hence are faster and more scalable. In our experience PHP-FPM with NGINX can really work wonders.