How and Why Twitter Uses Scala

More and more startups are choosing Scala these days. Some are doing so because it’s a flexible language, some like it for the excellent big data compatibility and some just find it the optimal option all around. However, startups aren’t the only companies putting stake in Scala as older tech giants and renowned companies are using the language as well.

Twitter is, of course, among those. The company switched over to Scala for their backend years ago. But what prompted the shift and was it a good decision? What does the language bring to the table and what are the pros and cons of Scala for the company? Today, we’ll take apart the way Twitter uses this language and see how this use-case can be an example to others.

We’ll start with an overview of the platform itself, the technology that came before the current setup, and the reasons why Scala might not be the ideal choice. Read on to hear the story of Twitter and Scala, a match made in heaven.

A Quick Look at Twitter

Before the life-changing move, Twitter’s backend used to be quite a problem for the company. The unstable architecture wouldn’t suffice as the social website grew and required technology capable of supporting the sprawling system.

Twitter needed caching layers and a language capable of handling heavy processes. This meant that something like Ruby wouldn’t do (more on that later) and even a popular choice like Java isn’t the best tool for the job. Unless you want to torture your engineer department, that is. So Twitter had a niche to fill and, despite still using Ruby on Rails for its frontend, the time came to change over to a new backend architecture method.

Options like C++ were reportedly barely considered as they were ill-fitting for the task. So eventually the choice was made and the slim pickings among languages made Twitter try Scala. It was chosen as a fast, advanced, and flexible language, winning over all others. A big factor was the fact that Scala takes from other Java libraries and the company could easily hire a Java developer and switch them over to Scala.

Why Did Twitter Move from Ruby to Scala?

Google “Does Twitter use Scala?” and you’ll get a bunch of detailed results. But the transition from Ruby to Scala isn’t as well-researched and, frankly, displayed Ruby in a poor light. It wasn’t an easy switch and it was one that was forced by circumstances at the time.

While Ruby on Rails is still used for frontend and, reportedly, Ruby isn’t abandoned altogether, Twitter using Scala language has prompted many to claim that Ruby was on its way out and somehow faulty. However, that is not the case at all and we’ll try to look objectively at why Twitter moved from Ruby to Scala without putting down the excellent language.

In truth, this transition only happened because Twitter’s needs changed drastically. When the company started, it had a small scale to operate on and productivity was key over scalability or growth. So Ruby was a very logical choice, offering an easy entry point for quicker operation. However, that was the earliest possible stage of development.

Twitter built on Scala would be an impossibility back then as Ruby fulfilled most of the needs the dev team had. It was reasonably fast, had enough support for the modest number of users the network sustained (remember, this was years ago), and it didn’t require any learning (Also, Scala was barely existent at that point with an extremely small community). But the user base has grown and tides shifted as real-time search along with fault tolerance became the top priorities.

Now, for all of Ruby’s advantages, these two points were not among its strengths. It doesn’t mesh well with real-time updates and delivering search results. Besides, Ruby lacked native thread support and couldn’t keep up with long-term processes, two crucial flaws that put it in the crosshairs.

Could Twitter have stuck with Ruby? Well, maybe if they changed course and somehow decided to limit their user base. But that’s counterintuitive and, in the long run, not worth it at all. So Ruby had to become a casualty in Twitter’s switch to Scala.

The transition was made easier as both languages involve OOP so there was some inheritance involved. Still, it definitely involved a lot of learning for all of the old devs and a rigorous search for new ones. After all, the number of skilled Scala developers isn’t sky high even now and it was depressingly low back in the day. Thankfully, the community is expanding and SysGears, in particular, is doing a lot for the cause, focusing its services on Scala. Why are we betting big on this one language? You’ll see it clearly as we dig into the advantages and flaws of Scala.

Pros and Cons of Scala

Scala is a functional and fast language but it’s not perfect (no language is, after all). Right now, we’ll look at a log of all Scala pros and cons and see how the benefits outweigh the problems. Keep in mind that some of the pros might not do much for your depending on the specifics of your product while some cons will not be an issue for you at all. It’s a matter of perspective and knowing how you can take a look at Twitter’s best practices with Scala and utilize that for yourself.

Pros of Scala

1. Simple Syntax

It takes less code for Scala to get the job done than it would in something like Ruby or Java. The way it’s structured also makes it easier to read, which means you won’t have to hunt down stray periods or feel your eyes glaze over as you try to find the one method you need among hundreds of lines of code.

2. Immutable Objects

The fact that Scala keeps objects immutable makes thread-safety less of an issue. It simplifies and streamlines things to a point, though it still bears some similarities to Java.

