Scala vs. GO
By: Javier Ramos
Tags: Big Data, Microservices, Golang, Serverless, Scala

Introduction

Since I got involved in DevOps and Big Data, I’ve been using two excellent but quite different programming languages: GO and Scala.

Scala is an older and more mature programming language that has found its niche in areas such concurrent programming and Big Data processing. Go, in the other hand, is a newer, simpler language created by Google to overcome the criticisms of C++; designing a language with multi core processors in mind.

Both are great languages that can achieve great performance for concurrent applications and stream processing but their design is quite different. In this article, I will try give you a quick overview of both languages, their advantages and drawbacks and review some real word use cases where we will recommend one language over the other.

Scala in a nutshell

Scala is a programming language created by scholars and it was the first alternative to Java running on the JVM. It defines itself as:

Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way.[1]

Scala is both, functional and object oriented, providing the best of both worlds. It has a flexible way to define your programs and it is up to you to decide how you want to describe them: from purely functional to pure object oriented programming.

Like Java, Scala is object-oriented:

Scala is a pure object-oriented language in the sense that every value is an object. Types and behaviors of objects are described by classes and traits.[1]

It also provides support for functional programming and a strong static type system. Unlike Java, Scala has many features of functional programming languages like Scheme, Standard ML and Haskell, including currying, type inference, immutability, lazy evaluation, and pattern matching. It also has an advanced type system supporting algebraic data types, covariance and contravariance, higher-order types (but not higher-rank types), and anonymous types.

Scala is also a functional language in the sense that every function is a value. Scala provides a lightweight syntax for defining anonymous functions, it supports higher-order functions, it allows functions to be nested, and it supports currying.[1]

Other features of Scala not present in Java include operator overloading, optional parameters, named parameters, and raw strings. Conversely, a feature of Java not in Scala is checked exceptions, which have proved controversial.

Scala is heavily used in Big Data among other use cases. Companies such Twitter moved to Scala, specially because of better concurrency support. Scala is powerful but hard to learn and still has some compatibility issues with Java.

Two key drivers of Scala usage is the Akka framework based on the actor system for concurrent programming and Spark processing engine for Big Data. Due to its academic nature, Scala didn’t find much usage in the real word compared to other languages, besides Akka or Spark. Although it is heavily used in financial institutions.

Scala Pros

Runs on the JVM . It comes with all the JVM optimizations built over the years and it is compatible with Java .

. It comes with all the JVM optimizations built over the years and it is . Very concise, it requires less boilerplate code compared to Java or GO.

Functional and Object Oriented .

. The type system is very powerful. You can build your custom DSL for your libraries, enforce checks at compile time and much more.

is very powerful. You can build your custom DSL for your libraries, enforce checks at compile time and much more. All the advantages of functional programming.

of functional programming. Akka ecosystem based on the actor model make it a great choice for concurrent systems.

ecosystem based on the actor model make it a great choice for concurrent systems. Powerful build tool.

tool. Versatile. Scala can be used to express any kind of problem.

Scala Cons

Complexity. Difficult to learn and to master. Usually used by senior programmers.

Difficult to learn and to master. Usually used by senior programmers. Too broad , there are a hundred ways to express the same thing, each Scala developer has its own style and it is difficult to maintain code consistency in a large project.

, there are a hundred ways to express the same thing, each Scala developer has its own style and it is difficult to maintain code consistency in a large project. Some compatibility issues with Java ; also if you need to use Java libraries, it takes away the advantages of Scala.

; also if you need to use Java libraries, it takes away the advantages of Scala. Slow compiler. Although there are tools such as Bloop that mitigates the issues.

Although there are tools such as Bloop that mitigates the issues. It inherits JVM issues for cloud native applications such large container size, slow warn-up time, difficult to tune, slow to deploy, etc.

for cloud native applications such large container size, slow warn-up time, difficult to tune, slow to deploy, etc. Although is quite versatile, Scala has found just certain niches and it is not widely adopted.

Use Cases

Big Data and Analytics . Because of its strong type system and extensibility, many tools in the Big Data world such Spark use Scala. It is the preferred language for Data Engineers and massive streaming and batch engines.

. Because of its strong type system and extensibility, many tools in the Big Data world such use Scala. It is the preferred language for Data Engineers and massive streaming and batch engines. Concurrent Systems and Real Time Applications . The Akka Framework is famous for it scalability for concurrent systems and widely used in trading services and other highly concurrent systems using the JVM.

. The Framework is famous for it scalability for concurrent systems and widely used in trading services and other highly concurrent systems using the JVM. Highly Concurrent APIs . The Play Framework provides a highly concurrent web framework to build Apps and APIs backed by Akka .

. The provides a highly concurrent web framework to build Apps and APIs backed by . Monoliths . Scala is great option for big monolith applications, even distributed applications. Using Akka clustering you can create highly distributed apps inside a single application.

