What I Was Looking for in Elixir
It wasn’t the syntax that brought me to Elixir but the execution model of the Erlang VM – processes, messages and supervisors, seen through JavaScript and C# eyes.
What I Was Looking for in Elixir
You try out a new programming language because you like its syntax – that’s the usual reasoning. Shorter notation, more elegant lambdas, less ceremony. I think this reasoning is backwards. You get used to syntax in two weeks; it is the most interchangeable part of a language. Things get interesting where a language executes differently: how it organizes concurrency, who owns state and what happens when something breaks.
That is exactly why a small Elixir repository has been sitting in my GitHub account since last fall. Not because I liked the Ruby-like dress of the language – at first I almost didn’t care about that – but because the Erlang VM is at work under this dress. And it answers the three questions above radically differently from what I know from my daily work with JavaScript and C#: processes instead of threads, messages instead of shared state, “let it crash” instead of defensive try/catch at every corner.
A look over the fence, with a period flavor#
The timing for this look over the fence is good. In February, Facebook bought WhatsApp for a sum everyone is still talking about – and suddenly even the management floor is interested in the fact that a surprisingly small team there serves several hundred million users. The backend: Erlang. The numbers that have been circulating from this corner for years – millions of concurrent connections on a single machine – sounded like folklore to me for a long time. Now people are taking a closer look, and so am I.
Elixir builds on exactly this machine. José Valim, known to many from the Rails world, is building a language with it that runs on the Erlang VM, can call Erlang modules directly and inherits the platform’s tools. The language is still young: currently version 0.13, on the way to 1.0, with occasional breaking changes between releases. Dave Thomas is writing “Programming Elixir” for the Pragmatic Programmers, available as a beta so far. So if you touch Elixir today, you are deliberately touching something unfinished – the platform underneath, by contrast, has been proven in production for more than twenty years.
A repository full of questions#
My entry point was not a project but a collection of small experiments: github.com/MikeBild/tryelixir, started in October, grown in evening hours. No README, no architecture, just folders – each folder a question I wanted to ask the language:
actors– what dospawnandreceive, the tools of the trade for processes, feel like?bankAccount– can a single process manage the state of an account on its own, without any lock?cowboy– how do you speak HTTP on the Erlang VM, with the Cowboy web server?kataTennisandkataTennisFSM– the well-known tennis kata, once in the classic way, once as a finite state machineleveldb– how do you bind a library like LevelDB?
I don’t claim more about the repository, because it isn’t more: a box of experiments. But the account exercise in it works well to show what grabbed me about the execution model.
Processes instead of threads#
First, the starting point. In my daily C# work, a bank account with concurrent access looks roughly like this – shared state, protected by a lock:
public class BankAccount
{
private readonly object _sync = new object();
private decimal _balance;
public void Withdraw(decimal amount)
{
lock (_sync)
{
if (_balance < amount)
throw new InvalidOperationException("insufficient funds");
_balance -= amount;
}
}
}
That works – as long as every access goes through the same lock, nobody takes two locks in different orders and no exception leaves the state half-changed. In code reviews, these are exactly the places where I get stuck the longest, and the bugs that slip through there only show up under load. In JavaScript, the question arises differently, but not better: a single thread with callbacks does protect against race conditions in memory, but in exchange a single expensive computation blocks everything else, and an uncaught error takes the whole Node process down with it.
The Erlang VM organizes the problem in a fundamentally different way. Its processes are not operating system threads but tiny units managed by the VM: a few kilobytes at startup, each with its own heap and its own garbage collection, preemptively scheduled. Hundreds of thousands of them on one machine are normal, not heroic. Above all, though: there is no shared memory between them. What a process knows, only it knows.
The same account in Elixir is a process that carries its balance along as the argument of an endless loop:
defmodule BankAccount do
def start(initial_balance) do
spawn(fn -> loop(initial_balance) end)
end
defp loop(balance) do
receive do
{:deposit, amount} ->
loop(balance + amount)
{:withdraw, amount, caller} when amount <= balance ->
send(caller, {:ok, balance - amount})
loop(balance - amount)
{:withdraw, _amount, caller} ->
send(caller, {:error, :insufficient_funds})
loop(balance)
{:balance, caller} ->
send(caller, {:balance, balance})
loop(balance)
end
end
end
No lock, no synchronized, no pondering over volatile. The balance can’t be changed from two sides at the same time, because only a single process owns it and works through the messages in its mailbox one after another. Concurrency doesn’t come from more threads on the same memory but from more processes, each with its own state – one process per account, not one lock per account.
graph LR
subgraph SHARED["Threads and shared state"]
T1["Thread 1"] --> L["Lock"]
T2["Thread 2"] --> L
L --> MEM["shared memory<br/>balance"]
end
subgraph MSG["Processes and messages"]
P1["Process A"] -- "{:withdraw, 30}" --> MB["Mailbox"]
P2["Process B"] -- "{:deposit, 50}" --> MB
MB --> ACC["BankAccount process<br/>own state: balance"]
endMessages instead of calls#
Seen from outside, such an account is not an object with methods but an address to which you send messages:
account = BankAccount.start(100)
send(account, {:deposit, 50})
send(account, {:withdraw, 200, self()})
receive do
{:ok, new_balance} ->
IO.puts "withdrawal ok, new balance: #{new_balance}"
{:error, reason} ->
IO.puts "withdrawal failed: #{reason}"
end
The difference from a method call is bigger than it looks. A call is synchronous, free to think about and expensive in its consequences: the caller depends on the callee succeeding and shares stack and fate with it. A message is asynchronous and decoupled – whether and when an answer comes is a separate decision that becomes visible in the code. When you switch, this feels cumbersome at first. But it made me notice how many design decisions a method call silently makes that I had never thought about before.
If you don’t want to knit every protocol by hand, you reach for the platform’s OTP library: gen_server pours exactly this pattern – state in a loop, synchronous and asynchronous messages, orderly start and stop semantics – into a reusable form with callbacks. Writing the account exercise by hand was still the right way: only once you have built the receive loop yourself do you understand what OTP takes off your hands.
“Let it crash” – and who cleans up then?#
The part that challenges my thinking the most is how errors are handled. My learned reflex from years with C# and JavaScript: errors must not happen, so I defend myself – null checks, try/catch around every layer, fallback values, logging and muddling on. I know the result from quite a few codebases, my own included: the error doesn’t get fixed, it gets smeared. Somewhere a catch block catches too much, the program keeps running with half-broken state, and the real bang comes hours later at a place that no longer has anything to do with the cause.
The Erlang world draws a conclusion from this that sounds reckless at first: let the process crash. No defensive catching inside the process itself – if a message doesn’t match the expected pattern or a computation fails, the process dies. This is only possible because of isolation: a dying process doesn’t drag any shared memory down with it; there is no half-changed state it could leave behind. Cleanup happens elsewhere – by a supervisor, a process whose only job is to watch other processes and restart them after a crash with fresh, defined state. Supervisors also watch other supervisors, and this produces trees:
graph TD APP["Application"] --> SUP["Supervisor<br/>strategy: one_for_one"] SUP --> A1["BankAccount process<br/>account 1001"] SUP --> A2["BankAccount process<br/>account 1002"] SUP --> A3["BankAccount process<br/>account 1003"] A2 -. "crashes" .-> SUP SUP -. "restarts,<br/>fresh state" .-> A2
This turns error handling from a question of coding style into a question of architecture. No longer: where do I catch the exception? But: which unit may die, and who restarts it in which state? The answer isn’t scattered across a thousand catch blocks but sits in one place in the supervision tree. Joe Armstrong spelled out this idea in his dissertation – reliable systems despite faulty software, not through error-free software. While reading it, I kept thinking of production incidents where exactly this was missing: not fewer errors, but a defined way back to a known state.
The syntax as a bonus#
And in the end I do like the syntax after all – but as a bonus, not as the reason. Three things stand out.
Pattern matching is already visible above: the receive branches are not an if cascade over a message type but patterns with conditions, and the structure of the data documents itself exactly where it is processed. That = in Elixir is not an assignment but a match operator tripped me up at first and then convinced me.
Data is immutable. There is no balance -= amount; there is only a new value that moves into the next loop iteration. What remains a question of discipline in C# – please don’t modify shared objects – simply can’t be expressed here. Entire classes of bugs from my reviews don’t exist in this language.
Finally, the pipe operator |> makes transformation chains readable, without intermediate variables and without calls nested from the inside out:
"transactions.csv"
|> File.read!
|> String.split("\n")
|> Enum.map(&String.strip/1)
|> Enum.reject(&(&1 == ""))
|> Enum.map(&String.split(&1, ";"))
Each line a stage, the data flows from top to bottom. I have been writing the ES5 equivalent with a for loop and helper arrays for years without thinking about it – only the side-by-side comparison showed me how much noise is in it.
What makes its way back into my daily work#
Elixir won’t become my production language in the foreseeable future. The projects I’m involved in are .NET and Node landscapes, and I won’t drag a pre-1.0 language into them. Still, the experiment wasn’t an end in itself, because I take some patterns of thinking back over the fence with me:
- Small units that are ready to crash. A Node service that starts in seconds and that a process watchdog simply brings back up after a crash is superior to the big process that has to survive every error. A fast restart with fresh state beats heroically running on.
- Messages instead of calls at the boundaries. Where two parts of a system should live and die independently of each other, a queue belongs in between, not a direct synchronous call – the decoupling that Erlang processes get out of the box is something I have to buy there with infrastructure.
- State has an owner. Even without Erlang processes, a module can be the only place where a piece of state is changed, while everyone else talks to it only through defined messages or commands.
- Immutable data wherever the language allows it – even if only as a team convention.
Just as important is what does not come along. Rebuilding a supervisor in C# or JavaScript is tempting and leads you astray: without isolated processes with their own heap, “let it crash” is not a strategy but recklessness – the crashing thread does leave shared state behind after all, and the restart doesn’t start from zero. The model holds as a whole or not at all. Imitating OTP in another runtime would mean copying the facade without the foundation.
Bottom line#
What I was looking for in Elixir was not a prettier language but a different relationship to the two things that cause me the most work in projects: state and errors. What I found is an execution model that doesn’t solve both problems better but frames them differently – state has exactly one owner, and errors have a defined area in which they are allowed to die. That a pleasant, young language sits on top makes experimenting easier; everything is carried by the Erlang VM underneath.
The little repository stays where it is and keeps growing when the next question comes up. And I’m watching Elixir itself on its way to 1.0 – not because I’m going to build an account system on it tomorrow, but because this look over the fence has taught me more about my own tools than many a framework release on this side of it.
Further reading#
- github.com/MikeBild/tryelixir – my Elixir experiments
- elixir-lang.org: Getting Started – the official introduction to the language
- Erlang/OTP Design Principles – supervision trees and behaviours in the official Erlang documentation
- Joe Armstrong: Making reliable distributed systems in the presence of software errors – the dissertation behind “let it crash”
Revised and translated from German with the help of AI.