SwitchPipe for Fun and Profit

Peter has stopped development on SwitchPipe in favour of mod_rails

You can read a bit about my experience with it in Ruby Enterprise Edition Gem Install Script

You may have seen or heard about SwitchPipe - a tool written by Peter Cooper, the dude who writes for RubyInside. Effectively, SwitchPipe is a proxy-esque Ruby app which listens on a port for requests and palms them off to Mongrel, thin, WEBrick, and other applications according to which site the request belongs to. The magic comes in that it's able to dynamically start, stop and manage the number of processes you have running for your sites, helping you to use your memory and cycles better.

For example, for most production Rails sites you'll want to have at least 2 mongrels running to ensure that it's responsive even when someone is doing something that takes a while (eg. slow upload, long running query, etc.) However, when the server is idle, or the site is not busy, there's a mongrel kicking about doing nothing but chewing up a big batch of memory. SwitchPipe allows you to say things like "for this site, keep 1 mongrel running at all times, and if it can't handle it because it's too busy, you're allowed to start up to 3 other instances". Then, after a period of inactivity (which you can define) your mongrel instance dies off, freeing up memory again. You also have the benefit of being able to "burst" a bunch of instances for a short period of time. For example, you might get a rush of users making slow requests - SwitchPipe can start up more instances of your application to keep your site responsive, and you can still set limits easily to ensure that one site can't overload your server.

It's a totally sexy tool - I'm using it exclusively on my new SliceHost slice. As the slice currently only has 256Mb of RAM, having more than a few mongrels running at any time (especially with some of my larger applications) causes the server to slip into a swapping nightmare. So, I'm using SwitchPipe to dynamically start and stop the instances as they're required. This means that my quieter sites (this blog, for example) isn't always chewing up memory, and that memory can be used by the busier applications.

There is a slight hit when starting a new instance, but it's not terrible - usually a few seconds, depending on how fast the server can start the Mongrel instance. For example, on my server I say that I don't want any mongrels always kicking around for this blog, but once they're started they should live for a few minutes before dying off. This means that when a visitor navigates to my site, they wait a couple of seconds while Mongrel is started (unless it's already started), and then so long as they make another request before the instance times out, it'll all be super fast and sexy - reusing the already started instance. Of course, I also use Apache to serve my static content so as that images, etc. will always be super fast to be delivered.

Not convinced? Not just this blog is running through SwitchPipe - so is the Achernar Solutions site, and 5 or so other sites on this server. And they've all been running in this way for well over a month with absolutely no problems. I haven't even bothered to install pal on the new slice (pal is the tool I wrote to make managing Rails sites easier for administrators) because SwitchPipe is simpler, and has more features. SwitchPipe manages all the ports for me automatically and manages starting and stopping all the processes for me. Why would I need pal when I just need to drop a YAML configuration file into SwitchPipe's configuration directory and it will pick it up on it's own and start handling the site?

So, if you're hosting Rails apps, Merb apps, Django apps, and a whole host of others, SwitchPipe is going to make your life simpler. Check it out!