Tuning a Pack of Mongrels
June 19th, 2007
After having installed mongrel and mongrel_cluster, I began reading the Tuning article. This article is basically in response to people asking “How many mongrels should I have?” The quick answer is it depends.
The article talks about how to determine what you need because every situation is different. Some of the steps they talk about:
- Baseline your server by requesting a static file. If your site was entirely static content, then this is the performance you would get. Rarely is a site entirely static these days, but it gives you an idea about what you’re shooting for.
- Run your tests multiple times. This allows for a warm up run and helps reduce the chance you’re seeing anomalous behavior.
- Increase the number of connections until the test runs for 10 seconds.
- Add mongrel instances to your setup to see the rate increase/decrease. This allows you to tune into how many instances you need.
- Continuously measure against your benchmark. They use httperf.
The examples use httperf as a tool for adjusting the inputs and gathering the outputs of adding more mongrels, concurrent connections, etc. I did not have httperf installed on my Mac, but it seems that the Apache HTTP server benchmarking tool is already installed with OS X. You can find it and it’s options by typing man ab. It gives you some pretty nice information and allows you to specify the number of concurrent connections, number of requests, etc.
Exploring Mongrel... Finally
June 19th, 2007
Well, it’s time for me to learn about mongrel and mongrel cluster.
What is Mongrel?
Mongrel is a fast HTTP library and server for Ruby that is intended for hosting Ruby web applications of any kind using plain HTTP rather than FastCGI or SCGI.
Mongrel was created by Zed Shaw and modeled after a Java web server framework called Simple. It is written in C and Ruby with an HTTP 1.1 parser written in Ragel. Mongrel is open-source, so you can modify it to fit your own needs, but it also supports using GemPlugin as an extension mechanism.
First, install Mongrel from the gem.
sudo gem install mongrel
Now create an application to test that we can use Mongrel.
rails test_mongrel
cd test_mongrel
mongrel_rails start
You should see Mongrel start the development environment on 0.0.0.0:3000. That’s it… Well, there are other options that you can pass to Mongrel if you want to customize, find them here. What we have right now is good enough for development purposes.
What is Mongrel Cluster?
Mongrel cluster is a wrapper around Mongrel to allow you to run a “pack of mongrels”. It basically lets you configure and run multiple Mongrel servers. Add to this a load balancing reverse proxy solution (e.g., Apache) and you can have multiple Mongrels running with requests balanced between them. Mongrel cluster was created by Bradley Taylor of RailsMachine.
First, install Mongrel cluster from the gem.
sudo gem install mongrel_cluster
Now, using our application created earlier, we configure Mongrel cluster. (From the test_mongrel directory)
mongrel_rails cluster::configure -p 8000 -N 3
This creates a configuration file in config/mongrel_cluster.yml. The configuration file is chock full of defaults except for the two items we specified on the command line. The first option (-p 8000) specifies the beginning port number. The second option (-N 3) specifies that there will be 3 mongrel instances. Now you can start your cluster.
mongrel_rails cluster::start
You should be able to navigate to your localhost on ports 8000, 8001 and 8002. Give it a try. To see the other options when creating the configuration files, see the docs.
The only thing missing at this point is configuring your load balancing proxy, so your users don’t need to specify the port number of the instance they want to make requests on. I have not done this yet, but it looks like the Mongrel site has good documentation for Apache, Lighttpd, Pound and Pen/Balance.