Java Returns to Rule the Web
Most of nowadays Web engineers came to realize that somo of dynamic rapid development languages, like Ruby, are not the best way to juggle tweets from millions of people across the globe. Suprasingly there is a wide raalization that the best way is a brand new architecture based on Java, a programing tool that has grown more powerful than many expected.
If some know Java at all, they 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. 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. Java is everywhere.
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. 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.
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.
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.
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. In the ultra-fast world of Web where concurrency is more important than ever, 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. But nowadays, developers see a very different tune. The great thing about the JVM is that there's a software library for everything, and if you want to solve a problem — no matter how goofy — there is likely something you can load up and use.
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.
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.
Java Virtual Machine Advantages
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. In other words, they're ready for the next renaissance.