January 26, 2008...12:46 pm

Get intimate with your load balancer tonight!

Jump to Comments

The Thin Cheesecake release is out (v0.6.1)!

sudo gem install thin

That tasty and sweet Cheesecake release comes with some new sweet topping: config file support, uses less memory, some speed tweaks, but that’s nothing new regarding what we all know and use from other web servers. Nothing very innovative, breath taking, crazy, revolutionary or surprising you say.

You’re right!

Almost …

There’s another feature that as never been seen amongst Ruby web servers (indeed, haven’t found any that does that). But first, lets explore the typical deployment of a Rails app.

Let’s deploy Rails shall we?

Typically you’d deploy your rails application using mongrel like this:

mongrel_rails cluster::configure -C config/mongrel_cluster.yml --servers 3 --chdir /var/www/app/current ...
mongrel_rails cluster::start -C config/mongrel_cluster.yml

Then on your web server / load balancer configuration file (Nginx in this case):
nginx.conf

upstream  backend {
  server   127.0.0.1:5000;
  server   127.0.0.1:5001;
  server   127.0.0.1:5002;
}

Now with Thin, it’s the same

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

That will start 3 servers running on port 5000, 5001 and 5002.
And your web server configuration stays the same.

Really, between you and me, is Thin really really faster?

Benchmarks
Simple “hello world” app, running one server

And uses less memory too:
Memory
Mesured after running: ab -n5000 -c3

What about that new, crazy, amazing feature you mentioned?

Ever wanted to keep closer to you web server? Sometimes connecting through a TCP port on 127.0.0.1 feels a bit … disconnected. What if we’d get closer to it, get intimate with it, share some feelings, exchange toothbrush?

Introducing UNIX socket connections

When using more then one server (a cluster) behind a load balancer, we need to connect those servers to the load balancer through dedicated ports like in the previous “Let’s deploy Rails” section. But this connect the 2 using a TCP socket which means, all requests have to go though the network interface stuff twice! Once from the client (browser) to the load balancer / web server and again to the backend server.

There’s a better approach to this. Some load balancer (like Nginx) support connecting to a UNIX domain socket.

nginx.conf

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

Then start your Thin servers, like this:
thin start --server 3 --socket /tmp/thin.sock

And yes, it is faster:
Socket
3 servers running a simple Rails application, behind Nginx

30 Comments


Leave a Reply