The Second Coming of Java: A Relic Returns to Rule Web

Biz Stone called it "one of the most special days in the history of Twitter." And as it turned out, it was also a notable day for Java, a relic of the 1990s that is once again remaking the internet.

In the summer of 2010, Russian President Dmitry Medvedev visited Twitter headquarters in downtown San Francisco, on his way to a meeting with Google chief Eric Schmidt in Silicon Valley and a sit-down with President Barack Obama at the White House. That day, Twitter HQ was transformed into something akin to an airport security checkpoint, complete with armed guards, and the worldwide press turned out in droves to watch the Russian president send his first tweet.

The tweet was predictably prosaic – "Hello everyone, I’m now on Twitter and this is my first message," it said, in Russian – but as Stone, one of the company's founders, told the gathered press, this was a milestone for Twitter, a moment that so clearly showed that the company's micro-messaging service had graduated from intriguing novelty to something capable of changing the world.

What no one realized is that Medvedev didn't actually use Twitter that day. The web service was juggling so many tweets from across the globe – thanks in large part to the World Cup soccer tournament underway in South Africa – its engineers couldn't keep the site up and running for any lengthy amount of time. Before Medvedev visited, they built a separate service for him to tweet from, just so the thing wouldn't crash in the middle of the company's big photo-op.

"We literally couldn't even keep the site up for him," says Raffi Krikorian, vice president of engineering at Twitter. "When he signed up and sent his first tweet, we had him do it on a staging site...[though] he didn't know it at the time."

In the end, the visit from the Russian president was a turning point in more ways than one. Krikorian and the rest of the company's engineering brain-trust soon decided it was time to rebuild Twitter from the bottom up. They decided the site needed a new foundation. They decided to move the whole thing onto Java.

Since its inception in 2006, Twitter had run on software built with a computer programming tool called Ruby on Rails – a tool that played a huge role in the web's resurgence in the middle of the decade, letting engineers build sites so quickly and easily. But Twitter's engineers came to realize that Ruby wasn't the best way to juggle tweets from millions of people across the globe – and make sure the site could stay up during its headline moment with the president of Russia. The best way was a brand new architecture based on Java, a programing tool that has grown more powerful than many expected.

If you know Java at all, you probably think of it as something from the late '90s, a child of the original internet boom, a little piece of downloadable software that sent a cartoon mascot dancing across your Netscape web browser. You think of it as something that promised a world of software apps that could run on each and every one of your personal machines – from PCs to cellphones – but that ultimately failed in the face of endless security bugs and poor decisions from its creator, Sun Microsystems. "For the general populace," says LinkedIn principal staff engineer Jay Kreps, "Java is some annoying thing that really out-of-date websites try to make them download." And if you see it as anything more than that, you probably dismiss it as a way of building stodgy "middleware" tools that connect things like web servers and databases.

But over the past few years, Java has evolved into something very different. It has quietly become the primary foundation for most the net's largest and most ambitious operations, including Google, LinkedIn, Tumblr, and Square, as well as Twitter. "It's everywhere," says Krikorian.

In the summer of 2011, Bob Lee – the chief technology officer at Square and a former engineer at Google – announced at a prominent software conference that the web was "on the cusp of a Java renaissance." Two years later, this renaissance is upon us. Like Twitter, many other companies have realized that Java is particularly well suited to building web services that can stand up to the massive amounts of traffic streaming across the modern internet.

"Java is really the only choice when it comes to the requirements for a company like ours – extreme performance requirements and extreme scalability requirements," Lee says of Square, the San Francisco startup that processes $15 billion a year in credit and debit card transactions via mobile phones and tablets. "There is no viable alternative."

>'Java is really the only choice when it comes to the requirements for a company like ours – extreme performance requirements and extreme scalability requirements. There is no viable alternative'

Bob Lee

But there's a twist to this Java renaissance. It encompasses more than just Java.

That may sound like a paradox, but the thing to realize is that Java isn't one thing. It's two. It's a programming language, a way of writing software code. But it's also a "virtual machine" that executes code – a foundational piece of software that sits on a computer server or a PC or a cell phone, providing a way of running applications at unusually fast speeds. Originally, the Java virtual machine – aka the JVM – only ran code built with the Java programming language, but today, it runs all sorts of other languages.

So, the web's big names are using the Java virtual machine as the foundation of their online services, installing the JVM across tens of thousands of servers, and they can then use this base to run code built in myriad languages – from classic Java to a language called Clojure to a new and increasingly popular invention known as Scala – picking just the right tool for the task at hand.

Twitter builds some of its code with the Java programming language, but it fashions the majority with Scala (a language that, for many programmers, lets you create software with an ease that eclipses Java) and a bit with Clojure (a language that feels like Lisp, a way of quickly scripting code that has been a mainstay for decades). LinkedIn mostly uses the Java programming language, while sprinkling in some Scala. But the common denominator is the JVM, software that has been finely tuned over the past fifteen years to run code at speed.

"There are so many different languages that run on it," Krikorian says. "I only have to worry about tuning and optimizing this one thing, and I can put it on all the hardware we run at Twitter. It's just easier."

Just in Time for Twitter
————————

On August 3, Twitter set a new record for tweets in a single second. As thousands of people in Japan jumped onto the service to discuss the television airing of the animated film Castle in the Sky, it hit a one-second peak of 143,199 tweets. That's a massive spike over the norm – about 5,700-tweets-per-second – and the site stayed up. "Our users didn’t experience a blip," Krikorian recently wrote.

The moment was a far cry from the day Dmitry Medvedev visited Twitter HQ, and for Krikorian, it proves the worth of the company's new architecture.

