Elixir is a functional, dynamically typed language that is built on top of Erlang’s VM and compiles down to Erlang bytecode.
Since its release in 2011, it has become one of the most loved languages, according to the Stack Overflow Developer Survey. And for good reason. Elixir applications are scalable, very reliable, and the syntax is also quite nice.
While the language has made headway in highly concurrent web applications, some of our readers might not yet be familiar with it. In this article, we will give a brief introduction to what Elixir is and why one would use it for a software project in contrast to technologies like Java, Ruby, and others.
Every discussion of Elixir must inevitably start with a discussion of Erlang.
Erlang is a rather old language, created in the 1980s. Its purpose was to solve the problem of reliable phone switching, which it did almost perfectly. Erlang provided tools for creating code that is highly scalable, concurrent, with incredible uptime and fault tolerance.
To make this wonderful toolset more accessible, José Valim created Elixir.
Elixir was intended to be a Ruby-like language that would give developers access to all the powerful tools that Erlang gives to their developers for doing parallel and concurrent computation. Elixir was to include features that were necessary but missing in Erlang.
In that sense, it succeeded. While Erlang and Elixir are both still used widely in their respective fields, Elixir is much more popular and gaining popularity.
You can learn more about the history of Elixir and its predecessor, Erlang, from our History of Erlang and Elixir blog post.
The Elixir programming language wraps functional programming with immutable state and an actor-based approach to concurrency in a tidy, modern syntax. And it runs on the industrial-strength, high-performance, distributed Erlang VM. But what does all that mean? — Dave Thomas, Programming Elixir
Elixir is built on top of the Erlang VM. Elixir has access to all the concurrency tools that Erlang has access to, making it one of the most powerful modern languages for building scalable, distributed systems.
Elixir has Ruby-like syntax. If you have already programmed in Ruby, the syntax of Elixir will feel very familiar. Ruby is one of the most concise and productivity-oriented languages out there, but it is lacking in performance and concurrency. Erlang VM solves both of these problems, and, therefore, Elixir is the best of both worlds.
Elixir is functional. While Elixir and Erlang could be characterized as being in a group of their own (they are both process-oriented), Elixir also has all the constructs you expect from modern FP languages. In particular, immutable data structures help concurrency quite a lot, and pattern matching is great for writing declarative code.
Elixir has dynamic typing. Elixir has dynamic typing in contrast to other functional languages like Haskell and Scala. This means that types are checked in run-time, not during compilation. While this can be a downside when building critical systems, it also increases development speed for simple web applications. Static types can be introduced in Elixir through typespecs.
Elixir is built on top of BEAM, the Erlang VM, and it shares the same abstractions that have made Erlang one of the best choices for concurrent, high-volume applications.
With Erlang, Elixir shares these three characteristics:
Concurrency. Elixir uses lightweight threads of execution (called processes). These are isolated, run across all CPUs, and communicate through messages. Together with the immutability of data that comes from the functional nature of the language, this makes it less hard to write concurrent programs in Elixir.
Scalability. These same processes allow us to scale systems easily, either horizontally (adding more machines to the cluster) or vertically (using the available resources of the machine more efficiently).
Reliability. Elixir and Erlang have a unique approach to fault-tolerance. While things sometimes inevitably fail in production, lightweight processes can be quickly restarted by the supervisor system. This deals with most bugs that are not due to serious faults in software development.
From Erlang, Elixir also gets the Open Telecom Platform. OTP is a part of Erlang's standard library that was used to achieve fault-tolerance in telecom systems, but its usage nowadays is much wider – one can use it for basically any type of concurrent programming.
Compared to conventional solutions, OTP gives a great foundation so that the developer can spend time on implementing the actual business logic. With other languages, a lot of time is spent on plumbing.
Besides, Elixir is very much a functional programming language.
Functional programming is a programming paradigm that treats programs like evaluations of mathematical functions and avoids things like mutable data and changing state.
In contrast to mainstream programming languages like Java or Python, Elixir code is structured in functions and modules (groups of functions) instead of objects and classes. In addition, all the data types are immutable. For example, calling a function on a variable will produce a new variable, not change the variable in place.
Functional programming supports and takes advantage of things like pattern matching, higher-order functions, and declarative style of writing code, and that comes with multiple benefits:
- better maintainability;
- clearer testing and debugging;
- easier handling of concurrent programs;
- high-level abstractions that make code simpler to write and understand.
For more info on the benefits of functional programming, check out our Introduction to Functional Programming.
If you want to use the best web framework in the world, you will have to use Elixir.
Phoenix is as easy to use as any of your regular MVC web frameworks (like Rails and Django), but it is also backed up by the BEAM. It enables one to efficiently churn out web apps while not compromising on either speed or maintainability.
Turns out, the lightweight processes of Elixir/Erlang are great for solving problems of concurrency and scalability in web services. While Phoenix Channels can achieve 2 million websocket connections on a single box, the capacity can be multiplied by distributing the traffic to more nodes.
It has downsides too, though. The ecosystem of Phoenix and Elixir is not as mature as for Ruby on Rails or Python’s Django, so one might run into the problem where “not all batteries are included”. But all in all, the benefits outweigh the costs.
Here are some useful resources on Phoenix to check out:
- Phoenix a Web Framework for the New Web
- Introduction to The Phoenix Framework & How It Works in Action
- Phoenix: an Intro to Elixir's Web Framework
Elixir is used by a ton of companies:
- Adobe used Elixir to build a client/cloud application for a collaborative photography workflow.
- Discord uses Elixir for their large-scale messaging system that you have probably heard about. 5 million concurrent users? No worries.
- Moz uses Elixir for the backend of their digital marketing and SEO toolset, Moz Pro.
- Financial Times has used Elixir for developing multiple applications.
- Motorola Solutions uses Erlang and Elixir for mission-critical communication systems that need to be reliable and fault-tolerant.
Many more also use Erlang, such as WhatsApp and Heroku.
As we can see from the above companies, Elixir enables to write fault-tolerant, scalable code for concurrent systems, and as such, it is perfect for messaging systems and web applications that might need to handle a lot of users efficiently.
If you want to apply Elixir to your projects but don’t know where to start, shoot us a message.