How to use Thin effectivly

Thin is a super-cool Rack app server written in Ruby. Normally you’ll run it behind a Webserver like Apache or Nginx. It’s great and I use it for most of my Rails and Sinatra Web apps. But my goals isn’t to convert you (like I was converted from Passenger). My goals is to tell you how to use it effectively in production.

Thin has plenty of docs with commands like

thin start -p 3001 -e production —threaded

That’s great for testing and development, but not for deployment. Fortunately Thin has some great options, but unfortunately it took hours of searching to find them. So here they are, all in one place. All of the following assumes you’ve already installed Thin with

gem install thin
are generally comfortable with it, and are running as root or sudo-ing.

Start your Thins on system boot

thin install

You’ll see output like the following. This will boot all your configured Thin apps every time your system starts. Or, on Debian/Ubuntu for instance, you could run /etc/init.d/thin stop|start|restart at any time.

To configure thin to start at system boot:
on RedHat like systems:
  sudo /sbin/chkconfig --level 345 thin on
on Debian-like systems (Ubuntu):
  sudo /usr/sbin/update-rc.d -f thin defaults
on Gentoo:
  sudo rc-update add thin default

Pick one of those and run it.

Configure your Thins

Place config files for all the Thin apps you want auto-started into /etc/thin/ . They’ll be YAML files, so their extentions should be .yml. Here’s an example with lots, but not all, available options. I have yet to find a comprehensive list, but they’re mostly the same as the command-line options listed by thin -h .

--- 
user: www-data
group: www-data
pid: tmp/pids/thin.pid
timeout: 30
wait: 30
log: log/thin.log
max_conns: 1024
require: []
environment: production
max_persistent_conns: 512
servers: 1
threaded: true
no-epoll: true
daemonize: true
socket: tmp/sockets/thin.sock
chdir: /path/to/your/apps/root
tag: a-name-to-show-up-in-ps aux

Stop/restart individual apps

Maybe it’s obvious to you, but it wasn’t to me. For a long time, if I wanted to restart one of my apps, all I knew to do was restart them all with /etc/init.d/thin restart . Eventually I figured out how to do it one at a time with

thin restart -C /etc/thin/app.yml

That’s it. Happy Thinning!

  1. Hsiu says:

    Is there a way to tell thin to change the pid file’s permissions? I’d like it to be 775 (or 77x) so members in the same group as the deploy user can manually bring it down or restart it on the box, if necessary for something quick and dirty.

  2. Jordan Hollinger says:

    No, I don't believe there's an option to change the pid file's permissions. But if you want lots of people to bring it down and back up, I'd suggest writing a script, which would include your permissions change.

  3. Andrzej Krzywda says:

    Hey,

    I assume you use threaded thin in some production environment. Can you share what kind of traffic it handles? Any problems?

  4. Jordan Hollinger says:

    @Andrzej Yes I use it in several production environments, though they aren't particularly high-traffic. But based on what I read though, Thin performs just as well or better than Mongrel, Passenger, etc. under high load. For my particular use, I think it performs better than Passenger.

    In my experience I've noticed it tends to use less memory. Also its ability to connect to the front-end Web server over a socket (as opposed to http over the loopback) can be a boon to speed.

    Except for some Ruby 1.9.2 + threading problems that I had to work around I haven't had any real problems. Figuring out how to gracefully restart an app was a challenge, though. Maybe I should write a post about an actual Thin + Nginx production deployment...

    This stackoverflow discussion might offer some helpful, if brief, insight.

Post a comment


(lesstile enabled - surround code blocks with ---)