Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Move Over Rails. Here Comes Merb.

Merb is potentially much faster than Rails, and the combination of Merb and Ruby can provide the performance most web apps require. Find out how Merb can deliver a 2-4X speed increase over Rails and why that makes it better for certain applications.


advertisement
bout three years ago, I spent a few hours building an RSS news aggregator for Ruby news. I wrote it in Java because my one leased server at the time was all set up to host multiple JSP web applications. I deployed the finished product as RubyPlanet.net. Around the same time, I discovered Ruby on Rails (RoR) and have since become an enthusiastic RoR user. For new web projects these days, I typically use Rails (unless I need the speed and scalability of some part of the J2EE stack). But the introduction of the MVC framework Merb may change that.

Merb is potentially much faster than Rails, and with the performance improvements to Ruby (C-Ruby, JRuby, etc.), the combination of Merb and Ruby may well provide adequate performance for most of my new projects in the future. On the eve of the version 1.0 release, this article explains how Merb can deliver a 2-4X speed increase over Rails and why that makes it better for certain applications.

Merb is clearly patterned after Rails, so this article concentrates more on how Merb is different from Rails and on which types of applications are better suited for Merb than Rails. While using Merb is more difficult than using Rails, Merb is touted as a "hacker's framework" and there is a lot of truth in that. With a small kernel (about 6,000 lines of code), it is easier to get into the Merb code base.



The web application I use as an example is a Ruby + Merb replacement for the original Java version of RubyPlanet.net that I wrote three years ago (see Figure 1 for an overview). The new Ruby + Merb version took the same amount of time to write, but it required less code and has more functionality. Less code makes it simpler to maintain and modify.

Thread Safe— Beyond the Speed Boost
Merb has more to offer than just runtime performance. For example, its small core of functionality with plugins and "slices" (complete mini-apps that install in an existing Merb project) does not, in principle, have cross dependencies. Because Merb was designed to be modular, you can include just the plugins and "slices" your application needs, keeping your systems simpler.

Perhaps more importantly, Merb is thread safe and has built-in support for running background tasks as threads. That means deployed Merb applications are likely to require much less memory because you can use work threads instead of extra processes to serve up page and web service requests. Without this feature, you risk duplicating application and framework libraries (if shared libraries are not used) as well as application data when you run many identical processes on a server.

For some types of web applications, using a cluster of Mongrels and aggressive page and page fragment caching provides very good performance. For applications with highly customized page content, a single process with in-memory data caching is better. Because Merb is thread safe, it enables you to build web applications that can better take advantage of in-memory data caching.

Figure 1. Layout of the Merb Demo App: Here is the rough layout of the Merb version of RubyPlanet.net.

If you want to use in-memory caching with RoR instead, you can run a cluster of Mongrels, each with a copy of the cache. However, that eats up more server memory. Alternatively, you can use Memcached to cache common data between multiple Mongrels, but that incurs some communications overhead. For some applications, it is simply more efficient to cache data in-memory and count on multiple threads to support concurrent users.

Because Merb is thread safe and supports running background tasks as threads, I was able to run the entire Merb version of RubyPlanet.net in a single process and use an in-memory cache for active feeds. A single Merb process can process many page requests per second (more on that later).

Figure 1 shows the rough layout of the Merb version of RubyPlanet.net.

Installing Merb
The first decision that you need to make before installing Merb is whether you want to use stable or "edge" versions. While I encourage you to eventually contribute to Merb, in which case you would install nightly developer builds, this article assumes that you are starting with stable builds and using C-Ruby 1.8.6 (I had some difficulties with Merb 1.0RC4 and JRuby but these may be fixed soon).

Begin by typing the following to install a stable version of Merb as a collection of gems:

gem install merb

You will also need the simple-rss and atom gems installed:

gem install simple-rss gem install atom

The simple-rss gem handles both RSS and Atom, but for Atom it returns only the first link element, ignoring the rest. For blogging services like blogger.com, you need all links to get the one with the attribute rel='alternate'.

The Merb core developer team intended Merb to be modular and highly customizable, but for new users they also wanted to provide a ready-to-use experience out of the box. This standard Merb install uses DataMapper instead of ActiveRecord (which you can easily use with a change to the config/init.rb file) and installs and uses the jQuery JavaScript library. Erb is the default template engine (also set in the config/init.rb file).



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap