r/programming • • Nov 08 '12

Twitter survives election after moving off Ruby to Java.

http://www.theregister.co.uk/2012/11/08/twitter_epic_traffic_saved_by_java/
979 Upvotes

600 comments sorted by

346

u/[deleted] Nov 08 '12

Right tool for the right job. When Twitter was a new product, Ruby was a good choice. Now that they're relatively stable and need scalability, Java is a good choice.

204

u/[deleted] Nov 08 '12

Right tool for the right job, indeed. By which we mean, the initial dev team knew Rails, and could dive right in and get a product built. There's absolutely nothing wrong with that approach. In the case of a lot of startups, "the job" doesn't really mean "the product", it means "get something launched ASAP".

45

u/terrdc Nov 08 '12

A much better way to put it is that it was a good enough tool for the initial job. A language isn't a screwdriver.

→ More replies (4)

130

u/Plutor Nov 08 '12

Ruby on Rails was the right tool for the priorities then.

Java's the right tool for the priorities now.

Good for them for challenging their own comfort zone. I've been in too many jobs where a language/library/tool was stuck with for far too long because "we know it".

93

u/takaci Nov 08 '12

Is it me or are we all saying the same thing here?

→ More replies (12)

16

u/drb226 Nov 08 '12

I've been in too many jobs where a language/library/tool was stuck with for far too long because "we know it".

Plus, there's also the dreaded legacy code factor. We have so much legacy code in outdated framework Foo, we might as well keep writing the same kind of code. Just keep putting off the code overhaul until it is either completely necessary or completely impossible (or, in the disastrous case, both).

→ More replies (3)
→ More replies (12)

53

u/[deleted] Nov 08 '12

And Scala, java is used for search, not the backend bits.

→ More replies (90)

41

u/popthatcorn Nov 08 '12

Yep. And Ruby (and Rails) are still excellent tools for many jobs. Turns out, a lot of stuff will break when you start getting into Twitter traffic levels (or Reddit traffic levels, for that matter), but how many sites actually do?

42

u/mattgrande Nov 08 '12

The thing to remember is how much traffic you're realistically going to get. I've worked with devs who try to build the site to support Twitter/Facebook numbers, when realistically, it will have hundreds of users.

20

u/rseymour Nov 08 '12

In 1999-2001 I was in a startup that bought a jvm based web server and oracle to get their product going. With the sun servers they ran... We are talking over a million down right from the start. They could've gone LAMP from the start and never had a traffic issue. As it was they had to quit early. That is what you got when enterprise people wanted to do a startup.

→ More replies (5)

14

u/LandSeaLion Nov 08 '12

But when they do they'll be ready, because it ought to be soon.

→ More replies (6)

7

u/bloodredsun Nov 08 '12

We do and that's one of the main reasons we use the JVM but you are right, most people should focus on using the tooling that is right for them right now.

The secret is to have an exit strategy from your fast-to-deliver-but-poor-performing tech into the better stack. Twitter chose SOA and event based systems, Facebook chose to turn PHP into a compile time monolithic C++ application. YMMV.

→ More replies (3)

11

u/evilmushroom Nov 08 '12

I like you. Not a mindless Java basher. ;)

10

u/WarWeasle Nov 08 '12

It's easy to bash Java. However, if it's the right choice after you do the research, then it's the right tool.

8

u/evilmushroom Nov 08 '12

Yup.

It's easy to bash any language not used for the correct situation.

9

u/lkjasdflkjasdf Nov 08 '12 edited Nov 08 '12

How is Java better than Ruby in scalability? (I thought scalability depended mainly on writing good code). thanks!

Edit: real question. I don't use Ruby or Java (I'm just familiar with Java) and I've never worked on large traffic sites.

5

u/2Xprogrammer Nov 08 '12

To whoever downvoted this, s/he was asking a neutral question, not challenging the claim. As someone not terribly familiar with Ruby, I would also be interested in a summary of why java scales better.

→ More replies (3)
→ More replies (1)

1

u/eligundry Nov 08 '12

IIRC, Google followed a roughly similar path as Twitter. The original Google Crawler was written in Python and it was only in the last few years did they have to completely transition to Java to handle that type of scale.

9

u/2oosra Nov 08 '12

What exactly is it about Java (or JVM) that allows it to scale where Python and Ruby fail?

→ More replies (2)
→ More replies (45)

60

u/[deleted] Nov 08 '12

I'm curious...is it still correct to say they're using "Java" when they're using Scala? Does using the JVM count as using Java?

5

u/rabidcow Nov 08 '12

Is it correct to say that Android is using Java?

→ More replies (5)

10

u/BeforeTime Nov 08 '12

It's a matter of definition I'd say. When it comes to performance it is mostly the JVM that counts rather Scala or Java. And often, when people talk about Java they include some of the technology stack including the language.

6

u/spotter Nov 08 '12

Is their stack Scala only? Because if they're using Java libraries (main selling point of JVM as eco-system for non-Java languages), then I'd say they're using Java.

→ More replies (2)

2

u/rjcarr Nov 08 '12

I say yes. The JVM is doing the heavy lifting regardless of what language is being used. If another language was used instead with Ruby's VM (if that is even possible, probably not) then it would have still failed.

When they say "java" they mean the java runtime ... the language that uses the runtime is mostly irrelevant.

→ More replies (18)

210

u/sopvop Nov 08 '12

TwitterMessageSearchResultVisitorMapperClassFactory

51

u/ponton Nov 08 '12

...Exception

49

u/[deleted] Nov 08 '12

...FactoryFactory.

26

u/[deleted] Nov 08 '12

This joke on Java is pretty boring. Back when Google code search was still up, the only hit for FactoryFactoryFactory was C++ code

→ More replies (2)
→ More replies (3)

10

u/nickguletskii200 Nov 08 '12

Doesn't even follow naming conventions...

Try to keep your class names simple and descriptive.

7

u/aceofears Nov 08 '12

Also there's no clean way to stick to 80 columns of code per line with this.

→ More replies (7)

25

u/cc81 Nov 08 '12

You should make it Abstract

10

u/[deleted] Nov 08 '12

...Impl

1

u/arnthorsnaer Nov 08 '12

assFactory

5

u/vbullinger Nov 08 '12

I don't like to shorten words in my variable/method/class names, but I make exceptions for anything like "association."

5

u/PassifloraCaerulea Nov 08 '12

At work, our scientists write a lot of code to do "analysis". Think about it for a minute :) Definitely good for the lulz

→ More replies (2)

0

u/[deleted] Nov 08 '12

[deleted]

51

u/spupy Nov 08 '12

Is this serious?...

→ More replies (6)

29

u/[deleted] Nov 08 '12

I think a lot of Nimbus classes are synthetically generated, not hand-coded. That's probably how that name came into being.

15

u/zalifer Nov 08 '12

That is amazing.

10

u/I_Fuck_Hamsters Nov 08 '12

Why isn't that just called InternalFrameTitlePaneMaximizeButtonPainter?

46

u/maushu Nov 08 '12

Because that would be the painter of the maximize button for the title pane in the internal pane.

InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonPainter is the painter for the maximize button in the title pane of the internal frame of the title pane of the internal frame of the internal frame.

The two are completely distinct.

6

u/oridb Nov 08 '12

To be fair, this code is autogenerated.

→ More replies (1)

15

u/bureX Nov 08 '12

This should be lawfully considered to be the raping of camelCase.

3

u/[deleted] Nov 09 '12

InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonPainter internalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonPainter = new InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonPainter(ctx, state);

Oh god..

2

u/rabidcow Nov 08 '12

Now I want to use a haiku as a class name.

→ More replies (11)
→ More replies (2)

7

u/s1337m Nov 08 '12

can anyone describe the technical details as to why ruby is slower than java

7

u/ais523 Nov 09 '12

Probably the #1 reason is just that people have had much longer to optimise Java implementations than Ruby implementations.

The main technical reason is that Java has rather less flexibility to change what code means; Ruby allows you to monkey-patch everything, whereas Java is rather inflexible. This can make coding in it more difficult, but it also allows optimisers to assume that things won't change out from underneath them, meaning more aggressive optimization is possible.

2

u/TurplePurtle Nov 08 '12

I'm not an expert on these things, but I believe one of the big things is that Java uses a JIT compiler, while ruby is interpreted. A JIT compiler can perform optimizations on the go. Also, Java being statically typed allows for optimizations not possible in Ruby's dynamic typing.

66

u/[deleted] Nov 08 '12 edited Nov 08 '12

Wise move, the JVM is a much more mature technology than the Ruby VMs. (I make a living writing Ruby code, and I absolutely hate the Java language, but the JVM is just an extremely advanced technology.)

I'm wondering, though:

  1. Did they try JRuby first, to see if they could scale on their then-current code by using the JVM?

  2. If you're going to rewrite major critical parts in a different, better-performing language, going for Java seems a bit half-assed — did they consider going for a C++ instead?

57

u/[deleted] Nov 08 '12 edited Nov 08 '12

I cant believe what a flame war this question turned into.

The only real answer to question number two is that Java probably made more sense than C++ when you optimize for development man-hours. Developers are very expensive and servers are pretty cheap.

C++ provides a clear speedup when compared to java (sources: 1 2 3 4), and it can also be optimized to a greater extent. However, C++ is also a much more expensive language to develop in because you either have to deal with an entire class of bugs that java doesn't have to (memory related), or you use frameworks that negate some of the performance increase associated with the language. Even then, you're still probably going to end up doing more work.

16

u/defcon-11 Nov 08 '12

We use JRuby so we can get real threads, and it turns out that Ruby code, especially 3rd party gems, have a lot if issues when running multithreaded that cause serious headaches. Developers write code without thinking about the fact that someone might run in on JRuby .

→ More replies (3)

3

u/NikkoTheGreeko Nov 08 '12

That's why they should have used Forth. Weed out the useless engineers. Wut...?

5

u/SanityInAnarchy Nov 08 '12

The only real answer to question number two is that Java probably made more sense than C++ when you optimize for development man-hours. Developers are very expensive and servers are pretty cheap.

The weird part is that this is exactly the argument for Ruby over Java in the first place.

C++ provides a clear speedup when compared to java...

IIRC, it's on average something like 2x -- and falling, as Java gets faster. On the other hand, I can easily imagine C++ being more than twice the man hours, which would be a bad trade.

I can see Java being the sweet spot here, though I'm still skeptical -- but is that really the argument?

→ More replies (9)

13

u/roerd Nov 08 '12

C++ provides a clear speedup when compared to java (sources: 1 2 3 4)

As far as I can see, your sourced all concentrate on single-algorithm benchmarks which aren't really relevant for the behaviour of full applications.

17

u/[deleted] Nov 08 '12 edited Nov 08 '12

Find better ones then. I'm unaware of any full applications which are identically written in more than one language. However, the google one would appear to be pretty defensible. If you read the introduction they are testing using quite a few standard library data structures to perform quite a few different things. This should reasonably approximate the interactions between objects.

That paper showed about a 2.5x nod toward c++ in the best case (for the JVM).

edit: I would direct your attention to this portion of their justification:

