This post is a part of the series of articles about choices available to a rails developer. Previously we discussed about the choice of databases. This time it is about application and web servers. We can make the choice of an application and a web server separately, but as they are so closely related that it is better to look at them together.
It is well known what a web server does, but for starters – the web server receives the HTTP requests first, and then responds by either serving the page requested or directing the request to our rails application server. The major web servers market-share wise (survey graph below) are Apache, IIS(Microsoft), nginx, GWS(Google) and lighttpd. We as rails developers are interested in all these except IIS and GWS. Not IIS because it is closed source and proprietary and not GWS because it is used internally by Google. Apache, nginx and lighttpd are all popular among rails developers.
The most popular web server in use. It supports a wide variety of features, and is reasonably fast as well. Many of the essential features are provided as compiled modules that extend the core. These include support for server side scripting languages like perl, php, tcl, python; support for authentication; support for ssl and tls and compression support.
The only issue with apache is that because it is process based, it spawns too many threads when there are too many simultaneous requests. This leads to excessive memory usage, so the performance can drop in restricted environments under heavy load.
The light-weight and high performance web server. It has most features that any good web server should have, but surely lacks a few that apache has. The major differentiator with nginx is the performance. Because of the event-based architecture, it gracefully handles tens of thousands of concurrent requests. There is limited english documentation (which is improving), as it was developed for russian portals . The adoption rate is very high – it has quickly become the third most deployed web server (after apache and IIS).
Lighttpd another light-weight, efficient web server with low memory footprint. It also has an event-driven architecture that is optimized for a very large number of parallel connections. Most rails applications that use it run as lighttpd + fastcgi configuration. The concept is that lighttpd receives the HTTP requests, and it spawns our rails app instances using fastcgi. In comparison, nginx and lighttpd are almost equally good in performance (some benchmarks), and some people find lighttpd easier to configure. However, there is a perception that lighttpd may not be stable because there are some very old memory leak bugs in it.
Here is a comparison of the market share of the major web servers –
Application server answers requests to the application and is mostly not meant to serve static content. In rails context, the app server runs our rails application code, on requests from a web server. The web server and the app server generally communicate via some standard communication protocols like FastCGI or HTTP.
The first server we come across when starting with rails is Webrick – it starts on a script/server command in development environment. It is a ruby library for providing http web services and a server. It can be used only in very simple circumstances like development, and anything beyond that, like the production environment of the same rails app, should be run on one of more specialized servers like mongrel, thin or passenger.
Mongrel is the first contender for a production environment application server in rails. It uses plain HTTP to talk to the web server, and is reasonably fast because it uses the Ragel compiler. (More on Ragel: wikipedia page, mongrel creator’s essay). A very popular way of running mongrel is to run it as clusters behind an apache server using mod_proxy_balancer for load-balancing. It can also be made to run as clusters behind nginx or lighttpd. The only flip-side of mongrel is that it is a little tough to administer the server, especially when operated in clusters.
Thin is yet another web server that has the same philosophy as mongrel (uses the mongrel parser), but uses the event machine to make it run faster than mongrel. Like mongrel, it is intended to be run behind apache or nginx web servers as clusters.
Passenger is the ‘mod rails’ for the apache web server. It is fast, easy to configure, is technically sound and very stable. For example, apache with passenger doesn’t crash if our app has to crash. It is recommended officially, and is praised by the creators of rails. It can run with nginx as well, and is supported on all unix-like operating systems – it does not work on windows! It is also easy to configure and administer.
Some performance benchmarks –
Various performance benchmarks are available for comparison. The summary is that Thin is faster than mongrel, and passenger is almost as fast or sometime faster than mongrel.
Going beyond the default, if you are expecting high concurrent load on your website, you can use nginx web server with passenger or mongrel clusters.