Elixir programming language guide - learn why it's so admired
In the programming language world so dominated by an object-oriented paradigm, there is a strong need to popularize languages such as Elixir - a productive, scalable, functional programming language that consistently ranks at the top of the most loved programming languages in the world.
Getting Started with the Elixir Programming Language
To understand a programming language well, it's crucial to take three aspects into account:
- The reasons behind its creation.
- Its current state of development.
- Plans for the future of development.
My goal with this introduction to Elixir is to provide a very deep overview that will help you decide if this is the right choice for your business (if you are a decision-maker) or IT career (if you are a developer). Enjoy!
Elixir history - why it was created and what are its benefits
Although Elixir's first version is dated to 2012, its history really started in 2008. These are the times of significant change in IT - the year of the AppStore release that will forever change the industry. Back then, the internet was already a thing, and even though mobile apps were just starting to take off, web applications were already a hot topic.
At the center of rapid web application innovation were languages and web development frameworks that promised the productive development of digital products. One of these languages was Ruby and its Ruby on Rails web development framework. One of the core contributors and users of this tech stack was José Valim, the later creator of Elixir.
Scalability issues in IT
A famous Moore's law in IT observed by Gordon Moore states that the number of transistors doubles about every two years, which effectively should mean that if you want to scale your application, you can do it with more powerful hardware. Back in 2008, cloud-based applications started to take off (take Facebook as an example), and many companies had to face the problem of scaling. There were four issues, though:
- Moore's law had limitations, and it was already apparent that it was impossible to expect a doubling of transistors' numbers every year. Therefore, hardware had its limitations (and still has).
- Most of the web applications were not effectively utilizing all available hardware cores.
- Building scalable and maintainable applications that effectively utilize parallel processing was very hard.
- Once the hardware reached its limits (every core was used), maintaining distributed systems was a problem.
It's hard to change the nature of problem number 1. However, 2, 3, and 4 can be fixed with the right choice of tech stack. José Valim noticed that because of Ruby's language design as well as its Global Interpreter Lock (the reason why Ruby struggles with true parallelism), he had to search for other languages that could potentially help him in building scalable systems.
Meet Erlang VM (Erlang virtual machine)
Even though José primarily searched for a language that would be perfect for concurrent programming, he found much more in one language - Erlang.
Erlang, a dynamic functional language created in 1986, was initially designed for the telecommunication industry that required (and still does) their systems to:
- be highly available, non-stop working solution
- maintain a high fault tolerance
- be distributed
Doesn't it sound like the set of issues that web applications are facing?
These features are part of Erlang thanks to its functional programming design, but even more, thanks to its powerful Erlang Virtual Machine platform (also known as BEAM), which, in short, can be described as a platform where Erlang executes its concurrent, distributed applications that maintain very high fault tolerance.
By 2008, Erlang had already been running parallel, distributed systems that would use all available processor cores by design for three decades. This is important since we're talking here about a tech that has proved its value over a long period of time (at this point, for around 40 years!).
In addition to that, Erlang was also a functional language, which means a series of benefits.
Functional programming language
Many developers tend to be afraid of functional programming languages, and so was I. I can assure you, though, that this belief is very limiting, and I strongly believe that functional programming is easier to understand and use than object-oriented. Since Erlang is a functional programming language, it, of course, has these benefits as well.
Here are the top features of functional languages:
- Immutable data structures. Many errors in object-oriented programming come from mutability, and these errors are totally gone in functional programming due to immutability. In short, there are no side effects, and it makes code easier to reason about.
- Pure functions as the core. No classes, interfaces, or inheritance. The only thing you concentrate on are functions that somehow transform data as well as how to organize them well.
- Declarative programming. Declarative code focuses on the "what" (declaring what you want) rather than the "how" (the specific steps to achieve it), which results in more concise code.
- Pattern matching. This allows for a more intuitive way to destructure data structures and simplifies complex conditional logic. Although it may be a bit hard for beginners, as soon as you grasp the pattern matching concept, it becomes an incredibly powerful tool that simplifies code.
I had a chance to work with object-oriented languages as well as functional ones for almost the same number of years at this point (and over a decade in total), and my observations are the following:
- The functional paradigm leads to fewer errors, and most importantly, once the error occurs, it's much easier to debug it.
- The functional paradigm is much leaner. It turns out you can design any system you can imagine with data, functions, and a way to organize both (for instance, with modules). Having said that, it's also simpler.
José Valim noticed the benefits of Erlang as a functional programming language and its powerful Erlang VM. Why did he decide to create Elixir instead of just using Erlang, though?
Elixir programming language as an answer to Erlang weaknesses
Erlang has existed since 1986, therefore it was missing some features that José believed were crucial for modern, productive development. Instead of using Erlang, he decided to build a new functional language based on Erlang's VM. He called it Elixir, and it first appeared in 2012.
Elixir language key features include:
- Extensibility - the language itself was designed to be rather small but extensible. In other words - why would you pack programming language with tens of features (even if you don't need them) instead of allowing developers to extend it as they wish? Extending Elixir to new domains is easy, thanks to Elixir's extensibility.
- Metaprogramming with macros - thanks to macros, Elixir code becomes more productive and extensible, as mentioned in the previous feature.
- Polymorphism via protocols - protocols enable authors of libraries to establish contracts, while users can fulfill these contracts. This feature means growth for the whole Elixir ecosystem.
- Productive syntax - inspired by other languages, Elixir syntax is designed to be more productive than Erlang, and this is one of the most loved features of Elixir.
- Tooling - such as mix, which contains tasks for creating, compiling, testing your Elixir projects, managing dependencies, and more.
- Documentation - without world-class documentation, new adopters get lost. Elixir language has very high-rated documentation quality.
- Concurrency model enhancements - while Elixir language uses the same actor model as Erlang for concurrency, it offers additional abstractions like GenServer, Agent, and Task. Concurrency model, after productivity, is the most interesting feature of Elixir.
- Elixir Type System - although Elixir is a dynamic language (or, in other words, dynamically-typed language), it recently got its first version of a gradual set-theoretic types system that allows developers to optionally define data types in the code.
These Elixir features are highly rated by the community. You can verify this claim in the results of our Elixir survey.
Elixir programming language - the current state of development
Since 2012, Elixir developed in many ways. Although the language itself didn't change much (resulting in fewer breaking changes, a characteristic that the Elixir community values a lot), the ecosystem (thanks to its extensibility) grew with many extraordinary projects.
Created by Chris McCord in 2014, the Phoenix web development framework is no doubt, a growth engine for the whole Elixir community. Since cloud-based applications are the default way of thinking nowadays, most languages have evolved into this domain.
Phoenix web development framework follows very well-designed practices and tries not to hide too much from the developer (the black box pattern is quite popular in Ruby on Rails). Chris and the open-source Phoenix contributors managed to balance high productivity and awareness of what's "under the hood".
Phoenix consistently ranks as the most loved web development framework in the world in the Stack Overflow annual survey.
Phoenix LiveView mixes two great things together: live frontend interface with backend live state. Think of a React-like experience without the need to communicate with the backend via API.
There is also an evolving Phoenix LiveView Native project that goes even further - one codebase for web and mobile native component-based backend and frontend with Elixir at its core.
Learn more about Phoenix LiveView in one of our articles: What is Phoenix LiveView.
Elixir also works for IoT. Thanks to Nerves project, you can build embedded systems that are:
- portable - it minimizes the effort to port to other devices,
- lean - firmware sizes start in the 20-30 MB range,
- reliable - a feature borrowed from Elixir,
- secure - Nerves puts a significant effort into building secure software,
- efficient - yet another characteristic thanks to Elixir,
- extensible - Nerves allows you to integrate other great projects built on top of Elixir.
If you’re interested in an introduction to Nerves, take a look at our tutorial on how to program an IoT device in Elixir using Nerves.
Numerical Elixir - AI and ML
There are several projects and a huge effort to bring Elixir to AI and ML. The most notable ones are:
- Nx - a multi-dimensional tensors library with multi-staged compilation to the CPU/GPU,
- Livebook - open-source local-first notebooks to Elixir (similar to Jupyter for Python),
- Explorer - series and data frames to Elixir for quick and sophisticated data exploration,
- Axon - a Nx-powered Neural Network library.
If you're wondering if Elixir is a good fit for numerical computing, you can take a look at this explanation (spoiler alert, it is).
And.. if you’re interested in a hands-on introduction to neural networks in Elixir, you can visit our tutorial: how to create a neural network in Elixir using Nx and Axon.
Thoughts on the Elixir ecosystem
At this point, you may be comparing Elixir to other languages. You may think that other languages offer similar characteristics.
Let me leave you with one thought here:
Elixir allows you to build the most sophisticated web app solution on the market (Phoenix & Phoenix LiveView), and it even allows you to build native mobile apps (Phoenix LiveView Native) while offering a set of tools to work on AI, ML, and IoT. What's crazy about it, is that all of it can be placed in one repo and scales massively. It's impressive, considering that it's here since 2012. There is no other language and ecosystem like that.
Big companies investing in Elixir
At the end of the day, a programming language is nothing without big tech companies investing in its development. Elixir has proven its power many times, but most importantly, it has helped save a lot of money on infrastructure and development costs.
Here are the most exciting examples with references:
- Discord - millions of active WebSocket events per second thanks to the power of Elixir,
- WhatsApp - hundreds of millions of users powered by Erlang VM,
- Remote - biggest HR platform focused on managing the remote workforce,
- Pinterest - Elixir saved Pinterest over $2 million per year in server costs (most companies in our annual Elixir survey noted that they observed operational costs drop thanks to Elixir).
These are just some of the examples. Take a look at more of them here: adoption of Elixir by top companies.
Adoption of Elixir in various industries
Elixir suits well for most industries you can imagine. It will shine especially bright in cases where reliability (fault tolerance) and scalability are critical aspects of the application. However, due to its productivity, it can be applied to almost any digital product you can imagine.
There are various examples of startups from many different industries where Elixir became a competitive advantage for the company. You can read about some of these here: startups using Elixir language in production.
To give you an idea of where Elixir worked well for companies, take a look at this list of industries:
- Fintech: Solaris Bank, Goldman Sachs, Klarna
- Blockchain: Aeternity, POA Network
- E-commerce: PepsiCo, Allegro Lokalnie
- Education: Multiverse, Digiforma
- HR: Remote, Recruitee
- Sport: Bleacher Report, Bet365
- SEO: SurferSEO, Moz Pro
- Social & Communication: Discord, WhatsApp, Pinterest
A small yet important note here. In certain industries, peak performance is essential. A handful of languages, like C, C++, or Rust, are capable of meeting these high standards. Elixir may not be among them. However, it's worth noting that integrating Elixir with these languages is straightforward, especially when only a fraction of the application needs the speed offered by the fastest programming languages. Here is an example of how you can integrate Elixir with Rust.
Plans for the future development of Elixir
Every year at ElixirConf, José Valim, and Chris McCord share the "next big things" that Elixir and Phoenix framework teams are working on. Some great things about them, as well as the people involved in these open-source teams:
- They react to the most common requests from the community and simply listen to them (that's how Elixir got its research on introducing type system).
- They're not afraid to innovate, giving the power of platform (that's how Phoenix LiveView started).
- They make sure to be up-to-date with recent IT directions (that's how Numerical Elixir was introduced to support ML & AI).
In short, the Elixir & Phoenix community is highly satisfied with the speed and level of improvements.
Here is what the future can bring:
- Further improvements in the Elixir type system.
- Phoenix LiveView getting more robust, release of the first major version, and more investments into the component-based structure of the application.
- Getting more tools and improvements around AI & ML, as well as promoting Elixir as a language that is well suited for these fields.
- More Elixir tooling further improving its productivity.
Is Elixir the right language for your business or career? As with everything, it, of course, depends on what you're looking for. Before deciding, I strongly encourage you to install Elixir and try it out.
I focused this article on a general description of Elixir to provide decision-makers (CTOs, CEOs) and programmers with the most important things you need to know about this language. You may find various learning materials (such as tutorials and books) around the internet to start the journey. If you need a hand, take a look at our article on this topic: Top Elixir media resources.
Finally, I hope there will be many developers like me - afraid of functional programming at first but then discovering that this paradigm is actually amazing (with Elixir taking a lot of credit for that).