. Scala is great option for big monolith applications, even distributed applications. Using Akka clustering you can create highly distributed apps inside a single application. In general, Scala is used to build frameworks and libraries for a specific domain like the case of Akka or Spark.

Go in a nutshell

Go is a different beast. Compared to Scala, it is not as elegant, powerful or concise but it was built for a specific purpose and it does it extremely well.

Go was created by Google and it is syntactically similar to C. Its goal was to overcome the unsafe operations present in C++ by adding memory safety, garbage collection, structural typing. It is very easy to learn and simple to use. It was built for multi core machines to maximize parallelism for concurrent programs. It uses very lightweight green threads called Go Routines for concurrent programming.

Go compiles quickly to machine code yet has the convenience of garbage collection and the power of run-time reflection. It’s a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language.[2]

Go is small, yet it covers many use cases such as microservices, stream processing, CLIs, and much more. Golang provides excellent support for producing binaries for different platforms without having to install Go on the target. Due to it’s small and efficient binary size, it is great for cloud native applications packaged in Containers. Your app container can be package into a tiny container (~5–10MB) that can be deployed in just a couple of seconds, making it a much better option for microservices than the JVM languages. For more information check my article about deploying Go Microservices in Kubernetes.

Go Pros

Super fast compiler , it feels like an interpreted language. Great developer experience. Fast development process and increased productivity .

, it feels like an interpreted language. Great developer experience. . Simple and safe , what I love about GO is that there is usually just one way of expressing problems, this speeds ups the development, code reviews and in general the whole development process.

, what I love about GO is that there is usually just one way of expressing problems, this speeds ups the development, code reviews and in general the whole development process. Great for both junior and senior developers. It is very easy to learn and adopt since it does not require a virtual environment.

since it does not require a virtual environment. Perfect choice for cloud native applications and Kubernetes . Due to the small size, no warm-up times and speed.

. Due to the small size, no warm-up times and speed. Concurrency made easy thanks to Go Routines .

made easy thanks to . Great standard library which includes a web server.

GO can be used in a wide range of use cases : CLIs, web applications, stream processing, etc.

CLIs, web applications, stream processing, etc. Very low resource usage. You can run literally millions of Go Routines in a single server. It uses very little RAM and CPU compared to the JVM making it much cheaper to run.

Go Cons

It’s not concise and it hard to keep the code DRY.

and it hard to keep the code DRY. Too simple , basic things like Generics are not available in Go, although they will be added soon.

, basic things like Generics are not available in Go, although they will be added soon. It’s a relatively new language with not many libraries or tutorials.

with not many libraries or tutorials. Dependency management is a bit counter intuitive and hard to manage but it has improved since the addition of go mod . The good news, is that go mod is part of the language and not a separate project like sbt , although sbt is much more powerful.

and hard to manage but it has improved since the addition of . The good news, is that is part of the language and not a separate project like , although is much more powerful. Error Handling is cumbersome .

. Not as elegant, powerful and flexible compared to Scala.

A bit immature, GO is newer compared to Scala.

Use Cases

CLIs and scripts : Most of the CLIs like kubectl use Go.

: Most of the CLIs like kubectl use Go. Web Applications . Since it is highly concurrent and does not require much resources, it is perfect to handle HTTP requests.

. Since it is highly concurrent and does not require much resources, it is perfect to handle HTTP requests. Stream Applications . Go can process millions of events extremely fast using Go routines. It is a competitor of Akka streams in Scala.

. Go can process millions of events extremely fast using Go routines. It is a competitor of Akka streams in Scala. Microservices . Due to the small size, speed and monitoring capabilities, Go is a great choice for cloud native microservices.

. Due to the small size, speed and monitoring capabilities, Go is a great choice for cloud native microservices. Serverless and cloud applications. Go is a perfect choice for Serverless functions, specially in Google Cloud.

Scala vs GO

Bot programming languages are strongly types, have a garbage collector, they are safe, highly concurrent and can process millions of records per second.

For Stream processing, GO is easier and simpler to use but depends on pub/sub systems such as Kafka and NoSQL database such as Cassandra. Akka is a beast on its own and can manage by itself large number of requests without any dependencies, it also provide more fores such back pressure.

For web applications, the Play Framework is more suitable for stateful web applications or long lived applications where there is enough time for the JVM to optimize the application. Scala is great for mini services or monoliths. It is also a good option if you need to integrate with Akka. In the other hand, GO is better suited for microservices, cloud native applications and Serverless functions. For Stateless APIs in the cloud I would use GO since it is cheaper and easier.

GO has great support gRPC making a great option for internal microservices that can be consumed in a stream fashion extremely fast.

For Big Data, Scala or Python are best choice. GO is still at its infancy in the Big Data world.

In general, GO is cheaper to run and to hire. So, if cost is an issue, look into GO. In the other hand, Scala developers tend to be really good engineers which also brings value to the organization.

