Adam @ Heroku
a tornado of razorblades

Sinatra, My New Favorite Microframework

Posted by Adam Wiggins on June 10, 2008 at 02:19 PM

A few months ago, I went in search of a way to build an extremely lightweight Ruby web app. Merb can be stripped down pretty far, but I wanted a true microframework. Ramaze and Camping were getting close, but didn't quite fit my taste. Then I discovered Sinatra.

Sinatra apps are typically written in a single file. It starts up and shuts down nearly instantaneously. It doesn't use much memory and it serves requests very quickly. But, it also offers nearly every major feature you expect from a full web framework: RESTful resources, templating (ERB, Haml/Sass, and Builder), mime types, file streaming, etags, development/production mode, exception rendering. It's fully testable with your choice of test or spec framework. It's multithreaded by default, though you can pass an option to wrap actions in a mutex. You can add in a database by requiring ActiveRecord or DataMapper. And it uses Rack, running on Mongrel by default.

One of the most important backend services for Heroku is written using Sinatra. We're now running several hundred instances of it in our cluster. It's performed like a champ - I haven't seen it die or leak memory, other than bugs in our app code.

Some interesting (though not necessarily meaningful) stats.

Lines of framework code (not counting tests or examples)

Rails87,990
Merb-core12,417
Ramaze11,796
Camping1,704
Sinatra1,576

require 'sinatra' pulls in just one file: sinatra.rb. Now that's a commitment to small.

How about memory footprint? Camping takes the crown here, but Sinatra doesn't do too shabby:

Memory footprint of an empty application

Rails52MB
Merb-core25MB
Ramaze18MB
Sinatra16MB
Camping7MB

Aside: I got these numbers using the Linux free command before and after starting the server. Gauging real memory usage is very difficult because of shared pages, but free is much better than the VSZ/RSZ silliness you see in ps, which don't tell you very much.

But my real joy in Sinatra is its minimalist simplicity. The direct mapping of URLs to code (routes? who needs 'em?), the incredible ease of writing tests, and even just the simple fact that the return value from an action is its output. For example, an action might look like:

get '/posts/:id.xml' do
  Post.find(params[:id]).to_xml
end

And a matching test:

should 'get a post in xml format' do
  Post.expects(:find).with('123')
  get_it '/posts/123.xml'
end

When I return to writing Rails apps after working on Sinatra for a while, I sometimes find myself thinking: "wait, what did I need all this other crap for again?"

Tags: sinatra
Hierarchy: previous, next

Comments

There are 3 comments on this post. Post yours →

I am a noob with Ruby but since many (all?) of those are running Rack, couldn't you just use Rack and build out the short list of things you need from there? I suppose time is the issue there. Thanks for the great post. I looked at all those because I wanted a web framework but I wanted to try to focus as much on Ruby as possible while learning. I thought Rails was too big and Merb is just going in the direction of a "better Rails." I ended up with Ramaze.

Oh, yeah, Sinatra is quite the hotness! Not only are you guys using it, but Github's API is also powered by Sinatra.

I have a couple of much lower profile things in production that are using Sinatra as well. Glad to hear that more cool cats are jumping on the bandwagon.

That's awesome! I am a hanger-on to the Sinatra community, and have been making a big push to document everything I find out on my blog (http://www.gittr.com). I'd love to know if you ran into any sticking points, or things that could be better documented or tweaked.

@Gexa - Sinatra is a pretty thin layer on top of Rack. It provides the common needs of routing requests, and rendering templates. Most everything else is an unrelated gem, or a piece of Rack middleware. In relation to Ramaze, it's a little lighter weight (no controller classes, just pure mappings), but has a very similar core idea of a minimalist web framework.

Post a comment

Required fields in bold.