3. Relies on Functions

In Scala you can use a function as an argument for another function. And then get the value set as yet another function. See where this is going? Yeah, you can do anything in Scala using functions, assigning them value, using them for storing datum, etc.

If you’re not a big fan of functions, you can turn to Python-style object-oriented programming. Scala gives support for both options and makes them equally viable.

4. Speedy Implementation, High Quality

It’s pretty clear why this is a pro. Fast implementation equals faster results and that’s what you’re always striving for. This speed is achieved thanks to Scala’s flexibility and the way it scales while remaining stable.

In the right hands, Scala can become one of the fastest ways to set up a system without sacrificing other qualities like fault tolerance, big data processing etc. This kind of perfect storm is rare in programming languages and it’s what makes Scala rise above the cons we’ll be discussing below.

5. Suitable for Big Data Analytics

Scala is all about scalability and fault tolerance, and that means it works well with huge amounts of data. Perhaps that is why a lot of data analytics software offers support for Scala, making it easy to integrate the software into your system. It’s one of the standout features of Scala because something like Ruby, which Twitter used before transition to Scala, doesn’t have the ability to support big data effectively.

6. XML support

Scala lets you place XML documents directly in your code, making it easier to parse and manipulate them. This gives you more freedom in searching through the docs, using them for your architecture etc.

7. Supported by IDEs

Scala makes the process of coding itself easier with the variety of integrated development environments you can use. Some of these are good for big projects with sprawling scopes while some are a solid fit for quick patching and coding. Either way, the ability to code through IDEs is a big bonus for long-term work in startups and companies.

Cons of Scala

There aren’t that many cons to this language (otherwise there’d be no Scala web framework on Twitter) but we’ll try to highlight each and describe it in detail.

1. Limited Community Presence

The lack of a broad support network that devs could turn to when they hit a wall in their work is certainly a minus. However, the community is growing (especially after companies like AirBnB, Netflix and Twitter put their future on Scala’s shoulders) and we expect things to turn soon.

Besides, small community doesn’t mean passive community. Scala experts are highly active and ready to help whenever someone comes to them with a problem. So don’t discount the current pros and expect new ones to show up soon. Alternatively, why not lead the way and help grow the community yourself? That’s what SysGears has been doing and we’re always happy to see new people in the Scala community.

2. High Entry Barrier

This is, in part, a result of the limited community presence. Since Scala has fewer experts than something ultra-popular like Ruby, it’s tougher to find a good teacher or courses, which prompts people to learn by themselves.

What doesn’t help, though, is the fact that Scala is incredibly flexible and complex. That’s a blessing for those who know how to wield the tool (see the Twitter engine that uses Scala, for example) but a complete newbie will feel lost if they lack any kind of guidance.

This con is going to be a problem if you don’t have at least one or two people with Scala knowledge on your team. Thankfully, you don’t have to search far and wide as many companies are offering their help and expertise and SysGears is among them.

3. Limited Backward Compatibility

Scala limits how far back you can go on its versions in terms of compatibility. This means that you can’t rest comfortably on the older, familiar version. These regular updates can be a pain if you don’t have your sea legs yet but experienced devs should handle them just fine. They still are a chore and take time and compatibility isn’t exactly something that should be an afterthought. Still, the flexibility and quality might be worth it, depending on your outlook.

4. Still Immature

Compared to some of the languages being used today, Scala is still in its “teenage” years. Sure, there are newer ones that have yet to gain traction just like there are ancient mammoths like C++, Python or Ruby. But that doesn’t stop many people from seeing Scala as too fresh to be used consistently.

We, however, see this differently. Unless Scala is given this push, like the Twitter Scala replacement, we won’t see any growth. Leaning on the fresh language is essential for all of us. Using it for big and bold projects is key to get Scala growing and maturing.

Summary

We’ve talked about the way Twitter uses Scala and the reasons it switched from Ruby. With the main pros and cons of using Scala listed above, you can decide whether Twitter made the right move for yourself. However, SysGears absolutely supports Scala and the way this high-level language changes businesses.

Just look at the way Twitter has improved as a service, utilizing Scala’s strengths completely and navigating around its few flaws. As the language’s community evolves, it also changes the things you can do with Scala and the kind of software it’s useful for and the issues it can help to solve. If you want to follow the footsteps of giants like Twitter and improve your daily operations with the help of Scala – SysGears is ready to help.

We offer development of both large-scale and medium-sized projects so you’ll find what you seek with us. We’re actively involved in the Scala community so if you want expert help – come to the source and see SysGears in action.