Ruby On Rails Showdown with Java Spring/Hibernate 555
Paradox writes "Java developer Justin Gehtland recently tried re-implementing one of his web applications in Ruby on Rails instead of his normal Java Spring/Hibernate setup. His analysis of overall performance and application size was startling, to say the least. The Java app's configuration alone was nearly the size of the entire Rails codebase, and Rails application was significantly (15%-30%) faster! At the same time, the Ruby community is abuzz because Ruby is getting a new optimized bytecode compiler for its upcoming 2.0 release. Will Ruby on Rails become even faster as a result?"
Article Text (Score:5, Informative)
So, a few weeks ago I made an offhanded post here about my new-found love for Rails. I'd been skipping off the surface of Ruby for a while, trying to decide if it, or Python, or Groovy, or something else, ought to fill out the empty slot in my tool belt. (I'll save the "why LISP isn't on this list" post for another time.) Rails seemed like an excellent way to put Ruby through a workout, and I had the right sized project to try it out with.
The project itself is not open-source; the client is now and shall remain anonymous. But they are paying me my going rate to do this work, which makes it a commercial Rails project, and it will in the future be installed into some rather large organizations. I can't really say much about what the application's domain is, but I can lay out the general principles of the app.
The application must support multiple users, in the order of 10-100 concurrently. The load isn't particularly high, but the application will be treated like a desktop app (more specifically, a spreadsheet replacement) so it has to be responsive. The application is for managing pieces of a large machine process. There are lots of types of components to be managed, and the relationships between them can be quite complicated. There are no one-to-one relationships in the model, but plenty of one-to-many and many-to-many. In addition to managing the components, the application has to allow for the addition of entirely new categories of components as well as a variety of customizable fields. Finally, the authorization rules call for a breadth of user roles with a complex mapping of permissions to those roles.
I've finally gotten around to running the profiling numbers and doing some comparison between the two systems. I won't spoil the suspense by listing my conclusions up front -- you'll have to scroll through the rest of the post to see them. But, first, let me set the stage: the original application is based on the Java/JSTL/Spring/Hibernate/MySQL stack. I used ACEGI for the security, SpringMVC for the web controller, and was using Hibernate 2.x for the O/RM. To increase performance, I was using the default output caching from SpringMVC and data caching in Hibernate. The re-implementation is in Rails on top of MySQL. The authorization is done through a custom observer and a custom authorization class, which uses a declarative rules file for permission mapping. For performance, I'm using a combination of page caching, action caching, and cache sweepers (observers whose job it is to expire cached pages).
Now, for the comparisons:
Time to Implement
I made a comment about this in the previous posts on the topic, and that comment has been quoted widely out in the wide blogosphere as a classic example of Rails hype. So, let me make it plain: any time you re-write an application, it will go almost infinitely faster because you already have a firm grasp on the problem domain. Such was the case for me in my re-write; we'd spent so much time on the domain model and the database schema that the second time through the application, everything already made perfect sense to me. Any comparison of how long it took to implement one or the other is bogus, since the only fair comparison would be to implement two roughly functionally equivalent projects in the two different stacks and measure the competitive results. Since I have not done that, making statements about how it only took 5 days to re-implement the site are almost meaningless. What I can say is that I had more fun implementing it the second time, but that's just personal preference.
Lines of Code
This one is a lot more interesting. Folks will tell you all the time that there is a running debate about the meaningfulness of LOC comparisons. They're right; there is a running debate. I just think it's moot. For my money, the fewer lines of code I have to write to get a piece of functionality, *as long as those lines are clear in meaning*
Re:Faster? (Score:5, Informative)
Ruby has one other massive advantage over Java on the medium-term horizon: Parrot. The Ruby-on-Parrot effort is progressing nicely, and when that is complete, Ponie [poniecode.org] will give Ruby transparent access to all of CPAN (thought yet another JITted bytecode system) even for those who don't like or can't use Perl.
At that point, Ruby becomes (IMHO) the most attractive HLL in existance (until Perl 6 lands... IF Perl 6 lands...)
Re:Development Resources? (Score:3, Informative)
Mirror (Score:5, Informative)
I couldn't agree more (Score:5, Informative)
Language is merely description, implementation is what you can benchmark.
And I also have to say, what we've got here is the single most intelligent AC post in the entire history of Slashdot.
No need to be pedantic (Score:5, Informative)
That's a matter only of constant speedup, assuming that the implementation is competent, and therefore theoretically swamped by the complexity of the algorithm. But if you've implemented the same algorithm using both systems, a constant speedup of 10 is an order-of-magnitude speed improvement. Even with the really, really fast computers we use today that makes a difference to a lot of applications. (And I've used plenty of languages where could get a 10x improvement by switching to a better language.)
In this case there isn't even a single Java environment; there are numerous JVMs out there and I hope they tested several of them before writing the article. (I doubt it, but that's a whole different story.) You're probably not going to get a 10x difference, but 2x wouldn't be uncommon for certain applications.
So you're using the terminology correctly and everybody else is using it wrong. They should talk about the "Java system" or "Java environment" rather than Java language. But I think nearly everybody reading the article knew what was meant.
Mojavi (Score:5, Informative)
Slightly off topic, but thought some might be interested.
There is a pretty cool and full featured MVC framework for PHP called Mojavi [mojavi.org]. If you like PHP and think Ruby on Rails or any other MVC framework is what you're into, you might want to check it out.
Re:Ruby is a toy (Score:5, Informative)
Re:Security (Score:4, Informative)
Nice FUD... Except that its made up. Rails is very secure by default (uses pepared statements and things like this).
It does whatever it can do to make the framework part of the application secure and even offers a book on security on the hieraki bookshelf for the user side of things: Securing your Rails application [rubyonrails.com] .
This is a great example of rails quality documentation. have a look at the bookshelf itself: Hieraki bookshelf [rubyonrails.com]
And the application which powers it is open source (MIT) at www.hieraki.org [hieraki.org]
This is a flawed recollection. (Score:5, Informative)
No amount of safety can make up for novice mistakes. Rails provides everything you need to make secure webapps, and it lets you do it painlessly.
TFA (Score:3, Informative)
So, a few weeks ago I made an offhanded post here about my new-found love for Rails. I'd been skipping off the surface of Ruby for a while, trying to decide if it, or Python, or Groovy, or something else, ought to fill out the empty slot in my tool belt. (I'll save the "why LISP isn't on this list" post for another time.) Rails seemed like an excellent way to put Ruby through a workout, and I had the right sized project to try it out with.
The project itself is not open-source; the client is now and shall remain anonymous. But they are paying me my going rate to do this work, which makes it a commercial Rails project, and it will in the future be installed into some rather large organizations. I can't really say much about what the application's domain is, but I can lay out the general principles of the app.
The application must support multiple users, in the order of 10-100 concurrently. The load isn't particularly high, but the application will be treated like a desktop app (more specifically, a spreadsheet replacement) so it has to be responsive. The application is for managing pieces of a large machine process. There are lots of types of components to be managed, and the relationships between them can be quite complicated. There are no one-to-one relationships in the model, but plenty of one-to-many and many-to-many. In addition to managing the components, the application has to allow for the addition of entirely new categories of components as well as a variety of customizable fields. Finally, the authorization rules call for a breadth of user roles with a complex mapping of permissions to those roles.
I've finally gotten around to running the profiling numbers and doing some comparison between the two systems. I won't spoil the suspense by listing my conclusions up front -- you'll have to scroll through the rest of the post to see them. But, first, let me set the stage: the original application is based on the Java/JSTL/Spring/Hibernate/MySQL stack. I used ACEGI for the security, SpringMVC for the web controller, and was using Hibernate 2.x for the O/RM. To increase performance, I was using the default output caching from SpringMVC and data caching in Hibernate. The re-implementation is in Rails on top of MySQL. The authorization is done through a custom observer and a custom authorization class, which uses a declarative rules file for permission mapping. For performance, I'm using a combination of page caching, action caching, and cache sweepers (observers whose job it is to expire cached pages).
Now, for the comparisons:
Time to Implement
I made a comment about this in the previous posts on the topic, and that comment has been quoted widely out in the wide blogosphere as a classic example of Rails hype. So, let me make it plain: any time you re-write an application, it will go almost infinitely faster because you already have a firm grasp on the problem domain. Such was the case for me in my re-write; we'd spent so much time on the domain model and the database schema that the second time through the application, everything already made perfect sense to me. Any comparison of how long it took to implement one or the other is bogus, since the only fair comparison would be to implement two roughly functionally equivalent projects in the two different stacks and measure the competitive results. Since I have not done that, making statements about how it only took 5 days to re-implement the site are almost meaningless. What I can say is that I had more fun implementing it the second time, but that's just personal preference.
Lines of Code
This one is a lot more interesting. Folks will tell you all the time that there is a running debate about the meaningfulness of LOC comparisons. They're right; there is a running debate. I just think it's moot. For my money, the fewer lines of code I have to write to get a piece of functionality, *as long as those lines are clear in meaning*, the better off I am. Obfuscated Perl programs don't make the
Re:Wait..... (Score:1, Informative)
PHP is a general-purpose scripting language. You can write shell scripts in PHP. You can write GUI applications in PHP. It started off as web-only, but that hasn't been the case for years.
Hibernate Vs. Ruby on Rails- flame free! (Score:5, Informative)
Re:any comparison like this... (Score:4, Informative)
class XXX "Person",
def self.table_name() "YYY" end
def self.primary_key()
"myprimarykey_id"
end
End
So, you can tell active record what the primary key is for each model, and
you can spell out what foreign keys to use for has_many and belongs_to,
etc., as well as what the table name is.
Re:any comparison like this... (Score:4, Informative)
Notice that Slashdot is written in Perl, with nearly all dynamic pages. According to the FAQ, which may or may not be up to date, they have 8 single processor 600mhz PIII web servers and 1 quad 550mhz xeon MySQL server.
Re:Application (Score:4, Informative)
No, the comparison did not highlight this. The development speed was not comparable because when the app was written in Ruby, it was not the first time the app was written. As such, many development problems had already been resolved. This is noted by the author in the post.
Re:any comparison like this... (Score:5, Informative)
Full disclosure: I don't particularly like the Rails way of doing things with databases, because it bases its data interaction semantics as if MySQL were a good (rather than an "easy") database.
That said:
The assessment on the Rails blog is, I think better. The numbers are impressive but meaningless -- there are tweaks that can be done in any case. The real point is that this shows that Ruby Isn't Slow and that Rails (or any other application environment) can be used in a production environment.
Re:Hibernate too hyped (Score:3, Informative)
We are using Hibernate in our new software. Hibernate has excellent SQL generation. Since it has its own abstraction from SQL, it can do things to optimize your queries in ways you can't really do yourself without considerable effort. Also, I believe Hibernate 3.0 has an equivalent to iBatis (being able to map straight SQL without abstraction), but I haven't tried it yet.
I don't trust/want something to auto generate my sql tables and such based on my objects.
You don't have to auto generate your SQL tables in Hibernate. Create your SQL tables first, and either auto-generate your mappings from the tables (using Middlegen), or hand code them. Or did you mean queries not tables?
Re:any comparison like this... (Score:3, Informative)
In the model for the class:
def first_name
fname
end
There might be a better way to do it, but that works.
Spoke to Justin about this... (Score:5, Informative)
At the Milwaukee No Fluff Just Stuff conference, I was invovled in a lunchtime conversation with Justin and [Pragmatic] Dave Thomas [pragmaticprogrammer.com] about this subject, just days after Justin completed the Ruby code.
The concensus at that point: it probably wasn't a difference in *execution* speed, but smarter data retreval strategies used by Rails persistance layer. While Hibernate has excellent support for lazy loading, both developers thought that Rails was being *lazier*.
Justin's new numbers also point to faster caching in RoR's persistance layer: while both applications performed about equally without pre-cached data, RoR performed 20x better than the Java stack with cached data [both versions using similar caching strategies].
As for those questioning Justin's java skills: he's one of the best programmer's I've had the privilege to know, one of the best speaker's I've listend to, and is freaking hilarious to boot. He's the co-author of O'Reily's Better, Faster, Lighter Java [oreilly.com], and he regularly speaks on advanced Hibernate, Spring, and a bunch of other Java topics.
He also points out a *significant* decrease in Lines of Code[Java:3293 RoR:1164] and Lines of Configuration [Java:1161 RoR:113]. While not an accurate gauge of effort, it is another point in Ruby's favor.
Last point for Ruby: Every single *top notch* Java programmer I know is at least playing with Ruby and RoR, with a large percentage [>50%] transitioning to Ruby as a first choice for new project work.
Don't call it a toy until you've played with it. There's some pretty convincing evidence that Ruby/RoR can beat Java for development effort, and now we're seeing it can beat it for performance, too.
Re:Application (Score:2, Informative)
Except that they were not written by the same people, so they both had to do the same effort of analysing the application. Also the features are quite different, for instance:
So there's a lot more to compare than just a screenshot that doesn't do anything constructive and just tries to hype a concise part of the RoR version.
The fact that Ta-da took 600 loc and Bla-bla 900 seems to be totally ignored by Rails advocates and they all keep chanting 'that screenshot says it all'. No it doesn't, since the loc factor is 2/3, not 1/14. Ta-da is probably just more verbose elsewhere, but that's impossible to compare since it's not open-source.
Re:Hibernate too hyped (Score:3, Informative)
iBATIS SqlMaps [ibatis.com] is my persistance *assistance* technology of choice, and have been for two years.
Notice *assistance* - it is not *meant* to compete with ORM solutions like Hibernate, which is why I use it. Ted Neward expresses it best: "Object-relational mapping is the Vietnam of Computer Science" [neward.net]
My take? Relational databases are procedural in nature, not object-oriented. Get over it, and stop trying to pretened otherwise! iBATIS SqlMaps makes writing relational database "method calls" very easy, and stays mostly out of the way.
Re:looks interesting, but does it have to be ruby? (Score:3, Informative)
As far as the OP's question, no, there's nothing at all in Python the language that'd prevent a similar project, although the implementation would be pretty different.
Re:Hibernate too hyped (Score:1, Informative)
"I don't trust/want something to auto generate my sql tables and such based on my objects"
You can generate tables from your objects, or generate your objects from your tables. Either way is possible with Hibernate. Personally, I create the tables and generate the objects from them.
"You put your sql queries in a separate .xml file and use them with very few lines of code. It maps the results of the queries to your javabeans."
Hibernate does the same. In fact, I did that very thing yesterday. You put your named queries in the mapping file and reference them in your code. Works beautifully.
Re:Mojavi (Score:1, Informative)
Admittedly, however, I learned a whole bunch of things about php that I never considered before playing with mojavi. I think if I were to build an app which I knew I would be the only developer that would have to revisit it in the future, I would do it in mojavi, but it is sort of overkill for most small projects.
Now, if only that zestic cms built on mojavi was done....hehe.
Java Trails is the Java version (Score:3, Informative)
https://trails.dev.java.net/ [java.net]
Re:looks interesting, but does it have to be ruby? (Score:3, Informative)
Oh, no question. It's the one thing that Ruby has that Python doesn't, and it's one that many developers (myself included) would love to have.
Re:looks interesting, but does it have to be ruby? (Score:3, Informative)
Well, this is just silly. I'm not a Python expert by any means, but even I know this works:
def func():
def my_code_block():
print "Hello, World!"
return my_code_block;
f = func();
f();
What Python doesn't have is nice, clean, anonymous code blocks (ie, for anything non-trivial, you have to name them).
Re:No need to be pedantic (Score:4, Informative)
It is possible that 10.4 will finally change this and really make OS X a first class java citizen, we'll see. Hopefully the better compilation from gcc 4 and 64 bit everything will help substantially. Good vectorization, and an auto-vectorizing bytecode compilier would go a long way towards fixing this embarassing little weakness. Also, including a -server VM wouldn't hurt either, if Apple wants their computers to be taken seriously in the Datacenter (Xserves).
Re:Sure. (Score:4, Informative)
I mean, if caching was the only thing holding Rails afloat, you could draw this conclusion, but even a cursory examination of TFA wills show that's just not true.
Making secure actions in Rails is trivially easy. There is even a generator for it. Rails developers are concerned with security, but they've yet to see a scenario where the standard login generators and practices of the rails community require a technical fix.Yes, you could bring up Geert's posts point out flaws in Ta-da Lists, but that'd be silly. Every application can have bugs, and Ta-da Lists was ripped wholesale out of Basecamp, reseated, and used mainly as an Ajax demo and advertisement for basecamp.
This is no longer true. It was true at one point. Yes, many features go into Edge Rails that are MySQL only, but that's just because the developers tend to know it best. Features don't end up in the mainline unless they're as compatible as people can make them. I cannot deny that some databases are not as well supported as others, though. I think you formed your opinions before the 0.10.0 release. We're closing in on Rails 1.0 now, and things are starting to get really stable.Re:sincere newbie asks.... (Score:1, Informative)
http://en.wikipedia.org/wiki/Python_programming_l
http://en.wikipedia.org/wiki/Ruby_programming_lan
They are both good languages, with useful features and libraries. If you've never tried either before, just take your pick of which one seems the best from a tutorial. They have some overlap with what type of stuff you can do with them (webapps, command line scripts, administration automation, etc).
Ruby on Rails and Zope are different web application frameworks. Zope is written in Python and you use Python to program with it.
With Ruby on Rails, you can get up and running pretty quickly. Zope has a bigger learning curve, and some would argue it is more powerful. That's really a matter of personal taste.
Ruby on Rails uses the "Model-view-controller" approach to software. This is a traditional way to do things, and IMHO Ruby on Rails does a good job of implementing it for web applications.
Re:looks interesting, but does it have to be ruby? (Score:3, Informative)
The real difference between Python and Ruby is that Python doesn't support true lexical closures. As such, it's not possible to duplicate your example, as you can't rebind the outer variables in Python... an annoyance, to be sure, but certainly not fatal, and it definitely doesn't prevent one from creating a Rails-like system in Python. In fact, it can be argued that Python's way is safer... fewer side-effects.
Incidentally, I have to say, Ruby's idea of having 'yield' execute an implicit codeblock (as opposed to just passing the damn thing in explicitely) is one of the most braindead things I've seen in quite some time... talk about confusing code. I mean, if you're going to copy Smalltalk, at least do it right... yeesh...
Trails Video (Score:3, Informative)
Re:Application scaling (Score:3, Informative)
The best solution for this is memcached. It means that all sessions will be in memory and therefore blazingly fast accessible to anyone in the server farm.
The database is a excellent second choice as well. I usually go with db based sessions first for convenience and move to memcached as soon as it is required. As always, dont try to make your app scale before you need it. I know its fun but just wait, you will have a much better idea where the bottle neck is when you hit one.
I know this is a trick 99% of all projects never need to run across more then one box, never mind what management says.
Re:any comparison like this... (Score:3, Informative)
Rails assumes that each model is one table. In the database, the table can also be a view, but you have to add capabilities in the database to update/insert into that view if you want that functionality.
Mappings should be done in the databases - that's what they are there for.
Re:Ruby on rails performance (Score:3, Informative)
OS X uses Sun's JVM code which is tuned by Apple with some of the tuning being returned to Sun. Perhaps your thinking of the OS 9 JVM?
Re:Sure. (Score:4, Informative)
The Oracle abraction for active record has a way to specify the sequence name, but the postgres one doesn't I hope that this will eventually be rectified.
Re:"Normal Spring/Hibernate" (Score:3, Informative)
Because... (Score:1, Informative)
Re:Zope 3 (Score:3, Informative)
Zope 3 is really targetted at J2EE-like functionality. Rails is based on a You-Ain't-Gonna-Need-It philosophy -- it provides significant base functionality and a set of conventions, but the framework is not all-encompassing or intended to match J2EE feature-for-feature, nor as a framework does it really mean much outside of the web. This is by design.
Zope 3 is really a whole design philosophy, kind of like J2EE. It makes heavy use of interfaces and adapters, which are intended to bring an increased level of formality to the development process. But it's not just an attempt to put static typing on top of a dynamic language; the techniques they use are fundamentally dynamic, and an attempt to utilitilize those formalisms as basic programming constructs. This is embodied in the idea of "adaptation", where instead of requiring an interface, you ask for a version of an object that implements the interface, and then you provide wrappers ("adapters") to translate interfaces.
There's other ideas in there too, generally built upon these basic programming ideas. It's a framework of frameworks. It's also heavy on the idea of modeling, and of an applications that provide a window into your models. The entire scale and concept is very different from Rails. It's actually a little foreign to the Python community too. A lot of Zope 2's ideas were also foreign to the Python community, but the problem is that they were also uncomfortable and put people off -- I don't think people are uncomfortable with Zope 3 as much as unfamiliar.
(Also, Zope 3 is a complete rewrite of Zope 2 and is very different, so most of people's experience of Zope 2 -- good or bad -- can't really be applied to Zope 3)