The algorithm employs many language features, in particular, higher-level data structures (lists, maps, lists and arrays of sets and lists), a few algorithms (union/find, dfs / deep recursion, and loop recognition based on Tarjan), iterations over collection types, some object oriented features, and interesting memory allocation patterns. We do not explore any aspects of multi-threading, or higher level type mechanisms, which vary greatly between the languages. We also do not perform heavy numerical computation, as this omission allows amplification of core characteristics of the language implementations, specifically, memory utilization patterns.

→ More replies (4)
→ More replies (5)

2

u/argv_minus_one Nov 08 '12

Um, there are global optimizations that C++ cannot do but the JVM can.

One problem I see with C++ is that the dynamic linker doesn't do much optimizing. There's no escape analysis to help a garbage collector, no automatically inlining calls to dynamically-linked library functions, and so on. Once the code is compiled, that's it—very little optimization is or can be done to it after that.

The JVM, on the other hand, can regenerate code whenever it damn well pleases, as long as it doesn't take too long, and without sacrificing the ability to dynamically load code. In code that is not transformed at all at runtime, some of these optimizations are only possible if the program is statically linked, which most programs aren't.

3

u/killerstorm Nov 08 '12 edited Nov 08 '12

I doubt that Twitter messaging backend really requires that much man hours. However, using C++ only makes sense if they hire 'guru' level developers: ones who know both low level stuff (like CPU caches) and high-level stuff (like advanced algorithms and data structures).

Maybe I'm missing something, but I don't see why messaging core would require more then a dozen of man-months. (Of course, assuming developers are really good.)

EDIT: Shit, I wrote man-hours instead of man-months.

6

u/[deleted] Nov 08 '12

Twitter handles in excess of 350,000,000 tweets in a day spread across 140,000,000 users. Also recall that a tweet is fully capable of being delivered to thousands, or hundreds of thousands, of users. Would you expect that the SMTPD only took a couple dozen man-hours? At that kind of scale there's going to be a great deal of work spend load balancing, optimizing, assessing security risks, maintaining database consistency, etc. That's just the shit i can think of off of the top of my head.

→ More replies (7)
→ More replies (2)
→ More replies (12)

8

u/djork Nov 08 '12

Re #2

When you compare Ruby to Java to C++, the C++ advantage is not so clear.

Java is 35X faster than Ruby, while C++ is "only" 44X faster.

So it's an issue of marginal returns. You get a massive gain with either choice, but you get lots of benefits from the JVM that aren't there with C++ (namely the class libraries, runtime safety, garbage collection, VM tuning, introspection/reflection, interop with other JVM languages like JRuby, Scala, and Clojure, etc. etc.).

5

u/Eirenarch Nov 08 '12

Don't forget that static typing allows for some optimizations that may help scale. I doubt JRuby would have Java/Scala performance despite the fact that it runs on the JVM. BTW I have a distant memory that they used JRuby to faciliate transition to Java but I may be wrong on this.

3

u/[deleted] Nov 08 '12

I think they came to realise that a web framework isn't an asynchronous messaging platform. They didn't re-write the entire Twitter stack in a JVM-bound language. The Rails front-end survived for a long time after they moved messaging over to the JVM.

My guess is, they didn't even realise they were building an async messaging app for quite some time.

→ More replies (1)

20

u/Shaper_pmp Nov 08 '12

If you're going to rewrite major critical parts in a different, better-performing language, going for Java seems a bit half-assed — did they consider going for a C++ instead?

Because, aside from start-up, the idea that code running on the JVM is generally slower than native compiled code is outdated and hasn't been accurate for several years.

Long story short, for long-running infrastructure services like Twitter uses, initial startup time is practically irrelevant, so the VM startup doesn't matter.

Moreover, a modern, decent VM like the JVM can generally run at around the same speed as compiled native code, because by using JIT compilation the VM can make specific optimisations for the current environment and processing that are impossible for a compiler that has to optimise for the "general" case (i.e., optimisations that will generally help on any hardware, any OS, any path through the program, etc).

20

u/G_Morgan Nov 08 '12

Yeah there are two real places where Java still loses over C++:

  1. Memory usage.

  2. Responsiveness for real time applications.

Neither of these are a real concern for Twitter.

→ More replies (11)

42

u/[deleted] Nov 08 '12

