Logo

dev-resources.site

for different kinds of informations.

Elixir: Concurrency & Fault-Tolerance

Published at
12/2/2024
Categories
beginners
elixir
Author
sandrockjustin
Categories
2 categories in total
beginners
open
elixir
open
Author
14 person written this
sandrockjustin
open
Elixir: Concurrency & Fault-Tolerance

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 of main() 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.

elixir Article's
30 articles in total
Favicon
A RAG for Elixir in Elixir
Favicon
Enhancing Elixir Development in LazyVim: Quick Documentation Access by Telescope
Favicon
Learning Elixir: Control Flow with If and Unless
Favicon
Pseudolocalization in Phoenix with gettext_pseudolocalize
Favicon
The Journey of Optimization
Favicon
How to use queue data structure in programming
Favicon
Phoenix LiveView, hooks and push_event: json_view
Favicon
🥚 Crack Open These 20+ Elixir Goodies
Favicon
Learning Elixir: Understanding Atoms, Booleans and nil
Favicon
Unlocking the Power of Elixir Phoenix and Rust: A Match Made for High-Performance Web Applications
Favicon
Elixir: Concurrency & Fault-Tolerance
Favicon
Enhancements to dbg in elixir 1.18
Favicon
Learning Elixir: Working with Strings
Favicon
Leverage ETS for Shared State in Phoenix
Favicon
Elixir em Foco em 2024
Favicon
Building HTTP/JSON API In Gleam: Introduction
Favicon
Phoenix
Favicon
Sql commenter with postgrex
Favicon
Learning Elixir: Understanding Numbers
Favicon
For loops and comprehensions in Elixir - transforming imperative code
Favicon
Phoenix LiveView is slot empty?
Favicon
Customizing IEx: Personalizing Your Elixir Shell Environment
Favicon
Masters of Elixir: A Comprehensive Collection of Learning Resources
Favicon
Leveraging GenServer and Queueing Techniques: Handling API Rate Limits to AI Inference services
Favicon
Chekhov's gun principle for testing
Favicon
Solving Advent Of Code 2024 on a elixir project
Favicon
Bridging the Gap: Simplifying Live Component Invocation in Phoenix LiveView
Favicon
Harness PubSub for Real-Time Features in Phoenix Framework
Favicon
Debugging with dbg: Exploring Elixir's Built-in Debugger
Favicon
New to dev.to and Excited to Share ProxyConf: My Elixir-Powered API Control Plane

Featured ones: