Recently, we discussed the history and use cases of Ruby, one of the more popular programming languages in use over the past few decades. Ruby’s focus on simplicity and usability for developers isn’t necessarily a unique philosophy, but the Ruby approach has since spawned a number of similar languages that have built off the initial Ruby model. That’s where Elixir comes in.
While Elixir doesn’t have quite the widespread name appeal of Python or Go, for example, adopters around the world have consistently given it praise and high marks on Stack Overflow developer surveys and other metrics. Built on top of the older Erlang language (which has existed since the 1980s), Elixir combined the popular features of Erlang with some of the Ruby-like language features that make it even more usable for programmers. This is a result of its creator, Brazilian developer Jose Valim, who was a Ruby developer himself and had intimate knowledge of the best features of Ruby to integrate with Erlang.
Because of the combination of functional usage and powerful capabilities, Elixir is seeing growth among developers looking for a souped-up version of the Ruby approach that can handle modern development needs. In this guide, let’s explore the basic structures and fundamentals of Elixir, as well as some of its most popular uses and features in modern web development.
What Are Elixir’s Essential Features?
Elixir is part of a category of development languages known as functional programming languages, which allow programmers to create high-quality code with minimal errors, automatic recovery, and scalability. These benefits are due to some structural features in the design of the language, which include:
Code with Local Reasoning
Unlike a programming language such as Java, where data is mutable (changeable), Elixir features immutable data. Because variables that are passed around within the program can’t be changed in various places (which would be the case in a mutable language), it is significantly easier for programmers to hunt down bugs, since there are only a limited number of locations for a specific bug to appear.
Clarity and Testability
Functional programming languages are popular among developers because they keep things simple and clear. In other words, languages like Elixir emphasize small, concise functions with a clear purpose. For example, when you pass data through any given function, it’s easy for you to understand what data was entered and what was returned. This makes testing easier, because you have a good idea of what the process was and thus don’t need to check a bunch of variables to improve functionality.
Efficient and Effective Refactoring
Refactoring is a term used to describe making significant changes to the existing code base, in some cases without changing the code’s external behavior. Developers often consider refactoring is a risky part of the development process because it’s easy to break something that can cause fundamental damage to your application or set your development schedule back significantly. However, in a functional and immutable language like Elixir, the refactoring and testing process are streamlined. This lets developers make sweeping changes without a risk of wrecking the existing code and having to backtrack or start from scratch.
Process-Based Language
Much as it was in Erlang, Elixir is able to parallel workloads and incorporate multiple processes, which helps minimize the number of changes needed to the code. This is in contrast to single-threaded languages like Java, which are not meant to handle high loads and often require extensive rewrites if the application begins to scale up significantly.
This parallelization is also due to the Phoenix framework, a popular web framework written in Elixir specifically for the use of Elixir web developers. When using Phoenix, each request is given its own (very quick) process with a minimal memory load, which can help increase speed. This also allows developers to create thousands of simultaneous processes without destroying the performance of the app or software.
Intuitive Control
Because a single task can require significant computing power in any parallel system, sometimes responsiveness and latency issues are an unfortunate side effect. However, Elixir tackles this issue by having the process scheduler preemptively shift control to different processes, which means that even if there is extreme load on a given application, a single slow process won’t derail the entire system. In turn, user experience is affected less than alternative systems without this intuitive structure.
Bug Management
The enemy of any developer and app or website manager, bugs in many cases can derail an entire application, causing it to crash or fail and frustrating or confusing countless users. This means languages without inherent structural defenses against this “chain reaction” failure can leave you exposed to a catastrophic application breakdown. However, Elixir contains failure within a single process, meaning that if a single user makes a request with a bug included, it will only crash the app for that user. This feature minimizes damage to the app and your user base from a given bug (and thus provides valuable peace of mind).
How Is Elixir Used?
Elixir is a particularly good choice for systems that are high-traffic, include many connections, or are I/O bound. This description applies to many modern use cases: chat backends (such as platforms like Slack or Facebook Messenger), web servers, APIs, web applications, and even more modern use cases like the Internet of Things or machine learning. Let’s use IoT as an example to show you how Elixir works in an applied setting.
Low-level programming languages such as C have traditionally been the preferred choice for IoT programming — often because low-level languages like C offer developers a lot of control and versatility during the build process. However, these languages are also more complicated than Elixir, and fail more easily. In contrast, Elixir can be used in embedded IoT firmware using the Nerves framework. As a higher-level language, Elixir allows you to write high-quality code more quickly. It also brings the added benefits we noted above — fewer crashes, easy bug avoidance, and simpler coding (the Ruby principles at work).
Here are some other examples of companies that employ Elixir for their own software and web applications:
- Adobe uses Elixir to create a client/cloud application that offers users a collaborative photography workflow.
- Discord uses Elixir for their large-scale messaging system that many people have become more familiar with, especially over the past year. Elixir was specifically useful to Discord to manage the heavy concurrent traffic the app saw on a regular basis.
- Moz utilizes Elixir for the back-end architecture of their digital marketing and SEO toolset, Moz Pro, used by companies around the world.
- Motorola Solutions uses Erlang and Elixir for mission-critical communication systems that need to be reliable and resistant to catastrophic bug crashes.