GO source code tends to become harder to maintain as the application grows, so I wouldn’t recommend it for a monolith, for more stable and bigger projects, Scala is a better option, specially for more traditional enterprises.

Performance

This is a tricky question, if you Google vs GO performance you will find several articles and depending on the tests the results vary. At first, GO seems to perform better for simple use cases but for real world scenarios, Scala and specially the Play Framework, do catch up due to the JIT JVM optimizations which at runtime adjust the byte-code adding optimizations.

For microservices, stateless applications and containerized applications, GO will be faster and cheaper which is a huge advantage. In general, if cost is important to you, GO will take the upper hand. For complex applications, monolith, stateful processing, Scala is a better option. For a detail comparison of Go and the Play Framework check this great article.

GO vs Play REST Get Performance, source: https://medium.com/ymedialabs-innovation/rest-api-performance-comparison-between-golang-and-play-991a8f4040de

For Stream processing, it’s the same story. Akka is much more powerful and elegant, it has a huge ecosystem and has much more functionality, but it is complex to use, developer and to optimize. If you have a good team of Akka developers, they can write a single monolith that process events much faster than GO. However, GO is very simple to scale up.

Recommendations

If you are a developer…

Learn GO if you are a junior developer and/or you don’t have Java experience but have Python or C++ experience.

Learn GO if you want to work in the cloud, in start-ups and cool projects.

Learn GO if you can’t afford spending years to master a new language.

Learn GO if you are a back-end engineer developing stream applications and APIs.

Learn GO if you embrace the DevOps culture.

Learn GO if you are using Google Cloud.

Use GO to write scripts and command line tools, POCs or super fast APIs.

Learn Scala if you are a senior developer and/or Java developer.

Learn Scala if you already work with the JVM and/or you run on-prem.

Learn Scala if you are looking after high paid jobs using Akka and Spark.

Learn Scala if you are heavily invested in Big Data.

Learn Scala if you want to work in more traditional industries such financial institutions.

In short, GO is fun and cool, easy to learn and simple. In 3 months you can be writing production ready applications for real world usages. Scala is fun, complex and challenging but rewarding, it pays quite well and it has more prestige, specially in the Big Data world.

If you are a tech lead…

Use GO when you don’t have a JVM dependency and can run binary applications inside containers.

Use GO for Serverless functions instead of the JVM.

Use GO for short lived distributed web applications or APIs managed in the cloud or Kubernetes.

Use GO for custom scripts, small jobs and CLIs.

Use GO to consume and process large amount of events at low cost using very little resources.

Use GO if you run in the cloud, specially Google Cloud.

Use Scala if you depend on the JVM or your development team are already familiar with Java.

Use Scala for high concurrent distributed systems that take advantage of the JVM optimizations.

Use Scala for Big Data, specially Spark.

Use Scala if you have scaling issues with your Java applications.

In summary, GO is a binary language that consumes very little and it is easy to adopt, it is a nice tool for your tool-belt but I wouldn’t use it as the only programming language for bigger organizations as it is not as versatile as other languages. Scala is more difficult but it is a great next step if you are already using Java.

If you are a manager…

Use GO as a cost saving mechanism. Go is cheaper than Scala , not only it uses less resources but also, developers salaries are lower.

, not only it uses less resources but also, developers salaries are lower. Use GO for Serverless or Kubernetes .

. Use GO to attract younger talent and present the company as a startup.

Use GO if you are running in Google Cloud since most of the GCP services are based on GO APIs.

Use Scala for critical concurrent applications, monoliths or real time systems.

Use Scala to attract highly skilled senior developers.

Use Scala for Big Data .

. Use Scala if you already are using Java and change in your organization is difficult and takes time.

If you are looking to add a new language and you cannot spend much time, GO is a lot easier to adopt than Scala, specially if you already use C++ and not Java.

In summary, GO is cheaper to run and to hire talent; it is easy to use and can be adopted in a wide range of use cases. It can also be used as a complementary language while you still another language for your core applications. Scala is great for bigger projects and Big Data. If you are heavily invested in the JVM, Scala is a common next step.

Conclusion

I have been using both, Go and Scala for the past few years, both have their strengths and weaknesses. As a developer, I enjoy solving problems in Scala, it is quite elegant, concise and powerful; and a joy to use; however in real world problems, I tend to rely on GO more often because I get things done faster and cheaper; and this is what manners when you work in a company that needs to make money. Thanks to the fast compile time and deployment time along with very low resource usage, GO is the most cost effective language to use for cloud native applications. Still, for Big Data and highly concurrent critical systems Scala is the best option, also it is more mature than GO. For bigger and more traditional enterprises, Scala tends to be better fit.

References

[1] https://docs.scala-lang.org/tour/tour-of-scala.html

[2] https://golang.org/doc/