When to Ruby on Rails, when to Node.js

(update) Take this post as a naive overview, it may not reflect the most accurate reality

Hello!

I am trying to do a sort of indirect comparison between Rails and Node.js. The very main reason of being indirect, is that Rails is a Framework, while Node.js is a runtime with custom libraries.

If it were be to put in a simple phrase, Rails is resourceful and Node.js is light and fast.

Lets elaborate some more..

Rails

Is the most complete open-source framework available (that I know of)! Big companies use it. It can do lots of stuff well, in an organized manner; this meaning, Rails is more than just MVC, it has a full stack of features well-integrated, at the same time being very modular. Some of the features included out of the box:

  • Database adapter for the majority of them, supporting plug your own.
  • Database migrations, so multiple dev can sync and experiment with their DB.
  • Powerful engines for Views, Controllers and Models.
  • Support to code generator.
  • Has structure to all sorts of tests and friendly to TDD.
  • Really awesome documentation.
  • Model has all kinds of hooks, validations and associations.
  • Controller has support to handle XML/JSON in the same action that serves HTML.
  • Gems that integrate, for instance, Memcached, MongoDB, Auth and lots more.
So Rails is war-proven, capable of integrating lots of features together without harass. There is also a very cool post of Fabio Akita in the refs. about how it made possible to develop systems in periods before impossible.

Node.js

Two things make this platform suitable for web:

Its engine, V8 is very fast! In a very loose average, 8 times faster than Python (or even up to 200 at peak). Python already outperforms Ruby (ref. bottom)

Second point; and this argument is separated from the above, is that it async-driven (is built around reactor pattern). As in the case, requests can be performed in parallel, without any blocking I/O. A single server can handle a lot. (update) And with >0.6.0 Cluster API, it can scale to use all of available CPU cores.

So, it is a very new sort of backend language, but huge players, besides Joyent, who invented it, are adopting it, including LearnBoost and LinkedIn, which has an awesome article about using. The language, and it’s main web framework, Express, deserve a list of features (you can check more info in the references below).

  • It´s web server is able to handle a HUGE number of connections out of the box
  • Various libraries can be run on browser, the same as in the server
  • Very friendly to Websockets (real-time web apps)
  • Lots of libraries are being ported to it from other langs.
  • Express, inspired in ruby´s Sinatra; is very light on memory but also very powerful
Running a simple benchmark against a single server instance, I were able to get 587 req/s accessing MySQL without any external cache support. This number could scale, if I used Cluster to spawn at least a process per processor.

Summarizing, When to use each?

Rails really shines when..

  • The database is complex in terms of associations.
  • The app structure is well defined.
  • Business rules are complex, and validation is needed.
  • When the number of requests isn´t the a decisive factor.
  • Administrative interfaces.
  • Many developers in parallel keep the DB up-to-date with migrations
  • The database to be used is undefined, or may vary.
What about Node.js?
  • APIs
  • Real-time web/mobile apps.
  • Application that should scale to lots of concurrent requests.
  • Little memory footprint

This being said, there is no reason at all, a web-site or service can´t easily integrate both.

— I’d appreciate if you could leave a comment, either to talk about your case, or add up.

References

 

UPDATE: http://www.mikealrogers.com/posts/a-new-direction-for-web-applications-.html

http://guides.rubyonrails.org/

http://railscasts.com/

http://blog.heroku.com/archives/2011/6/22/the_new_heroku_2_node_js_new_http_routing_capabilities/

http://nodejs.org/

http://akitaonrails.com/2011/04/16/twitter-muda-de-ruby-para-java-ruby-e-3x-mais-lento-que-java

http://venturebeat.com/2011/08/16/linkedin-node/

http://blog.bossylobster.com/2011/08/lesson-v8-can-teach-python-and-other.html

https://github.com/LearnBoost

http://www.readwriteweb.com/hack/2011/01/how-3-companies-are-using-node.php

http://twitter.com/#!/FlockonUS/status/104655096956190720

https://github.com/LearnBoost/cluster

Advertisements

20 thoughts on “When to Ruby on Rails, when to Node.js

    • Thanks for commenting!

      I guess the conclusion greatly varies from the use case, what would be yours?

  1. Node, I choose you!
    Teh internetz use Thousands of Requests!
    Node reply in 1 second!
    It’s super effective!

    That’s the point, right?

  2. Pingback: Node Speed Vs Rails Elegance « Ruby On Rails Blog

  3. In you opinion, how do you think NodeJS would communicate with Rails’ Models in case you need to ? or you just connect to the same database and replicate the models ?

    • As I understand the question, you would like to use Rails goodness as association, validation and/or filters, right?
      You could set your Rails controllers to serve JSON for your Node.js app, and how it would be consuming. There may be significative latency on this process.
      You have to think if there are significative performance constraints and if it is worth at all.

      • If I understand correctly, what you are saying is to use Rails as an API and every time a request is handled by Node.js it requests Rails controllers for data ? (here’s where I think the latency on the process you are talking about appears). Maybe I should think of a design where Node.js is only used in certain cases and not all the process of the app.

      • Yes, you got it! And yes, that would be quite a lot of overhead, in fact, if there is no API setup in Rails the consideration would probably be pretty bad (unless you really have a ton of logic in ruby and fast response and throughput would not be required to justify it)

  4. Hi.. Went through the article and I must say its damn helpful! One thing that stands out is that you said we can use both.. Can we have references on using both together? Like for example, say I want to create a rails app with normal functionality and then use Node.js for a different functionality say chatting or something which asks for swifter response?

  5. Perfect overview. I thought I’d add that there are a few more Node.js frameworks which add Rails-style functionality for rapid development and robustness.

    Check out:

    Meteor: http://meteor.com/
    Derby: http://derbyjs.com/
    Sails: http://sailsjs.com

    Of course, my personal favorite is the one I created, Sails. We’ve been using it in production on client projects since last March, and it’s rapidly becoming more and more robust.

    Sails is built on Express (discussed above) and adds the minimal set of features necessary to build awesome apps in the style of Ruby on Rails. It doesn’t include any front end requirements like Meteor or Derby– in my opinion, that “magic” can cramp your style and prevent you from getting up and running (and on your timeline) quickly.

    Sails boasts an automatically generated JSON api for your models, database adapters (more being developed all the time, with the ability to write your own), and most of the other features you expect from Rails, only for Node.js. Sails also knows how to gracefully handle WebSockets– it uses Socket.io to reuse the same controllers you’re using to handle HTTP requests over sockets– automatically!

    Anyways, if you’re interested in trying Sails out; or even better, if you want to contribute 🙂 drop me a line!

    http://github.com/baldedashy/sails

    • Thank you, Mike.

      This article is old but a few bits can still considered contemporary.

      Your framework seems pretty cool, and what seems really attractive specially for the ability to re use the controllers for websockets! Last time I was interested in the topic, about 1 year ago, there was no easy way to manage web sockets connections. Not sure how Meteor does it, but I am highly biased against it anyway.

      Currently I am developing a scalable reusable solution for real-time games, non oss, let me know if it interest you

  6. Nice Comparison 🙂
    Still having some doubts on my mind !
    I have to build a full e-commerce website for crowdFunding domain (just like kickstarter.com), which path should i be taking ? node.js OR rubyOnRails ???

  7. very succinct article on the topic . Agreed with conclusion! Mostly nodejs based architecture perform well when we have to dealt less with complex database schema. Mostly, thin application layer perform well with node because of single threaded async way of doing stuff and lesser memory footprint.

    Again thanks for the article .

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s