Improving rails app and mongrel performance with Thin

Feb 06, 2008

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;

}


Hi, I'm Glenn! 👋 I've spent most of my career working with or at startups. I'm currently the Director of Product @ Ockam where I'm helping developers build applications and systems that are secure-by-design. It's time we started securely connecting apps, not networks.

Previously I led the Terraform product team @ HashiCorp, where we launched Terraform Cloud and set the stage for a successful IPO. Prior to that I was part of the Startup Team @ AWS, and earlier still an early employee @ Heroku. I've also invested in a couple of dozen early stage startups.