Why was Elixir developed?
Elixir was created in 2012 as part of a research and development project; Elixir was intended to be a productive and extensible language for writing software that would be both maintainable and reliable. It aims to enhance compatibility, productivity, and extensibility.
What is Elixir used for?
Elixir is a functional programming language that is used for building scalable and maintainable applications. Elixir code runs in lightweight threads of execution (processes) that run concurrently in isolation but can exchange information; the language is also extensible in that developers can extend the language as necessary. According to the Elixir website:
Elixir runs on the Erlang VM, known for creating low-latency, distributed, and fault-tolerant systems. These capabilities and Elixir tooling allow developers to be productive in several domains, such as web development, embedded software, machine learning, data pipelines, and multimedia processing, across a wide range of industries.
What makes Elixir different?
First, we need to explore what it means for something to be a thread. A thread is a single sequence of instructions for a computer to execute. In many programming languages, multiple threads are used to handle different tasks at once, with each thread executing a separate sequence of instructions.
However, in a language like JavaScript, the main execution thread handles all the code in sequence, ensuring that each task gets processed one at a time. Our synchronous code is added to the call stack for immediate execution, while asynchronous operations are offloaded to the environment and their results are queued for later processing by the event loop. In this sense, JavaScript achieves pseudo-concurrency—the illusion of performing multiple tasks at once—even though it runs on a single thread.
In contrast, Elixir operates as a multi-threaded language, where tasks are spread across multiple threads and executed in isolated processes. As a result, Elixir doesn't need to offload operations to an external environment. Elixir achieves true concurrency by design; multiple tasks are executed simultaneously on separate threads. Whenever necessary, these processes can communicate with one another by passing "messages" to share data.
Elixir's multi-threaded model allows it to handle many tasks at once, making it ideal for large-scale applications like real-time messaging platforms, databases, and distributed systems. Therefore it is better suited for concurrent and fault-tolerant systems, especially when system scalability and reliability are key priorities.
What is "fault tolerance", and why is it important?
Fault tolerance refers to a system's ability to handle errors, failures, or exceptions without crashing entirely. If you’re familiar with JavaScript, you’ve likely encountered errors that could potentially bring down your entire application. This raises the question: is fault tolerance always a good thing?
Erlang embraces the reality that errors are inevitable in software development and production, and its fault tolerance philosophy gave rise to the motto "Let It Crash." Elixir, which builds on this philosophy, adopts the same approach. I’d like to acknowledge Fred Hebert, who explains this philosophy in his article The Zen of Erlang:
When you write a C program, you have one big
main()
function that does a lot of stuff. This is your entry point into the program. In Erlang, there is no such thing. No process is the designated master of the program. Every one of them runs a function, and that function plays the role ofmain()
within that single process.So the question becomes to figure out how do we ensure that crashes are enablers rather than destructors. The basic game piece for this in Erlang is the process. Erlang's processes are fully isolated, and they share nothing. No process can go and reach into another one's memory, or impact the work it's doing by corrupting the data it operates on. This is good because it means that a process dying is essentially guaranteed to keep its issues to itself, and that provides very strong fault isolation into your system.
In Elixir, like Erlang, fault tolerance is achieved by leveraging lightweight, isolated processes. These processes are the building blocks of the system, and each one is responsible for executing a single task. The key idea is that processes in Elixir are completely isolated from each other. If one process fails, it does not affect others because each has its own memory and execution space. Let us take another look at Hebert's article on this topic:
Message passing is the most intuitive form of communication in a concurrent environment there is. It's the oldest one we've worked with, from the days where we wrote letters and sent them via couriers on horse to find their destination. A critical aspect of all this message passing, especially in the olden days, is that everything was asynchronous, and with the messages being copied.
No one would stand on their porch for days waiting for the courier to come back, and no one (I suspect) would sit by the semaphore towers waiting for responses to come back. You'd send the message, go back to your daily activities, and eventually someone would tell you you got an answer back.
This is good because if the other party doesn't respond, you're not stuck doing nothing but waiting on your porch until you die. Conversely, the receiver on the other end of the communication channel does not see the freshly arrived message vanish or change as by magic if you do die. Data should be copied when messages are sent. These two principles ensure that failure during communication does not yield a corrupted or unrecoverable state. Erlang implements both of these.
Elixir allows for isolated processes to run concurrently, with the option to communicate with each other by passing messages. This approach not only ensures that errors in one process don't bring down the entire system, but it also supports a highly concurrent and scalable environment. When a process crashes, it doesn’t leave the system in an inconsistent or unstable state.
How can I get started with Elixir?
Elixir has extensive documentation that can help guide you towards installing the necessary packages to begin using it on your system or within a project.
Top comments (0)