BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Ruby on Rails gets down to the Metal

Ruby on Rails gets down to the Metal

Leia em Português

This item in japanese

The Ruby on Rails team has been busy moving Rails to the next level with the adoption of Rack. The implementation of Rack allows developers to use many available middleware pieces in their applications. This addition has allowed the Rails team to create Rails Metal, a wrapper around the generic Rack middleware which sits in front of a Rails request with access to Rails sessions. Metal is meant for applications that need to be very fast and process hundreds of applications per second.

David Heinemeier Hansson announced Rails Metal on the Ruby on Rails blog and describes an example of its use:

For this scenario, we’ve built a thin wrapper around the generic Rack middleware and given it a place in the hierarchy along with the name “Metal”. Rails Metal is meant for things like the Campfire poller service, which is doing hundreds of requests per second and needs to be really fast and is at the same time really simple (2 database calls and returning a string). This service is currently written in C, but we’ll look into converting it to Rails Metal instead.

Jesse Newland also posted a blog entry describing Rails Metal as a micro-framework with the power of Rails and had this to say:

After the recent work to replace Rails’ crufty request processing code with Rack and integrate its middleware support, Rails Metal is a logical progression that allows Rails apps to use the power of Rack middleware to create super-fast actions.

Jesse provides an example of the typical “Hello World” application using Metal compared to the normal Rails controller:

class Poller < Rails::Rack::Metal
    def call(env)
      if env["PATH_INFO"] =~ /^\/poller/
        [[200], {"Content-Type" => "text/html"}, "Hello, World!"]
      else
        [[404], {"Content-Type" => "text/html"}, "Not Found"]
      end
    end
end

Normal Rails controller:

class OldPollerController < ApplicationController
      def poller
        render :text => "Hello World!"
      end
end

The code is clearly very different, with a simple benchmark showing Metal 25x faster than the controller code.

InfoQ caught up with Joshua Peek of the Rails Core team and the developer behind Rails Metal to find out some details about this promising new technology.

When asked what Rails Metal is, Joshua said:

Rails Metal applications are tiny little application-specific end points that are built for speed. They can run along side your Rails
application in the same process, which means it really easy to evelop, or they can be deployed standalone.

On determining what types of problems is Rails Metal meant to solve:

Metal is meant to build little tiny services that need to be able to do hundreds of requests per second.

Rails Metal is new and not meant for everyone, so the question of what Metal means to the average Rails developer:

Though its really easy for any Rails developer to start building metal applications, it is a very sharp tool. Rails developers should still continue to use Rails as they normally do, but when they are sure that a specific action requires extra performance, the tools are available.

Metal may seem like a natural use of the latest Rack implementation but the reason for its creation may not be, Josh explains:

We wanted to give developers the best of both worlds. Rails gives you a full stack MVC framework with everything you need to build a real application, but still allows you to drop down to the "metal" when you need the most speed possible.

It is interesting looking at Merb-Core and possibly comparing it to Metal.  It turns out the two are pretty different:

Merb is a full MVC framework. However, Metal bits are extremely bare bones. Its simply the Rack API. You are given a raw request environment hash, and you just need to return a status code, headers, and a response body. There is no notion of a controller or view. Its up the developer to implement the minimal amount of code they see fit.

We hear a lot about micro-frameworks and the niche they fill.  Metal is used in the same breath by Jesse Newland referring to other “micro-frameworks”, we asked Joshua if Metal would remove the need for other micro-frameworks:

No. There is still room for micro-frameworks like Sinatra that make it dead simple to whip up a simple API handler. In fact, you can actually use Sinatra as a metal application. Since Sinatra is built on Rack, it pretty much just works.

Finally, on the subject of how developers could  get Metal:

Its already available now on Edge Rails. Otherwise you can wait for the next gem release of Rails, which will be 2.3.

Rails Metal has gotten a lot of attention with its announcement.  It shows some great promise to Rails applications needing some speed.  More information about Rails Metal can be found on the Ruby on Rails blog as well as at the GitHub repository where Rails is made available.

Rate this Article

Adoption
Style

BT