Elixir vs Erlang - Comprehensive Overview of Functional Programming Languages
In the world of programming languages, many options depending on your needs. For example, for beginners, Python is often recommended for its readability and understandability. On the other hand, languages like Java are better suited to more complex applications.
This article will focus on two intertwined languages: Elixir and Erlang. Let's closely look at both languages to help you decide which is suitable for your project.
What is Erlang? - a brief introduction to Erlang
Let's start with what Erlang is.
In 1986, Joe Armstrong, Robert Virding, and Mike Williams created Erlang - a functional programming language, which at the beginning, was a proprietary tool of Ericsson company. Later, in 1998, Ericsson released Erlang as an open-source programming language.
Developers use this functional programming language to create computer systems with large amounts of traffic. Moreover, system admins also use Erlang for real-time applications requiring soft real-time calculation.
But what does that mean? Before we get into the nitty-gritty of things, let's break down some basics.
What is functional programming?
Functional programming is a type of declarative programming. In functional programming, the programmer declares what the program should achieve, not how it should do it. As a result, functional programming is more concise and intelligible than other programming paradigms, such as procedural or object-oriented programming.
In pure functional programming, once defined, a function returns the same value for specified argument values, just like mathematical functions, so great emphasis is also placed on data consistency. So, where does Erlang belong in this equation? Strictly speaking, Erlang is not a purely functional language because it allows functions to cause side effects on the system. However, it still is an available language in the paradigm.
Now that we know a little more about what Erlang is let's look at its pros and potential disadvantages.
Advantages and disadvantages of Erlang functional programming language
Parallelism & Concurrency
One advantage of using Erlang is its focus on parallelism and concurrency. By design, Erlang programs can utilize multiple cores on a single CPU - Erlang developers can fully use today's multicore processors to improve performance.
Furthermore, because Erlang was designed with concurrency in mind from the start, it's relatively easy to write programs that can take advantage of multicore processors without running into common concurrency issues such as race conditions or deadlocks.
Fault Tolerance
Another significant advantage of using Erlang is its built-in fault tolerance features. For example, when an Erlang process crashes, it can be configured to be automatically restarted by the Erlang runtime system - minimizing downtime and preventing errors from propagating throughout the system.
Because processes in Erlang are isolated from one another by design, a crash in one process will not bring down the entire system - unlike in languages such as C++ or Java, where a single process crash can take down the whole program.
Learning Curve
One potential disadvantage of using Erlang is its steep learning curve. While functional programming concepts are not particularly difficult to understand in theory, they can be challenging to implement - especially for programmers from a traditional object-oriented background.
In addition, because Erlang has its unique syntax and semantics (inspired by Prolog), it can take some time for new Erlang programmers to get up to speed with writing programs in this language.
Limited Library Support
Another potential downside to Erlang is its limited library support compared to other languages, such as Java or Python. While there are undoubtedly many useful libraries available for Erlang - including libraries for working with databases and HTTP - the number of suitable libraries is still relatively small compared to other languages.
This lack of library support can make it difficult to find existing solutions to common problems - forcing Erlang developers to either write their code or learn a new language altogether.
Companies that use Erlang virtual machine
As you already read, Erlang is a functional language used to build scalable, fault-tolerant systems. However, while it has been around for decades, it has only recently begun to gain mainstream adoption.
Thanks to its ability to handle large numbers of concurrent connections and its excellent performance, more and more companies are turning to the Erlang virtual machine (VM) to power their applications.
Let's look at some of the most notable companies that use Erlang.
WhatsApp is a messaging app with over 1.5 billion users worldwide. Facebook acquired it in 2014 for $19 billion. WhatsApp uses Erlang to power its real-time messaging system. In 2017, WhatsApp handled over 60 billion messages per day!
Skype
Skype is a VoIP service owned by Microsoft. It has over 300 million active users and handles over 3 billion minutes of calls each day. Skype uses Erlang for its backend services.
Tinder
Tinder is a popular dating app with over 50 million users. It processes over 1 billion swipes and 12 million matches per day! Tinder uses Erlang for its real-time matchmaking engine.
What is Elixir?
Now that you know what Erlang is, its advantages and disadvantages, and its use cases, it's time to learn the Elixir programming language.
Elixir is a dynamic, functional language designed for building scalable and maintainable applications.
Elixir's syntax is very similar to Ruby's. However, a few key differences make Elixir an appealing choice for developers looking for an alternative to Ruby.
Some of these differences include the following:
- Elixir is a compiled language that collects down to bytecode that can be run on the Erlang VM. This results in faster performance than interpreted languages like Ruby.
- Elixir inherits from Erlang, a very different concurrency model - it's actor-based, as opposed to Ruby's thread-based approach. Erlang and Elixir's lightweight processes can run in parallel without any limitations communicating via message passing and without issues with semaphores, locks etc. At the same time, Ruby's primary implementation has the global interpreter lock, which cripples its parallelism capabilities significantly, often forcing developers to use heavy OS-based processes.
- Elixir uses pattern matching instead of method overloading. This makes code more declarative and easier to read and understand.
Is Elixir based on Erlang?
Elixir leverages the Erlang VM, known for running low-latency, distributed and fault-tolerant systems, while also successfully used in web development and the embedded software domain.
The pros and cons of the Elixir programming language
Elixir was designed to address some of the shortcomings of other languages, such as Ruby. Here are some of the pros and cons of Elixir language.
Functional
Elixir is a functional programming language. This means that it is designed around immutable data structures and pure functions. Functional programming languages are generally easier to reason about than imperative languages, which makes them more suited for large projects.
Productive
Elixir is a very productive language. It has a concise syntax that allows you to write code quickly. It also has excellent tooling support so you can get up and running with Elixir speedily and efficiently.
Scalable
Elixir is built on top of the Erlang VM, designed for concurrency and fault tolerance. This makes Elixir ideal for building scalable applications.
Community
Elixir has a growing community of users and developers. While it is not as large as some other languages, it is still possible to find help and support when you need it.
New
Elixir is a relatively new language. While this can be both a pro and a con, Elixir is undoubtedly a 2010s language that doesn't feature leftovers from the 90s while at the same time being developed with stability in mind.
Syntax
Elixir, compared to Erlang, has a modern and - in most cases - friendlier syntax. In addition, its metaprogramming capabilities are much more unlimited than Erlang's, with its hygienic macro system providing much more flexibility. This comes at a price, though, as poor or premature usage of macros for code transformation can easily make the codebase convoluted and even harder to read or debug.
Limited Libraries
Elixir's library availability is growing, and the community is constantly making it enter new areas of usage unseen in the Erlang community, with machine learning coming to mind as a primary example. However, it still doesn't feel that "everything can be done via a library", so in some cases, you need to invent the wheel yourself.
Companies that use the Elixir language
Even though Elixir was established many years after Erlang, its advantages are appreciated by many globally known companies. As I gave examples of companies using Erlang before, here are some companies using Elixir language in their tech stack.
WhatsApp is a cross-platform messaging app with over 1.5 billion users. It's owned by Facebook and offers end-to-end encryption for all conversations.
Sky Betting and Gaming
Sky Betting and Gaming is a UK-based gambling and sports betting company. They offer online sports betting, casino games, and poker. Sky Betting and Gaming started using Elixir in 2016 to help reduce latency and improve user experience.
Pinterest is a visual bookmarking tool. Users can create boards where they save images, videos, and other content that they find online. Pinterest switched to Elixir in 2017 to help improve performance during high-traffic periods.
What is the advantage of Elixir over Erlang?
Although Elixir is based on the same basic principles as the Erlang programming language, it offers several advantages that make it a more attractive option for many developers. Here are some of the critical benefits Elixir has over Erlang.
Elixir offers a more concise syntax
One of the most significant advantages that Elixir has over Erlang is its more concise syntax. This makes Elixir code easier to write and read, saving considerable time and effort when developing large applications. In addition, using macros in Elixir allows for even more concise code by allowing developers to create their custom syntax constructs.
Elixir has a better tooling ecosystem
Although both Elixir and Erlang have excellent tooling ecosystems, Elixir's tools are generally considered more user-friendly and easier to use. For example, the popular Phoenix web framework has outpaced Erlang's OPA in terms of adoption and popularity. In addition, many of the most popular open-source Elixir libraries are better maintained than their Erlang counterparts.
Summary
Elixir offers several advantages over Erlang, making it a more attractive option for many developers. These include a more concise syntax, better scalability, and a superior tooling ecosystem. So if you're looking for a language to develop large distributed systems, Elixir is worth considering.