Zalando case study: why they chose Scala?
As Zalando kept growing to become an ecommerce giant, at some point they have decided to switch from Java to Scala. Although it’s a major move that required a lot of effort, they managed to do it in less than three months.
Want to see how they did it? Wondering if this might be the right choice for your company? Keep reading to find out more!
Java vs. Scala – the main differences
We’ll start with a quick introduction so you can see clearly what are the main differences between Java and Scala:
- Amount of code – This is the main difference you’ll surely notice at first sight. Scala is a drastically more concise language than Java. The number of lines is visibly reduced, thanks to the use of type inference, function passing and several other factors.
- Parallelism – Scala is a much better fit for parallel programming, as it’s designed to express patterns in a very concise manner.
- Objects and functions – While Scala is both functional and object-oriented (we’ll cover that in more detail later), you need to keep in mind that functions are acting as objects in Java. Scala treats every function as a variable, which means than one Scala function can accept another. This is one of the most impressive features of this language.
- Syntax – This is one of the reasons while Scala’s learning curve can be so steep. Although concise, the syntax seems a little off-putting at first, which is why some people consider Java more readable. This might be a problem for these who want to get started with Scala.
Why Zalando chose Scala
Alright, so why exactly did Zalando decide to look for a Java alternative, then? Here’s why they thought Scala might be a better choice for some of their needs:
- The company had years of previous experience with the Java Virtual Machine – Javier Arrieta, one of the Senior Software Engineers at Zalando, had a whopping 18 years of previous experience working with JVM. This solution is fast, efficient and quite convenient for debugging as well. Java worked fine, but there was one problem: the amount of code needed to ensure stability. The team thought that it would also be nice to use lambdas for transforming collections.
- Scala works well with Java libraries and frameworks – This made the transition from Java to Scala much more convenient. The developers didn’t have to let go of all the libraries they’ve already been used to. They could also stick to Play, which is used for both Java and Scala development. This framework is intuitive, extensive and trustworthy, as it’s backed by Lightbend (formerly Typesafe).
- It combines functional programming and object-oriented programming – Why choose one when you can have both? Scala presents a hybrid approach, as it combines both object-oriented (OOP) and functional programming. The possibility to choose one or the other paradigm, depending on the situation, makes Scala a really flexible choice.
- Scala is great for parallelization – This is one of the main advantages of functional programming. What’s more, Scala comes with a Futures API, which makes parallel programming much smoother. You can use this solution instead of sticking to traditional methods like locks, callbacks and threads.
What kinds of challenges come with Scala
Although the Zalando team is still positive that moving to Scala was the best choice they could make, there are some challenges they had to face:
- Compilation times – Advanced languages features in Scala make the compilation time longer. Luckily, there’s a solution to this issue. Zalando team turned to incremental compilation. This trick helped them solve the problem.
- Language problems – The same thing in Scala can be written in many different ways, which is both a blessing and a curse. There is no canonical style guide available and the code can get unreadable quite. When implementing Scala, it definitely pays off to implement some sort of in-house style guide. This will help you achieve consistency.
- Possible operator overloading – In Scala, the operator can have a different implementation depending on the argument. This case of polymorphism can lead to confusing constructions.
- Learning curve – As we’ve already mentioned in the Java vs. Scala comparison, Scala’s syntax can seem highly complicated to those who want to learn it. It’s a hybrid language, which is a double-edged sword: it makes work easier, but you need to consider more aspects to understand how it works.
How Zalando introduced Scala
The team has to admit that Scala’s learning curve is pretty steep. All developers had to be introduced not only to a new language, but also new frameworks, new build tool and, above all, functional programming concepts.
So, how do you tackle such a complex project? Of course, you need to start by providing high-quality educational materials. The team at Zalando picked Scala courses from Coursera, which turned out to be a great fit for their needs.
The developers proceeded to learn through prototypes and greenfield projects. They were creating systems for a new environment, without integrating them with other systems yet. Another method used to introduce Scala was the layered approach. All these measures helped the team learn new skills in a controlled environment.
Internal workshops worked quite well, as many senior Java developers were willing to learn Scala. Actually, all-Scala developers are quite a rare phenomenon – most of them have transitioned from another programming language. When it comes to exact numbers, 40% of engineers and Zalando expressed interest in learning Scala.
In Zalando’s Dublin office, most services and data pipelines are written in Scala. The Irish team is using that language to develop the Customer Data Platform.
How the developers at Zalando adapted to Scala
It is also worth mentioning that there were two main types of Scala adoption in the company:
- Using Scala as Java – For developers who already had years of experience working with Java, it was almost certain to happen. You could say that they’re using Scala as an improved version of Java, with all the lambdas, pattern matching and case classes involved.
- Functional programming nerds – The second group, on the other hand, really got into Scala and all the exciting new opportunities that come with functional programming. They treat Scala like an entirely new programming language and are more open to trying new things.
It’s good to keep this pattern in mind, as it’s quite likely that introducing Scala will take a similar turn in your company. Some developers may prefer to stick to their own habits and only adopt certain features of the new languages, while some may be particularly excited about implementing a new solution.
What Zalando loves about Scala
Now that Zalando has been working with Scala for quite some time, they can definitely list some of their favorite things about it:
- Types – The team at Zalando absolutely loves types. They help them understand what kind of value they’re working with, whether it’s the customer’s password, their name, or maybe their email address. They prefer using either tagged types or value classes to manage this data.
- Referential transparency – It’s about the substitution model. Referential transparent computation allows the team to substitute a function with parameters, which makes work so much easier. It’s also incredibly helpful for testing a program.
- Using monads for function composition – One of the main advantages of Scala is how it allows composing functions to create more complex ones. Monads one of the most popular ways to make that happen. Thanks to them, many operations can run sequentially.
As you can see, the Zalando team is very happy with using Scala instead of Java. They love to explore the wide possibilities that come with the hybrid of object-oriented and functional programming. Why not have a closer look at their process and implement a similar solution in your company? Who knows, maybe Scala is the alternative you were looking for. The case of Zalando proves that it can be successfully implemented in just a couple of weeks.
The article is based on the following sources:
Check out also: