Staying Alive with Thin!

TravoltaThin was the first Ruby server to be able to connect to UNIX domain sockets, giving you a little more speed, so you could spend less time browsing and more time dancing and eating ice creams with the people you love. But only Nginx (that I know of) supports UNIX domain sockets. Non-Nginx users might like to have time to dance and eat ice creams with the people they love too!

So that’s why Thin new release (0.7.0 codename Spherical Cow) supports persistent connections (aka Keep-Alive).

Apache & mod_proxy

Under Apache, if a client requests a persistent connection, the connection to the backend server (through mod_proxy) will also be persistent. That means the time spent opening the connection is saved for subsequent requests because all requests will be sent through the same connection. Even cooler is that you have nothing to do to set that up, unless you’ve turned Keep-Alive off.

I ran some benchmarks and I got an average gain of 200 req/sec with Keep-Alive on.
Keep-Alive benchmark

Sadly Nginx doesn’t support persistent connections to backends yet, but it seems to be a highly requested feature, and Igor Syseov (author of Nginx) said he’s working on it several times on the mailing list. Imagine that: UNIX sockets + persistent connections, oooh man!

Hey we develop too, sometimes!

Of course you’ll take advantage of that feature when it’s just you, Thin and your browser. And we can also benchmark it, just for fun, to get numbers that are sure to give you goose bumps: 7800 req/sec is just, ooooh, aaahh, hummm, yeah, that was good!

Keep-Alive

Swiftiply

If you’re looking for even more speed. Thin can now be used as a Swiftiply client. And it’s very easy to use:

thin start --servers 3 --port 8000 --swiftiply

Just add the --swiftiply option. This also means that any Rack adapter can be run through Swiftiply (using the --rackup option) including Ramaze, Camping, Merb, YourCrazyLittleFrameworkThatIsSoooBetterThenAllTheOtherAndThatSupportRack etc.

Speed with control

But what is speed with no control, right?

The main reason why Mongrel couldn’t support persistent connections was because of Ruby 1024 file/socket descriptors limitation. If you don’t close the connection (keep them alive) it’s one less descriptor you can use to process another connection or open a file.

Although EventMachine doesn’t have an infinite number of file descriptors it was reported to handle more then 20 000 concurrent connections.

You can now tune the number of connections a Thin server can handle.

--max-conns: This sets the maximum number of concurrent connections your Thin server can handle. Setting higher then 1024 might require superuser privileges on some system.

--max-persistent-conns: This sets the maximum number of persistent connections your Thin server can handle at the same time. If resource usage is important, you might want to turn that down. You can turn Keep-Alive support off by setting to 0.

Get it!

Spherical Cow also comes with a couple bug fixes and tweak.
As usual, you can get the latest version from RubyForge:

sudo gem install thin

If you have any question, join the Google Group or the #thin channel on freenode.

I hope you like it!

16 Comments

Filed under rails, ruby, thin

16 responses to “Staying Alive with Thin!

  1. Well, SwitchPipe followed Thin straight away by supporting connecting via UNIX sockets (to Thin backends, primarily – it works well and is a bit faster).. so perhaps I should get working on Keep Alive support 🙂

  2. yeah peter, that’d be cool!

    But I suspect implementing this for a proxy server is a little harder then for a simple web server since you have to manage the pool of persistent connections. That might be why it’s taking forever to be implemented in Nginx.

    Let me know if I can help you w/ that, it’s sure to give a significant speed boost.

  3. MA: How can I start Thin with socket support for Merb apps? AFAIK, I can only start Thin using the “merb -a thin” command at this moment as doing “thin start” doesn’t work. However, Merb doesn’t currently allows me to specify the “socket” argument.

    Is there another way I’m not aware of?

  4. Carl, you have to create a Rack config file. It’s very hard to create one for Merb 0.5 since it was not based on Rack but w/ the latest Merb 0.9 it should be something like:

    ## in config.ru file
    Merb::BootLoader.run
    run Merb::Rack::Application.new

    then run w/ thin using:

    thin start -r config.ru

  5. Marc: Yeah, it’s a lot harder, but there are two levels you can implement it on. First, there’s the trick of, as a proxy, opening a Keep Alive connection to a backend, then sharing it between multiple proxy requests. This would yield an improvement on its own and not require any tracking.

    But then, yeah, you have the next level up of allowing Keep Alive the whole way through, from client to proxy to backend, and that’ll need quite a bit of thought 🙂

  6. MA: thanks for the answer! Why couldn’t Thin detect a Merb application and automagically start when doing a simple ‘thin start’? That seems like the way to go, no?

    Awesome software! It’s not in production at ajaxwhois.com

  7. yeah carl that is a great idea, now that Merb internal are more stable w/ 0.9 it is possible, was not w/ 0.5

    ajaxwhois is now running thin or is not? :/

  8. Carl: the generic “thin start” command looks for “config/environment” as it would be in a Rails app. Otherwise, you need to point it at a config (rackup) file.

    Peter: I love SwitchPipe so far, great work!

  9. Pingback: A Fresh Cup » Blog Archive » Double Shot #152

  10. this is awesome, i’ve been given thin a try today

    so for development purpose, start today i will use thin and replace mongrel

    hope, Thin web server gain more popularity and getting better performance, so we can use it for production server

  11. Pingback: Just Try To Be a Bloggrammer :: Thin, Solusi alternatif web server Ruby :: February :: 2008

  12. Pingback: Just Try To Be a Bloggrammer :: Thin, Solusi alternatif web server Ruby :: February :: 2008

  13. nice option thin +swiftiply

    who server static contents fast thin OR apache

    is there any setting sothat we can improve proxy balancer serving style to proxy[thin ]

  14. Pingback: Thin 0.7.0 Released

Leave a comment