Twitter is Slow, but not because of Ruby
Jeff Atwood commented on the Twitter scalability problem and blamed on Ruby’s slowness. I have quoted from Coding Horror a few times (in my other blogs as well), but I still do not get how his opinions can be so highly regarded in programming community, when he cannot even distinguish between performance and scalability. Even he himself admitted that
To be fair, it sounds like most of Twitter’s problems are database problems, so maybe it doesn’t matter what language they use”
Even Alex stated it clearly in the Twitter interview that database has been the bottleneck, just like most share-nothing web development platform that was designed to be highly scalable in the first place! Ruby is slow. Python is a tad better, but in a heavily IO bounded web-based application, both will be more than fast enough. How to scale the database? It is not trivial and very domain specific, and that is what Alex is complaining about.
Comments
Huh? Are you commenting on the right post?
I am not a Rails fan. I don’t even code in Ruby. I have even criticised DHH several times here and in my other blogs. There is only one point in my little “opinion” here, which is — you simply cannot say Twitter is currently experiencing slowness because Ruby is a slow interpreted language.
And I full understand what Alex is talking about. A framework can get you from 0 code to a workable product *very fast*, but a framework can also restrict you in how you can optimise/scale the architecture. Scaling a database is not trivial, and database engines, commercial or open source, do not come with a “Go Faster” and “Scale Horizontally” button that work in every application! As I have said, it is very “domain specific”, and I doubt a generic framework like Rails would take care of it. Take splitting the DB for example — how would a framework know where to draw the division?
Maybe Alex is saying that RoR did not provide enough hook to make custom DB backend possible. I don’t code Rails so I would have no idea. But one thing for sure — it has nothing to do with Ruby being slow running a tight loop.
And do you think replacing Ruby with C# in Jeff Atwood’s example WITHOUT touching the DB would solve Twitter’s scalability issue? Get real! I don’t think so.
Actually, I was blaming the *platform*, hence the title of the post– service vs. platform. The language is part of the platform, but it’s much more than just a language.
It’s the same reason Reddit switched from Lisp to Python. Not because of the language per se, but because the platform around the language was so limiting. Look that one up if you’re curious, it’s interesting reading.
Also, I’m as mystified as you about Alex’s comments on the language speed. He’s the one who brought it up, and actually harped on it quite a bit with the Python comparison.
Jeff — thanks for the response, and I have read your latest post on the battle of platforms. However, as others have pointed out on your blog, that platform might dictate the language you can use, but there are usually more than one platform available on one language. Bashing the language still does not prove how scalable a platform is, as scalability of a platform is usually an architecture issue, and usually has little to do with the language.
As of Reddit migrated from Lisp to Python — why they programmed in Lisp in the first place is still questionable. Buying too much into what Paul Graham has said? Moreover, they are not switched to .NET, C++ or Java, the languages that “people should use when they cannot afford giving up performance”, from the way I interpreted your post. They switched to Python (with Aaron’s hack’ish web.py) because of all the batteries included.
Scott,
I think your comment about one size fits all from a database scalability stand point is pretty much on the money, until you walk into the world of Oracle. As soon as you start clustering through RAC, it pretty much is one size fits all as you can keep flopping in more disks into a SAN and more nodes into the cluster. That being said, no start up could afford enterprise Oracle licensing, so I guess in this discussion its a mute point.
Regarding the language being slow, anyone thinking that Ruby/Python being the limiting factor in a database intensive site are kidding themselves. Lets assume an average 10ms response time for a single query, you can do an enormous amount of work in any programming language in 10ms – you’ll be waiting on disks/locks far more than the server side language to process something.
Would switching to .NET make a difference, sure it would. It would reduce the CPU time on their web servers, so they’d be able to serve more requests per second. Unfortunately, from what they’ve said – that isn’t their bottleneck at the moment, its database so although moving into .NET would yield better performance, right now it isn’t going to solve their current problems.
I don’t know enough about RoR, however I would have expected that it comes with a comprehensive set of caching utilities like Django does? I believe Django currently comes with file system caching, local memory caching, local/remote memory caching (memcache) – which as far as I understand, allows you to cache as high as the raw HTML output and right down to an individual object. Do you suppose Twitter currently have caching patterns in use?
Al.
Scott, I agree with your point in the comment below, I guess your sentence regard a shared nothing platform threw me off (causing me to go off on a contra-DHH rant – my apologies!)
I agree that a true shared nothing framework would help in a situation that twitter is in. Speed of Ruby isn’t as critical either, agreed again with your and Jeff’s point, when you are not in a straightjacket that is.
The point I was trying to raise was, RoR claims to be shared nothing, but for that to be true, the DB tier has to scale horizontally as well. The DB architecture would have to scale as well. I don’t think it is a trivial case, but for a system to claim “shared nothing” status, it ought to apply that to the DB tier as well. So, by default, Rails cannot claim to be “shared nothing” and yes, Ruby’s speed has very little to do with it, I was treating that point as a given.
The problem is that all that I/O has to be routed through the database, and partitioning your DB horizontally or vertically or Horiz/Vert + Read and Write pools is difficult with Rails even if you want to do it. By Default you start out with one DB, and since Rails happens to be an “opinionated” framework, whatever that means, it does not offer a natural path to this partitioning of Data on the DB end in a graceful manner.
I’m not sure how exactly Twitter is architected, but hopefully they are looking at a high performance messaging backend which speaks to various different kinds of clients, a Rails UI server being just one of them. I think the issue is that they are trying to handle all these messages using Rails and the framework is just not suited to that kind of soft-realtime requirements. Just because Twitter started out as a Web app, and happens to have a web interface doesn’t mean it is a web app, it is a messaging app with a web interface, but it also has to speak to IM, Twitterrific, and ad-hoc API calls. Maybe a Pure Ruby backend would offer a better performance profile, but to do it all through the layers that rails routes these packets through is just asking for trouble.
I would suggest that the backend architecture be broken down in two logical parts. One, lets call it Message Handler (MH) does nothing but message handling and routes them asynchronously to a DB store, and the other communicates MP (message Processor) with the various “internal” clients, one of whom happens to be a Rails frontend serving their website. The others maybe IM handlers, Mobile handlers etc. It might be better to write the MC and MH parts in Erlang perhaps while the web UI is handled by the Rails frontend which talks to the MC via a specialized “DB adapter”, ie; the MC exposes itself as a DB server. I have read somewhere that The Twitter team is using ejabberd in some capacity, but maybe it is only to talk to all the IM and Mobile clients all the while passing the info back to the Rails portion which commits new info to DB and reads back stored info.
I couldn’t think of a worse platform than Ruby on Rails, to write the messaging portions (MH + MC) in. While it is probably the one of the better ones to write the web front-end in.
@Alister — sorry last time I worked on projects involving Oracle (around 7-8 years ago), they were pretty slow, even on “beefy” boxes like a few UltraSparc E450 we’ve got. However I have no doubt that Oracle can be very scalable (provided that your bank account is very scalable as well :)
@GravatarIsused — sorry about being “straightjacket” (or whatever that implies). I can assure you that I am a nice person in “real life” :)
I agree with what you say. RoR will never be able to provide a complete end-to-end solution to a busy web application. Message queue is pretty much essential for anything that is even slightly complicated to ensure the front-end don’t stall on handling the request.
But we are talking about scaling the architecture here. RoR (or PHP) might make you think that “creating a web app is so easy!” but I do not think that they are at fault when the developers failed to re-architect the app to make it scalable.
Add a comment
Gravatar is used. Email address is required but will not be displayed. Please keep your comment on topic. No spamming and/or bad language. First time poster will be moderated. Scott reserves the right to delete/edit your comments.

