Skip to content

Elixir

Elixir is a functional, dynamically typed language on the Erlang VM (BEAM). Its selling point is not syntax: it is the runtime — lightweight processes, preemptive scheduling, supervision trees and a “let it crash” philosophy that produces systems which degrade instead of falling over.

“Elixir runs on BEAM. The unit of concurrency is a process — not an OS process, a BEAM process of a couple of kilobytes — so you can run hundreds of thousands of them, each with a mailbox, communicating only by message passing. Data is immutable, so there are no locks and no shared-memory bugs. Pattern matching replaces most conditionals: = is a match, not an assignment. OTP supplies the patterns — GenServer for state, Supervisor for restart strategy — so a crash is contained to one process and restarted by its supervisor instead of taking down the node. Phoenix is the web framework, LiveView does real-time UI over WebSockets without a SPA, and Ecto is the database layer. You would choose it for high concurrency, soft real-time, fault tolerance and long-lived connections.”

Ruby (CRuby) Elixir (BEAM)
Concurrency unit OS thread, GVL limits parallelism BEAM process, preemptively scheduled
Cost per unit ~1 MB stack, thousands at most ~2 KB, hundreds of thousands
Isolation Shared memory, locks needed No shared memory; message passing
Failure An exception can kill the process A process crashes; supervisor restarts it
Scheduling Cooperative threads; GC pauses Preemptive, per-process GC, no global pause
Distribution External (Redis, queues) Built-in node clustering

That table is the honest answer to “why Elixir”. It is not faster at raw computation — it is concurrent and fault-tolerant by design rather than by library.

A process has a mailbox, and receive blocks until a message matches a pattern. That is the whole concurrency primitive — no locks, no shared state. Everything is built on it: a GenServer is a process with a receive loop, a Phoenix channel is a process per connection, a LiveView is a process per browser tab.

defmodule Counter do
use GenServer
def start_link(initial), do: GenServer.start_link(__MODULE__, initial, name: __MODULE__)
@impl true
def init(initial), do: {:ok, initial}
@impl true
def handle_call(:value, _from, count), do: {:reply, count, count}
@impl true
def handle_cast(:increment, count), do: {:noreply, count + 1}
end
GenServer.cast(Counter, :increment) # asynchronous
GenServer.call(Counter, :value) # synchronous, waits for a reply

State lives in the process and is only ever replaced, never mutated in place.

user = %{name: "Ada", role: :admin}
%{name: name} = user # match: binds name to "Ada"
%{role: :admin} = user # succeeds; a non-matching value raises MatchError
def greet(%{role: :admin, name: name}), do: "Admin #{name}"
def greet(%{name: name}), do: "User #{name}"
case Repo.get(User, id) do
%User{active: true} = user -> {:ok, user}
%User{} -> {:error, :inactive}
nil -> {:error, :not_found}
end

= is a match operator, not assignment. ^ pins an existing value: ^expected = actual asserts equality instead of rebinding. Function heads dispatch on pattern and guard, which removes most if/elsif chains and is_a? checks. An “update” is a copy with one key changed and the old value remains valid — which is what makes concurrency safe without locks.

children = [
{Counter, 0},
{MyApp.Repo, []},
MyAppWeb.Endpoint
]
Supervisor.start_link(children, strategy: :one_for_one, name: MyApp.Supervisor)
  • :one_for_one restarts just the failed child; :one_for_all restarts all siblings; :rest_for_one restarts the failed child and everything started after it.
  • A supervisor restarts a crashed child up to a maximum intensity within a period; beyond that the supervisor itself dies and its parent escalates.
  • “Let it crash” means do not write defensive code for states you cannot handle: let the process die and be restarted into a known-good state. Process isolation plus the supervision tree makes that safe.

This is the biggest mental shift from Rails. There is no global rescue_from; there is a tree of processes where failure is local and recovery is automatic.

  • Phoenix — router, controllers, channels. Plug is the middleware abstraction, the Rack equivalent.
  • LiveView — server-rendered HTML over a persistent WebSocket; state lives on the server, diffs are pushed to the client, no separate API or JS framework.
  • Ecto — the database layer. Unlike Active Record it separates schema from changesets: a Changeset is an explicit, composable validation pipeline you can pass around and test.
  • Oban — background jobs backed by PostgreSQL, comparable to GoodJob or Solid Queue.
  • Mix — build tool, task runner and dependency manager.

Good: many concurrent connections (chat, presence, notifications, collaborative editing, IoT); soft real-time with strict latency, because scheduling is preemptive and GC is per-process; fault tolerance as a requirement; distributed systems, since nodes cluster natively; Phoenix LiveView for rich UI in one language with no SPA.

Poor: CPU-bound number crunching (use NIFs or another service); a need for a large pool of existing experts; a team that must ship next month and has never written a functional language; heavy ML tooling, where Python’s ecosystem is deeper.

  1. What is the difference between a BEAM process and an OS thread?
  2. Why does immutability make concurrency easier, and what does it cost?
  3. What does a supervisor do when a child crashes repeatedly?
  4. What is “let it crash” actually saying, and when is it wrong?
  5. How does LiveView avoid needing a JSON API?
  • “Elixir is fast.” It is concurrent and predictable; raw throughput per core is not the pitch.
  • Writing Ruby in Elixir. Mutating data, if instead of pattern-matched heads, exceptions instead of tagged tuples.
  • Swallowing errors to avoid crashes. The model depends on failures being visible to the supervisor.
  • Assuming processes are free. They are cheap, not free; a process per request still exhausts database connections.
  • Ignoring hiring and ecosystem risk. It is a real cost, and naming it signals maturity.