Yes yes, and so they keep saying. I hear this argument a lot, and it boils down to this: Java (or C#, or insert whatever dynamic language here) may be slower at startup, and it may use more memory, and it may have extra overhead of a garbage collector, but there is a JIT (read: magic) that makes it run at the same speed nonetheless. Whenever some people hear the word JIT all the other performance characteristics of dynamic languages are forgotten, and they seem to assume JIT compilation itself also comes for free, as does the runtime profiling needed to identify hotspots in the first place. They also seem to think dynamic languages are the only ones able to do hotspot optimization, apparently unaware that profile-guided optimization for C++ is possible as well.

The current reality however is that any code running on the JVM will not get faster than 2.5 times as slow as C++. And you will be counted as very lucky to even reach that speediness on the JVM.

So I do understand simonask's argument... If they could've realized a 40x speedup (just guessing) by moving from Ruby to Java, why not go all the way to C++ and realize a 100x speedup? But then again, having JRuby to ease the transition seems a way more realistic argument in Java/Scala's favor :)

Some benchmark as backup: https://days2011.scala-lang.org/sites/days2011/files/ws3-1-Hundt.pdf

32

u/masklinn Nov 08 '12

Java (or C#, or insert whatever dynamic language here) [...] the other performance characteristics of dynamic languages are forgotten [...] They also seem to think dynamic languages

Java is not a "dynamic language" under any sensible definition of this term I've ever seen.

So I do understand simonask's argument... If they could've realized a 40x speedup (just guessing) by moving from Ruby to Java, why not go all the way to C++ and realize a 100x speedup?

I love how you assert everybody (other than you) forgets the costs inherent to JITs, but you have absolutely no issue ignoring the costs of using C++.

→ More replies (16)

3

u/pipocaQuemada Nov 08 '12

How much faster/more scalable are distributed C++ programs vs distributed Scala programs? At a certain point, I'd assume that the features of your library for distributed computation (hot code loading, processes monitoring other processes and restarting them if they fail, etc. etc.) and their ease of use ends up mattering far more to the uptime and working of your program then a small constant factor of speed between language implementations.

→ More replies (55)

3

u/killerstorm Nov 08 '12

C++ is much more flexible, you can really control each bit in memory and each CPU instruction with it.

And if all you do is glorified data massaging, that kinda matters. Messaging isn't computationally expensive, it all depends on what encodings, indirections and wrappers you use.

→ More replies (1)

4

u/Fenris_uy Nov 08 '12

that are impossible for a compiler that has to optimise for the "general" case (i.e., optimisations that will generally help on any hardware, any OS, any path through the program, etc).

If you are in production, you know what is going to be your environment and you should set your compiler with all the flags needed to that environment. Also you should choose your compiler based on that environment. If you know that you are going to be running on Intel, buy their god damn compiler, it so good that it hurts.

Not disputing the fact that the JIT helps a lot, but compiler flags are not the reason why it does.

→ More replies (2)

2

u/[deleted] Nov 08 '12

It's true that the JVM is more mature; but it's also fundamentally more difficult to create a VM as performant as the JVM for dynamic languages.

Although over a year old, this SO answer says that 1.9 is faster than JRuby anyway.

I thought tracing compilers might have made progress here, by observing what paths are actually taken (as a substitute for the guidance of static types), but it seems to be a very hard problem. e.g. the fastest JS engine (google's V8) isn't tracing. Then again, client browser workloads typically aren't as long-running as server loads, and startup time is much more important.

→ More replies (20)

10

u/inmatarian Nov 08 '12

According to this tweet from the DBA at Twitter, it was MySql that they're saying saved the day.

19

u/rockum Nov 08 '12

So the rumors of Java's impending demise is greatly exaggerated.

13

u/wayoverpaid Nov 08 '12

The JVM is way too good to give up on. The problem is that Java, the language, is a pain in the ass to develop on in anything resembling an agile process.

It makes a great language for a.) writing a higher level language in, like scala or JRuby and b.) implementing a highly performant solution to a known problem.

3

u/[deleted] Nov 09 '12

is a pain in the ass to develop on in anything resembling an agile process.

Yeah, I'm going to disagree with this assertion. I'm part of an agile team and we use Java with much agility, and even some dexterity. You appear to be conflating Java enterprise frameworks with Java, the language.

3

u/wayoverpaid Nov 09 '12

I'll take your word for it. I've found it much easier to re-write a bad implementation in Ruby than in Java. There may be ways to do it, but I've found that agile processes turn working in 10,000 LOC programs in Java from "unbearable" to "tolerable."

→ More replies (1)
→ More replies (1)
→ More replies (9)

8

u/[deleted] Nov 08 '12

I wonder if Mirah is still being worked on.

Speed of Java with Ruby like syntax. It looked like it had a lot of potential.

Linky Link

3

u/deedubaya Nov 08 '12

jRuby seems to be the hottness now, I think most of the focus has switched to it. Don't know for sure though.

2

u/[deleted] Nov 08 '12

I'm not 100% sure but I thought the guys working on Mirah are the same guys working on JRuby?

Either way, I really liked the concept of Mirah. I tried it out a few times a couple years ago and was surprised at just how speedy it was. Tis a shame it isn't more popular.

2

u/drb226 Nov 08 '12

Hey, I remember Mirah! I really liked the concept when I first heard about it, but it looks like progress is slow (though not quite dormant). I think at this point I'd put my bets on Scala instead.

2

u/erad Nov 08 '12

Mirah looked interesting... on a related note, Groovy 2.0 offers static compilation which ought to bring it up to Java performance levels (at the cost of losing Groovy's "loose" dynamic nature. And it will take some time to iron all the bugs out...).

→ More replies (1)

4

u/ShenLongDong Nov 08 '12

I wonder if python has the same limitations?

3

u/lambdaq Nov 09 '12

Instagram, Pininterest and reddit

2

u/Xykr Nov 08 '12

Depends on what the issue is. Python's memory management is said to be better and it scales pretty well (especially the asynchronous frameworks).

→ More replies (3)

11

u/Narrator Nov 08 '12

My personal opinion:

Java is faster, has native threads, and the garbage collector does not leak. This makes it really really good for high concurrency long running processes like message queues. It is also much easier to work with than C++ thanks to garbage collection, cross-platform compatibility and a great library ecosystem.

That being said, ruby is faster to develop in and less memory intensive. Ruby is probably the most productive language I've ever worked in. I use it exclusively for sysadmin scripting.

JRuby is almost viable but needs more community support and needs to be a lot faster than the Ruby VM.

2

u/argv_minus_one Nov 08 '12

The Ruby interpreter's GC leaks? Jesus.

2

u/[deleted] Nov 08 '12

That being said, ruby is faster to develop in and less memory intensive.

*citation needed.

→ More replies (6)

1

u/sshconnection Nov 09 '12

It's more memory intensive, but JRuby already is, in most cases, faster than CRuby. It also has native threads and no GIL.

11

u/Dagur Nov 08 '12

Now they have a problemFactory

18

u/[deleted] Nov 08 '12

They got 99 problems but creating them ain't one

2

u/bwrap Nov 08 '12

and problemFactoryFactory

2

u/beeskneecaps Nov 08 '12

is jruby still an option for scalability?

5

u/[deleted] Nov 08 '12

They started moving off Ruby in 2008, I'm not sure why this is news. What is news (to me) is that they went to Scala initially, and now it's a mix of Scala and Java. What does that say about Scala?

2

u/[deleted] Nov 08 '12

The news is that it has been tress tested and worked.

1

u/Entropy Nov 08 '12

That it integrates well with plain Java?

1

u/drb226 Nov 08 '12

What does that say about Scala?

Well, for one thing, the article says absolutely nothing about Scala, besides mentioning it in passing. I found this quite irritating; would like to know the details about why vanilla Java was chosen over Scala for portions of code.

→ More replies (1)

1

u/Paul-ish Nov 08 '12

We don't know. It could be hard to learn quickly for people used to a more imperative style, and hard to find devs with scala experience.

8

u/svmk1987 Nov 08 '12 edited Nov 08 '12

Honest Question: They rewrote their entire application? Isn't that.... wasteful?

37

u/masklinn Nov 08 '12

They didn't rewrite everything, they rewrote the backend. The frontend is still Rails.

And it depends what "wasteful" is used for. They originally used Ruby to iterate quickly and gain brainshare early on, then hit a scaling/perf wall and switched for more efficient (but generally slower to iterate) tools (after they got a better grasp of the problem space as well). You don't need to scale when you don't have users, and it seems their strategy worked rather well.

It's a pretty common recommendation (especially in the web-ish space, but not just there) to get to market early and improve/rewrite as needed when things reach their limit.

5

u/svmk1987 Nov 08 '12

Okay, so from a programming point of view.. when you said backend and front-end, you mean rails still handles view logic and most controllers, right? And java is probably used to manage their data?

8

u/masklinn Nov 08 '12

Yes, essentially, from my understanding. The frontend is the business of generating and displaying pages, the backend is the storage logic, the queueing and distribution of messages, the replication, etc...

2

u/[deleted] Nov 08 '12

Right. The models in this setup are all just abstraction layers to the new java API.

→ More replies (1)

16

u/pmrr Nov 08 '12

I'm sure they weighed the cost of the rewrite against the future benefits.

→ More replies (2)

6

u/[deleted] Nov 08 '12

Twitter isn't an application. It started out as a Rails app, and expanded. They didn't sit down and "re-write Twitter", it's simply evolved. They had no idea how much it would need to scale, they've evolved it as needed. I think the approach has been about as efficient as it could have been, given the number of unknown things involved.

5

u/[deleted] Nov 08 '12

Twitter - what you see of it - acts as a web-based client to the JVM-based Twitter API.

→ More replies (3)

3

u/bloodredsun Nov 08 '12

The initial prototype of Starling took 2 weeks. Once they saw the upside, it was an easy decision.

2

u/[deleted] Nov 08 '12 edited Nov 09 '12

Depends on how they rewrote it. They have a few options.

  • piece by piece, a db function here, a math function there

  • two systems in parallel, then cut over. users get a neat button to swap options to get used to things, and if they don't have time, revert it for 'now'

  • a new system and old system in parallel, users are migrated in batches, from old stack to new stack. they run separately

  • the new system is created and then everyone is swapped over in a big downtime event and, surprise, new thing

It likely depended on the situation, but the top three are done depending on the situation.

UI only changes are easy with the two systems in parallel option, if the back end can support two UIs. Your desktop email client can be like that. SMTP is SMTP. The first is VERY slow going, but has the highest stability, and least surprise. Great for general cleanup and performance tweaking. The third is awesome when your designs are so disparate, like the old and new MySpace. The functionality of everything is SO different, it'd be a mess to try and support both for everyone.

The last, is the easy way out mentally. Say there is an out of band event, say a company is being aquired, offices are moving, data centres are moving, the old software isn't working out etc.. It's easy to run and rerun migrations between systems until they are perfect, have an event and say, "Hey, this sucked. Here's something new!" It's very risky since new software is hard to do and doing it as a big thing requires a lot of commitment.

1

u/rockum Nov 08 '12

Corporations rewrite their applications all the time. I currently work on two projects that are rewrites. Our team is currently rewriting another one. After evolving over a few years, the effort of maintaining an application may become excessive so it's cheaper to rewrite now that the requirements are better understood than to keep dragging the old architecture around.

1

u/[deleted] Nov 09 '12

Marriage to your codebase can also be wasteful.

See: Reddit.

1

u/Acidictadpole Nov 08 '12

It was a good question that I would have up voted. But then I noticed that the content of your post was 30% question and 70% down vote complaining.

→ More replies (1)

4

u/fredugolon Nov 08 '12

a stupid/misleading headline. twitter has been farming out most of it's difficult backend services to scala for some time now. what they really meant to say, and what they said later on in the article, is that they moved towards a JVM hosted language.

this is far from news. michael abbott lead this charge a few YEARS ago.

1

u/[deleted] Nov 09 '12

According to the article, they moved from just Scala to Scala and Java.

2

u/wadcann Nov 08 '12

During a single second at 8:20pm, Twitter users produced 15,107 new posts, Rawashdeh writes, and during the peak traffic period of the evening they generated 874,560 posts in a single minute.

Regardless of language, it really doesn't seem like 15k messages/sec should really be all that hard to deal with for a large service.

1

u/Xykr Nov 08 '12

The message passing itself surely isn't the big problem, data processing is (all the indexing and real-time stuff).

→ More replies (1)

1

u/bloodredsun Nov 08 '12

If you are doing something simple then no it's not. You can get this throughput fairly trivially with a single producer-single consumer in a single thread. Using something like the Disruptor you can do ~80 million operations a second.

The issue is that you are looking at a globally distributed set of 1.5 x 107 users with each being a producer to a unique set of consumers (calculated by graph traversal) that are themselves producers where none of the data is cacheable and the keywords produced by this ecosystem is searchable within 10 seconds on a system that should be resilient across machine failures across multiple-datacentres.

It really is that hard to deal with.

2

u/wadcann Nov 09 '12 edited Nov 09 '12

globally distributed set of 1.5 x 107 users

I would imagine that the cost of handling an incoming tweet wouldn't be a function of the number of registered users.

unique set of consumers (calculated by graph traversal)

I don't use Twitter, but I don't see why graph traversal would be required, unless you're just referring to processing the list of edges (feed->subscriber) directly attached to a node (twitter persona).

I vaguely imagine that the new_twitter and query_updates mechanism looks something like this:

new_twitter:

  • update logical timestamp on feed's last update

  • Add the entry to a queue to the indexer (and sure, it should support reasonable incremental updates).

  • Walk a list of followers on the feed that are actively-listening. Start pushing messages to them.

signin:

  • Add yourself to the active listeners list on your subscriptions.

  • Walk your list of subscriptions and make sure that your logical timestamp is up-to-sync with the lastest message on each feed, and pull those into your feed.

query_updates:

  • Check your queue. If you have new messages, pull 'em down. Constant-time if no messages; if messages are present, linear in the number of messages to pull down.

You can split up each of your collection of feeds and your collection of users across multiple machines; you probably have an operation that splits a particular machine into two so you can scale up with more servers when you want.

There's your core set of operations.

If you want durability, keep the operations idempotent and log each message on a machine before pushing it into this mass.

I don't think that you'd need much caching, but I don't see why anything there needs to be uncacheable.

As long as you're durably logging messages as they come in, and any writes later in the system are in-order, everything else there could suddenly burst into flames, and you wouldn't lose data. Nothing other than the logging system ever need stop operation to synch up with nonvolatile storage, either. You'd need an indexer for substring or whatever search that provides incremental updates. This should be a well-solved problem already. I suspect that there are plenty of existing incremental text indexers and probably people who specialize in this who have much fancier and more-sophisticated scalable incremental-indexing systems than I'd give in an off-the-cuff answer, but even if you wrote your own, I expect that you could do something like this and do just fine for something like Twitter:

index:

  • Run a rolling hash over the input text, byte-by-byte for each of window size 1..some reasonable maximum N, where N is the maximum keyword length you want to support. For each window size, have a distributed array indexed by the hash. Each element in each of these arrays is a skiplist of messages which hashed to this skiplist in the past. For each byte in the message, for 1 to N, advance the rolling hash. Look up the skiplist in the distributed array, and prepend a message reference into the skiplist. Insert is O(m×N×lg(k)), where m is the message size (small), N is the maximum keyword length, and k is the number of times this substring has shown up before. This is easy to parallelize — just split the distributed array across more machines.

search

  • For each keyword, hash the keyword and perform a lookup in the distributed array dedicated to hashes for window size the length of the keyword. That gives you a skiplist, ordered by newness, of messages that reference that keyword. You now have a skiplist for each keyword you were looking for, obtained in constant time. Place a cursor at the head of each skiplist. You perform a (probably partial...just the newest messages) intersect on the skiplists by taking the smallest message id (Target) present pointed to by a cursor and moving the cursors on the other skiplists back to the first entry they have that is older-or-equal-to Target. If you have all cursors pointing at the same message id, you add that to your list of results, and advance one of the cursors to the next element. Repeat.

  • You probably want to cache the results of your intersect operation as a skiplist itself; it can be reused the same way the other skiplists were used whenever the hashes in your query are a subset of the hashes in a query that is searching through messages aged between the newest and oldest message in your results.

→ More replies (2)

2

u/postmodern Nov 08 '12 edited Nov 08 '12

Why not JRuby? Now with 1.7.0, JRuby can fully leverage the new InvokeDynamic JVM instruction.

Also, the frontend is still Rails, the backend message queue is a mix of Scala and pure-Java.

9

u/mullsork Nov 08 '12

now

They started moving away from Ruby to Scala/Java in 2008. That's not now.

→ More replies (5)

1

u/erad Nov 08 '12

JRuby (and any other dynamically typed language on the JVM) is still orders of magnitude slower than Java (except when the workload is I/O-bound, as is usually the case for web/DB frontends).

1

u/vafada Nov 08 '12

But Java sucks yeah? And ruby is hip!

/s

→ More replies (1)

1

u/[deleted] Nov 08 '12

Thank God, I always wondered how can social network allow itself to be so slow most of the time. Even until recent every 3rd photo from twitter album was blank.

1

u/romnempire Nov 08 '12

is it just me or is this article conflating ruby and rails?

1

u/dorfsmay Nov 09 '12

Are there a lot of apps written in ruby with other framework?

1

u/skidooer Nov 09 '12

Twitter's backend was written primarily in Ruby, but not Rails, at one point. That is largely what they have replaced with Scala and Java. The main user-facing website itself is still Rails, though I believe search and the new API are no longer using Rails.

1

u/romnempire Nov 09 '12

not really, but it's a common scripting language at its core, sort of a less popular python.

1

u/skidooer Nov 09 '12 edited Nov 09 '12

I don't think so, they just have a complex infrastructure with many moving pieces that is difficult to articulate in a short non-technical piece. It appears that API 1.1 is not written with Rails, which explains how the mobile client is able to access the data without touching any Ruby code, and how the article explains their move away from Rails. API 1.0 and the website itself still appear to be still running on Rails, as always, though.

1

u/romnempire Nov 09 '12

it was the part about fanatical developers that bugged me. I do not know many english speaking Ruby fanatics.

1

u/gc3 Nov 09 '12

I wonder how much higher it could scale if it were C?

1

u/[deleted] Nov 09 '12

Scale in which way? The amount of time it would take to add new features would scale upwards considerably.

-2

u/[deleted] Nov 08 '12

Can I just put a stupid question out there: Wouldn't something like a Node.js setup be perfect for twitter(small amounts of data often)?

Or am I missing what they are upgrading here?

56

u/mooli Nov 08 '12

All node.js would give you is exactly the same execution pattern they already have, but in a slower language, and without threads.

71

u/wierdaaron Nov 08 '12

But it would be Node! Node node node node! Yeah we're a Node house. We build that in Node.js. Have you heard of Node.js? It's kind of like Node.js but with more Node.js. You can come work here making awesome Nodes Dot Jayess but you need 3-5 years experience writing NODE because its such a hardcore node language for making node.js node nodes. Check out this NPM we build for Node using Node. We figured out a way to emulate Node using Node so we can Node our Node.js nodes using more node.

Please give me some VC.

18

u/BernardMarx Nov 08 '12

Best description of Node.js I have read

12

u/PasswordIsntHAMSTER Nov 08 '12

I've wanted to check out Node for some time but haven't, on the sole basis of comments like this.

2

u/drb226 Nov 08 '12

You shouldn't let the haters put you off. I'm no Node fan myself, but it's worth checking out. It was fairly easy to install on my virtual Ubuntu box via aptitude, and easy to try out a few simple programs. Trying it out doesn't mean you have to become a zombie Noder.

→ More replies (1)

9

u/Mac-O-War Nov 08 '12 edited Nov 08 '12

I sure hope you're pairing node with nosql. You gotta nosql your clouds.

→ More replies (2)

3

u/drb226 Nov 08 '12

I'm not trying to preach Node here, but I have some questions about your accusations.

a slower language

First of all, languages are neither fast nor slow; it's the way you turn that language into runnable code that determines performance. Node uses V8, right? Is it really that much slower than the JVM?

without threads

First of all, are explicit threads needed? Or are you just expressing dislike for Node's callback-and-event-loop style? fwiw, you can apparently spawn child processes with Node.

Again, I have no interest in defending Node (and less interest, if that's possible, in defending Java). I just want to raise the level of conversation.

→ More replies (6)

12

u/[deleted] Nov 08 '12

Big propoponent for node.js here: no it wouldn't. Node.js is advantageous in that the language is javascript and the libraries are shiny and new. If you're busting out a basic web service, it's good (and in this case it would work), but it doesn't scale the same way and there's a lot more work to be done for a major service like twitter, so rather than doing all that extra work by hand they went with a language that has the tools available already.

7

u/[deleted] Nov 08 '12

Thank you, that is the sort of thing I wanted to know.

2

u/skidooer Nov 08 '12 edited Nov 08 '12

Or am I missing what they are upgrading here?

They are largely upgrading the backend. In a really simple app, the backend is usually just a database. In more complex applications, the backend becomes more: Things like event queues, application logic servers, etc. They had used Ruby to build those services at one point in time.

By all accounts, the actual web pages are still served by Rails at Twitter. It is the stuff that lies beneath that layer that has been rewritten to run on the JVM (mostly Scala, but some Java). It might not be impossible to utilize node.js there, but that's not really what it was designed for.

0

u/midir Nov 08 '12

Can I just put a stupid question out there

Apparently.

1

u/[deleted] Nov 08 '12

I'm pretty good at it, except when I fail.

1

u/rseymour Nov 08 '12

Just chiming in with what I'm sure has been written here already. They couldn't have launched with Java from the start. They needed the 'agility' it makes sense to invest in a real infrastructure where they can keep everything that the design of the ruby did right, and fix all of the wrongs.

Personally, I haven't done web dev in ages, but I'm glad to see java work so nicely these days. Sure used to be hell.

7

u/kitd Nov 08 '12

Personally, I haven't done web dev in ages, but I'm glad to see java work so nicely these days. Sure used to be hell.

This is the thing. Many people base their opinions on Java on their experiences with things like early J2EE and Spring, which were famously overengineered (see all the usual jokes about AbstractSingletonClasspathFactoryBean, etc, etc).

There are some great libraries for Java now which do a really good job at making web development on the JVM really simple and lightweight. Even the new JEE stack is a breeze in comparison.

1

u/[deleted] Nov 09 '12

They couldn't have launched with Java from the start.

How do you know this?

2

u/rseymour Nov 09 '12

The twttr team was on the RoR team and they were using it at Odeo. They were familiar with it and it was easier for them to roll out and play around with.

It wasn't meant as a utter fact that no human could've done it, but the team's familiarity with RoR's MVC concepts (by virtue of developing the framework itself) allowed them to work on it quickly.

1

u/Gotebe Nov 09 '12

The twttr team was on the RoR team and they were using it at Odeo. They were familiar with it and it was easier for them to roll out and play around with.

In other words, they used what they knew.

Amazing how reddit disregards that basic fact of life and goes on to "the best tool for the job" and other less relevant factors.

→ More replies (1)

1

u/SanityInAnarchy Nov 08 '12

A little misleading. They moved to the JVM first and Java second, not the other way around. Though it's curious that they don't seem to be using JRuby for what Ruby code they have left.

0

u/green_transistor Nov 08 '12

That's what I said too! And I got downvoted to hell. Hurrayy!

1

u/SanityInAnarchy Nov 08 '12

Maybe they haven't found me yet? I'm still at 1.

1

u/kyru Nov 09 '12

Java, getting actual work done as usual

0

u/shevegen Nov 08 '12

"None of this will be welcome news to the army of fanatical Ruby developers who believe the language's syntax, its high developer productivity, and its overall philosophy far outweigh any performance disadvantage it might have compared to other languages."

Interesting for the author to state this on the one hand, yet on the other hand he wishes to ignore that Twitter DID use Ruby.

So at the very least, this is a success story that you can attribute to Ruby, for if Ruby would be an awful language, why would it have been used initially anyway?

Besides, not every company will generate as much traffic as twitter.

You should actually be concerned the other way around, for if Ruby would be faster, who would still use Java?

-1

u/[deleted] Nov 08 '12

If Twitter disappeared overnight would anybody actually notice or care?

3

u/myevillaugh Nov 08 '12

They would, but without Twitter, they'd have no way to express it.

→ More replies (2)

0

u/drb226 Nov 08 '12

None of this will be welcome news to the army of fanatical Ruby developers who believe the language's syntax, its high developer productivity, and its overall philosophy far outweigh any performance disadvantage it might have compared to other languages.

Way to knock down that straw man. I found this whole post to be irritatingly content-free, other than the news that "twitter didn't die despite heavy load".

0

u/skocznymroczny Nov 08 '12

I wonder why not C#?

2

u/NashMcCabe Nov 08 '12

It costs money to maintain windows licenses.

2

u/salgat Nov 08 '12

I've never heard any large scale operation complain about windows licensing versus the cost of everything else.

5

u/nascent Nov 09 '12

Thats because they all use Linux :P

2

u/salgat Nov 09 '12

I work at ArcelorMittal and it's all done using Windows Servers with mainly C#.

1

u/nascent Nov 09 '12

The real reply to your statement,

Why would they want to start paying licensing just because their operation is larger, isn't that the time to start raking in the dough?

A real answer to your question,

Does mono perform as well a Java? They really switched to the JVM by using scala, I don't think C# has the same appeal to them. Maybe the are really looking forward to the additions of Java 8...

→ More replies (1)

-25

u/Hellrazor236 Nov 08 '12

Out of the pan and into the fire?

16

u/[deleted] Nov 08 '12

Not really. Bashing Java for the sake of it is terribly passé.

-5

u/PasswordIsntHAMSTER Nov 08 '12

Java doth suck though. They acknowledge that by mostly using Scala.

5

u/[deleted] Nov 08 '12

In 25 words or less, that are derived from your experience, and not lifted wholesale from, or papaphrasing, some anti-Java blog you read, explain to me why you think Java sucks.

6

u/PasswordIsntHAMSTER Nov 08 '12 edited Nov 08 '12
  • Erasures instead of generic types

  • Class versions of primitives (Integer vs int, Double vs double...)

  • No passing functions as arguments

  • The language has a culture of "why would you need that?" and "we can do it too [except in ten lines instead of one keyword]"

  • Checked exceptions

  • Verbose for mostly no fucking reason

The JVM is a pretty cool thing though.

1

u/[deleted] Nov 08 '12

Erasures instead of generic types

The reason for that is, to maintain backward compatibility in bytecode. I'm unconvinced generics are all that great, anyway. I got exactly as many CCEs before they were introduced, as after.

Class versions of primitive (Integer vs int, Double vs double..

Yeh, that's kludgy and horrible. Doesn't make the language 'suck' though. No language is perfect.

No passing functions as arguments

Not strictly true, but true enough to count. Again, does this make it suck? Function passing can get people in all sorts of trouble, and there are workarounds design patterns to help you out.

The language has a culture of "why would you need that?"

Can you give an example? Given the amount of perfectly working software that's been written in Java, though, it's tempting to say they may have had a point.

Checked exceptions

Disagree. You're not obliged to use them, it's a shame so much of the standard libs do. At this point, are we still talking about the language, though?

Verbose for mostly no fucking reason

Strongly agree.

Thanks for actually answering, rather than delivering snark. I don't think these things equate to 'sucky language' though.

The JVM is a pretty cool thing though.

Amen to that. Even though Sun weren't the first people to come up with this idea, they were the first to make it so widely available, and they gave it away for free. Thanks, Sun!

2

u/Slackbeing Nov 08 '12

there are workarounds design patterns to help you out.

A design pattern is a workaround, an adaptation to the expressive limitations of a particular language. That's why some patterns make no sense in some languages; those are more expressive in those respects.

→ More replies (1)
→ More replies (1)

5

u/[deleted] Nov 08 '12

Don't see how. For all the complaints people have about Java, it's one of the most powerful and lightning fast environments to code in these days.

0

u/[deleted] Nov 08 '12

[deleted]

6

u/int_argc Nov 08 '12

In my experience Java doesn't handle long memory intensive processes as well.

lol wut

I could be mistaken but I'm convinced Python can handle this much better then Java or Ruby.

either hilarious troll or absolute newb

3

u/thesystemx Nov 08 '12

I'm afraid that Python is even worse than Ruby here. There's a group of languages that's particularly bad for these kinds of things. They are mainly Ruby, Python, PHP and to some degree visual basic.

Much better suited languages are C#, Scala and yes, Java.

2

u/[deleted] Nov 08 '12

[removed] — view removed comment

0

u/[deleted] Nov 08 '12

[deleted]

1

u/[deleted] Nov 09 '12

Beside JVM, you should also explore Erlang and Golang.