Originally, Twitter was one, monolithic application built with Ruby on Rails. But now, it's divided into about two hundred self-contained services that talk to each other. Each runs atop the JVM, with most written in Scala and some in Java and Clojure. One service handles the Twitter homepage. Another handles the Twitter mobile site. A third handles the application programming interfaces, or APIs, that feed other operations across the net. And so on.

>On August 3, Twitter hit a one-second peak of 143,199 tweets. That's a massive spike over the norm – about 5,700-tweets-per-second – and the site stayed up

The setup helps Twitter deal with traffic spikes. Because the JVM is so efficient, it can handle much larger amounts of traffic with fewer machines. But the new operation is also more nimble. All these services are designed to communicate with each other, but if one goes down, it doesn't take the others down with it. The day we visited Krikorian at Twitter's offices this month, the Twitter homepage went dark for many people across the globe, but other services, including the company's mobile feed, kept on ticking.

From LinkedIn to Tumblr, many other big web names have adopted a similar "services architecture," and generally, they're building these services with Java or related languages. Java programmers are easy to come by, and compared to C and C++, the languages that rival its popularity, Java is rather easy to use. "It's the easiest of the fast languages," says LinkedIn's Kreps. But so much of this trend is driven by the JVM – and its ability to run more than just the Java language.

The JVM provides what's called "just-in-time compilation." After writing software code, you have to compile it – convert it into the native language spoken by the machine that will run it. Traditionally, developers compile their code into machine language and then ship it off to the computer in question. But with just-in-time, or JIT, compilation, you can compile code as it is executing, gaining some extra speed by tailoring the compilation according to behavior of the application. Java still can't match the speed of languages like C and C++, but according to Krikorian, it comes close enough.

Plus, the JVM is specifically designed to run multiple tasks – or threads – at the same time, an essential part of running web services in the modern world. "Concurrency is more important than ever," says Lee. "There is really no platform that compares to Java in that respect. It lets you write concurrent code – and extremely fast concurrent code." The JVM does this for Java code, but it also does this for Scala, Clojure, and more.

There was a time when many questioned the efficiency of the JVM. "I worked with Java a fair amount a long time ago," says Tumblr software engineer Mike Hurwitz. "I was glad to leave it behind." But nowadays, people like Hurwitz and Krikorian and Square's Lee sing a very different tune. "The great thing about the JVM is that there's a software library for everything," says Hurwitz. "If you want to solve a problem – no matter how goofy – there is likely something you can load up and use."

Ruby Derailed
————-

In 2006, when Twitter built its micro-blogging service with Ruby on Rails, it wasn't alone. As the web experienced a rebirth in the mid-aughts, the programming tools of the moment were Ruby and PHP, two "dynamically typed" languages that let you build succinct code at an unusually fast clip. But time has shown that these languages just weren't suited to running the world's largest web services, and now they've taken a backseat to Java – at least on the big stage.

"Ruby on Rails was great to get us to the point where we could make the decision to get off it," says Krikorian. With Java, he explains, Twitter needs about ten times fewer machines to run its site than it would need with Ruby. And unlike the Rails programming framework, Java and Scala let Twitter readily share and modify its enormous codebase across a team of hundreds of developers.

The Java language isn't quite as easy to use as Ruby, but for Krikorian and his engineers, Scala is. "Scala seems like a more modern language," he says. "It makes the transition from Ruby easier – and it's just more fun."

The exception that proves the rule is Facebook. Facebook was originally built with PHP, and it still runs on PHP. But to solve the scale problem, the social networking site has taken a page from the Java book, moving its PHP code onto a custom-built virtual machine that provides just-in-time compilation.

>'We just couldn't get Ruby on Rails to scale to the size of Twitter. Ruby on Rails was great to get us to the point where we could make the decision to get off it'

Raffi Krikorian

Facebook enjoys this sort of in-house hack. But so many others have just moved away from their original languages. Much like Twitter, Square switched to Java from Ruby. Tumblr migrated to Scala after juggling several other tools. No less a name than Google has moved towards Java from C – though it still runs C in places.

Meanwhile, outside the programming world, Java is still portrayed as security nightmare that no longer runs applications on PCs, laptops, and phones. And there's some truth to this. Late last year, a spate of new security bugs shined a harsh light on Java as a way of running software on most personal machines.

But thanks to a brand new virtual machine built specifically for mobile devices – Google's Dalvik virtual machine, the Java language has found new life on Android phones and tablets, where it's the primary means of building applications. And on servers, it's helping drive not only big name web services, but countless software applications used inside other businesses.

Java has continued to evolve, even in the face of corporate dithering from the late Sun Microsystems. Sun, for all its faults, was clever enough to open source the JVM, and Oracle, which acquired Sun is 2010, has proved to be a more active steward for the Java platform – to the surprise of many.

As an open source project, the JVM is free for everyone to use, and anyone is free to build new software and even new programming languages that run atop it. In the wake of Scala, other developers are building a new language for the JVM called Ceylon, and if you like, you can even run Ruby atop the virtual machine, in the form of something called JRuby.

Companies such as Twitter and LinkedIn and Square are constantly building new Java tools from scratch, and in many cases, they're sharing this code with the rest of the world, much as Sun shared the JVM and other parts of Java. This open source code then spawns more open source code. And so on. "We all just pick and choose the things that meet our needs," says Square's Lee. "Companies like ours are building all sorts of custom infrastructure, but we also think it's very important to open source."

The added benefit – for all these companies – is that, when the time comes, they can more easily move their services onto new types of hardware. They're not writing code for specific servers or processors. They're writing it for the JVM. So, when the world embraces a new type of server – which is very much on the horizon – these Java houses needn't rewrite everything. They can just move it to a new version of JVM.