Glenn Gillen

Improving rails app and mongrel performance with Thin

Thin is a replacement webserver for mongrel* that further improves the already lightening performance of mongrel. Installation is a breeze, performance is fantastic, but it's still in alpha so use with caution.

As you can see in the chart above (taken from the Thin website, I've not had a chance to verify independently), the improvement while handling 10 concurrent connections isn't exactly an order of magnitude larger. But you'll see that as concurrency increases, performance holds steady unlike using mongrel. This is because it's using EventMachine network I/O library to handle incoming requests much quicker than mongrel can. But mongrel is super quick at parsing HTML and Marc recognised that, so Thin still uses mongrel to handle that aspect (and uses Rack to talk to it).

Installation

It's a gem, so it's easy:

sudo gem install thin

Configuration

If you want to just get straight into it, you can start up a thin server with the following:

cd /to/my_rails_app
thin start

If however you want to setup a cluster similar to you have with your mongrel cluster, and then run those instances so it will continue to work with your upstream config in nginx do the following:

thin config -C config/thin.yml --servers 3 --port 5000 --chdir
thin start -C config/thin.yml

That will create a config file with 3 instances starting at port 5000.

Improving Thin performance with unix sockets

But we can do one better than that. Thin also supports a feature not often seen in the ruby webserver world, but one you've probably seen with MySQL. It can enable access to the webserver via a unix socket. What's the point? Well it means that communication doesn't have to got back through the internal ethernet interface with the associated TCP overhead. And thankfully, nginx has support for accessing upstreams via a socket. You can start a server as sockets with the following command:

thin start --server 3 --socket /tmp/thin.sock

That will create 3 sockets in /tmp/, named thin.0.sock, thin.1.sock, and thin.2.sock. All that is left to do is update your nginx.conf to use these as your upstream:

upstream  my_server {
  server   unix:/tmp/thin.0.sock;
  server   unix:/tmp/thin.1.sock;
  server   unix:/tmp/thin.2.sock;
}

Glenn Gillen

I'm an advisor to, and investor in, early-stage tech startups. Beyond that I'm an incredibly fortunate husband and father. Working on a developer-facing tool or service? Thinking about starting one? Email me and let me know or come to one of our days to help make it a reality.