Elixir, in the words of its creators, is “a dynamic, functional programming language designed for building scalable and maintainable applications.” Because it’s a relatively new language, first appearing in 2011, many people have not yet heard of Elixir or are reluctant to use it.
The good news is that Elixir is built on top of Erlang, another functional programming language that has been around for more than 3 decades. Elixir is able to leverage any Erlang library, which means that developers have a lot to work with despite the language’s newness.
What is Elixir?
Elixir was created by José Valim, a Brazilian developer who used to work with the Ruby programming language. One of the advantages of Ruby is that it has generators and templates to quickly create a skeleton of your project so that you can get up and running quickly. However, Ruby struggles to perform well under high load. Elixir was born by leveraging lessons learned from languages like Ruby with the runtime characteristics of Erlang.
Using Elixir: 6 Things You Should Know
As a functional programming language, Elixir allows you to quickly write high-quality code that will scale easily, fail minimally, and recover automatically. Below, we’ll discuss some of Elixir’s standout features.
Local reasoning of code
In programming languages such as Java, data is mutable. Variables that are passed around within the program can be changed in various places, which makes it more difficult to hunt down bugs. This isn’t an issue with Elixir, however, because the language is functional and data is immutable.
Conciseness and testability
Functional programming languages such as Elixir emphasize small, concise functions with a clear purpose. When you pass data through a function, it’s easy to understand what you put in and what you get out. This facilitates testability and allows you to make basic changes more quickly.
Fast and confident refactoring
Refactoring—making significant changes to the existing code base, sometimes without changing the code’s external behavior—is often a “dangerous” part of development because it’s all too easy to break something. In functional, immutable languages such as Elixir, however, refactoring and testing are much simpler. You can make sweeping changes without fear of accidentally making things worse.
At Very, we practice test-driven development (TDD). All tests are written before writing a single line of code. Once these tests (expectedly) fail, you write the code that makes them pass. This process ensures that you have tests for your code, which makes refactoring easy and fast.
Being able to parallelize workloads and use multiple processes is native to Elixir, a trait that it inherits from Erlang.
Normally, code that is single-threaded in languages such as Java and not meant to handle high loads will need to be rewritten once the service starts to scale. In Elixir, you’ll need to make a few changes here and there, but much less than in other programming languages.
Elixir makes use of the Phoenix framework, a web framework written in Elixir for Elixir developers. With Phoenix, each request is given its own process that’s extremely fast with a low memory footprint. This allows you to create thousands of processes quickly without worrying about their impact on performance.
Parallelism vs. concurrency
Parallel systems can have latency and responsiveness issues because a single task will require a lot of computing power. In Elixir, however, the process scheduler will preemptively shift control to a different process. Even when your application is under an extreme load, a single slow process can’t hinder the entire system.
As a result, Elixir almost guarantees low latency for your application. This is highly important for modern web applications that need to provide a consistent experience to all visitors.
With many programming languages, a bug in one process will cause your entire application to crash and fail. In Elixir, however, failure is contained to a single process. If one user in a web application makes a request with a bug in it, that process will crash but all other users will remain connected. Elixir enables you to limit the “blast radius” when a process fails.
When Should You Use Elixir?
First, let’s discuss when you should not use Elixir. Systems that are CPU-bound by an inherently sequential task are not a good match for Elixir. If an individual task is difficult to compute and requires serious CPU power, such as advanced mathematical problems, then you should look to another programming language.
However, nearly any use case that is not CPU-bound, and not inherently sequential, is probably a good fit for Elixir. In particular, Elixir works well for systems that are high-traffic, have many connections, or are I/O bound. This description applies to many modern use cases: chat backends (e.g. Facebook Messenger and Whatsapp), web servers, APIs, web applications, and more.
Elixir and IoT
Low-level programming languages such as C have traditionally been the preferred choice for IoT (Internet of Things) development. Although these low-level languages are powerful and give you a lot of control, they’re also difficult to work with and easy to fail.
Elixir can also be used in embedded firmware for IoT devices using the Nerves framework. As a higher-level language, Elixir allows you to write high-quality code more quickly. Here at Very, for example, we recently used Elixir to build an IoT self-serve beer kiosk powered by facial recognition.
Elixir is a powerful and dynamic programming language that builds on the strength of predecessors such as Ruby and Erlang. It’s an excellent choice for any situation where performance and scalability are at a premium, including web applications and IoT projects.
To learn more about Elixir and if it could be a good fit for your project, click here to get in touch with a member of our team.