How come this “highly scalable” framework can’t scale on the DB end? And you are partly correct, performance and scalability are different by in-extricably intertwined. True “shared nothing” offers some relief, but if you can’t scale your DB tier or can’t separate reads from writes while caching on the app->web pair, you’re in for a lot of pain.
The point Al3x is making is not that it can’t be done, but that the choice of framework makes it difficult to do it readily, and what is needed is time (not money or hardware). Rails makes it difficult because of retarded assumptions cloaked in an even sillier mantra of being “opinionated”.
Their basic response is, we’re opinionated, our opinion happens to be X at the moment, if you’re real world requirements are not X then too bad. It is sad to see a really great Rails adapter being attacked by sleazeball marketroids like DHH, because he dared to point to some issues publicly.
DHH’s response has actually been couterproductive. A VC looking at this will now think twice before committing their money to a startup using this framework. Mind you, VC’s are looking for this high-usage so the problems are very real, and very taxing on the bottom line.
Rails diminishes profit taking, it gives you a great site in 3 developer months, and how you to deal with the 3 sysadmin/dba years along with 10 times the servers.
What is the point of decreasing up-front cost when you’re backloading the total project with recurring costs of hardware/software maintenance? It is sad to see that most developers and rails fans cannot see this real concern.
Talk about “getting real” .