I have a very basic question: Why did Twitter choose Scala rather than Groovy to switch from Ruby? I think using Groovy is easier for the Rubyist or Javaman than Scala. Thanks.
3 Answers
While only Twitter can answer that, you're essentially asking the wrong question. You should be asking yourself what business or technical problem made Scala useful to Twitter.
In fact, if you read about Twitter's integration of code in Scala, you'll see that they didn't just toss out Rails; they built systems to support a portion of their application in Scala and refactored existing code to talk to services built in Scala.
At some point, their primary technical concern ceased to be about the web app itself and became more about messaging and notifications. Groovy and Grails wouldn't have helped them solve that problem much better (or perhaps ANY better) than Rails. Scala, and other functional languages, make it easier to reason about highly concurrent problems by minimizing mutable state. It provides an actor model for concurrency problems that makes it easier to scale certain categories of applications to multiple processors and multiple servers.
This is essentially the reason why you should have more than one tool in your repertoire. Some problems aren't as simple as storing data and presenting it. You can get pretty far with pure Ruby tools, but there are still some practical limits when dealing with problems that either need threading or distribution across multiple machines.
They might as easily have chosen Erlang or even F# given a different set of team members or motivations. But another web framework would have likely provided little benefit at a far more substantial cost when their problem wasn't really at the front end.
-
Thanks for your explanation, Twitter choose Scala because it focus on Realtime UX.– setyoCommented Apr 19, 2011 at 8:24
-
As time has proven, their intent WAS to toss out ALL Ruby code. It is clear now that this answer was wrong. Commented Oct 13, 2013 at 22:12
Maybe you can find your answers here: http://www.artima.com/scalazine/articles/twitter_on_scala.html
Paragraph "Reliable, high performance code" catches it pretty well :-).
Bill Venners: I’m curious, and the Ruby folks will want it spelled out: Can you elaborate on what you felt the Ruby language lacked in the area of reliable, high performance code?
Steve Jenson: One of the things that I’ve found throughout my career is the need to have long-lived processes. And Ruby, like many scripting languages, has trouble being an environment for long lived processes. But the JVM is very good at that, because it’s been optimized for that over the last ten years. So Scala provides a basis for writing long-lived servers, and that’s primarily what we use it for at Twitter right now. Another thing we really like about Scala is static typing that’s not painful. Sometimes it would be really nice in Ruby to say things like, here’s an optional type annotation. This is the type we really expect to see here. And we find that really useful in Scala, to be able to specify the type information.
Robey Pointer: Also, Ruby doesn’t really have good thread support yet. It’s getting better, but when we were writing these servers, green threads were the only thing available. Green threads don't use the actual operating system’s kernel threads. They sort of emulate threads by periodically stopping what they are doing and checking whether another “thread” wants to run. So Ruby is emulating threads within a single core or a processor. We wanted to run on multi-core servers that don’t have an infinite amount of memory. And if you don’t have good threading support, you really need multiple processes. And because Ruby’s garbage collector is not quite as good as Java’s, each process uses up a lot of memory. We can’t really run very many Ruby daemon processes on a single machine without consuming large amounts of memory. Whereas with running things on the JVM we can run many threads in the same heap, and let that one process take all the machine’s memory for its playground.
Alex Payne: I’d definitely want to hammer home what Steve said about typing. As our system has grown, a lot of the logic in our Ruby system sort of replicates a type system, either in our unit tests or as validations on models. I think it may just be a property of large systems in dynamic languages, that eventually you end up rewriting your own type system, and you sort of do it badly. You’re checking for null values all over the place. There’s lots of calls to Ruby’s kind_of? method, which asks, “Is this a kind of User object? Because that’s what we’re expecting. If we don’t get that, this is going to explode.” It is a shame to have to write all that when there is a solution that has existed in the world of programming languages for decades now.
-
-
3But the question ask whey there was a move to Scala and not
Groovy
.. I don't think the OP was asking why Ruby was abandoned in favor of Scala. Commented Jan 30, 2015 at 11:22
In the hindsight they may present it as a rational decision, but we know... Scala was just a cool new toy.
There cannot be any rational reason to choose new, unproven, untested in production system for your implementation.
Developers are famous for being inconsiderate to risks. These guys got lucky. We do not hear of those who wasn't though.
-
2Yeah, yeah... downvote. :-) Like it would change the fact: Ruby couldn't keep up, Scala was cool, Java was boring. The choice was not rational, whatever they say now. Commented Apr 19, 2011 at 7:05
-
11Scala was several years old when they adopted it, and it is definitely more than a new toy. Sometimes there is a reason for a hype. (BTW: I'm not the downvoter, everybody should have the right to have his own miscon... err.. opinions)– LandeiCommented Apr 19, 2011 at 7:06
-
3As a professional Scala developer I still find your comment funny :-) Early adopters have hard days, I am sure. As for now (2011) I do not think of myself as an early adopter anymore. Commented Apr 19, 2011 at 9:15
-
2How is this supposed to be an answer? At the very least it's not useful. Commented Apr 19, 2011 at 14:29
-
6Claims of this nature need more evidence to back them up. They do address the "cool new toy" aspect in the article, but it also seems to have helped their performance problems. So you need some supporting evidence to show that they could not have known in advance that it would work, and they just got lucky. (Note: being able to fall back on Java-style programming is not part of "getting lucky", since Scala is explicitly designed with that in mind.)– Rex KerrCommented Apr 19, 2011 at 18:09