Systems ProgrammingErlangSystemsConcurrencyHigh Availability
Learn the secrets of building systems with 99.9999999% uptime using Erlang's lightweight processes and fault-tolerant architecture.
getbetterat.work
TUTORIAL: ERLANG
The Language That Never Sleeps.
Erlang is a tool to write computer programs. But not just normal programs. It was built for things that
can never, ever turn off. If you need a program to run for 10 years without a single second of downtime,
you use Erlang.
Why was it built?
Imagine the phone system for an entire country. If a computer running the phone lines crashes,
thousands of emergency calls might drop.
A company named Ericsson needed a way to write code for these phone switches. They needed a system
that could fix its own errors while it was still running. So, they created Erlang.
99.9999999%
The "Nine Nines" Uptime
This means the system is only down for 31 milliseconds per year.
Core Idea 1: Millions of Tiny Tasks
Most computer languages do multiple things at once by using "threads." Threads are heavy. If you make
too many, the computer slows down or crashes. Erlang uses something called processes.
These are so small and light that a single normal computer can run millions of them at the exact same
time without breaking a sweat.
Normal Language
One Big, Heavy Task
If this crashes, everything stops.
Erlang
Millions of tiny, safe tasks.
Core Idea 2: Sending Letters
In many languages, tasks share the same computer memory. Imagine two people trying to write on the exact
same piece of paper at the same time. It gets messy. They bump hands, and the writing gets ruined.
In Erlang, tasks never share memory. Instead, if one task wants another task to do
something, it sends a message. It is exactly like sending an email or a text message.
Process A
"Hello!"
Process B
1
Core Idea 3: "Let It Crash"
This is the craziest part of Erlang. Most programmers spend hours writing code to guess every single
error that could happen, trying to prevent a crash.
Erlang says: "Don't try to catch every error. Just let that tiny task crash."
Because Erlang uses millions of tiny tasks, if one task runs into an error, it just dies quietly. It
does not take down the whole program. The system simply cleans it up and starts a fresh new task to try
again.
Core Idea 4: The Manager Tree
If tasks are crashing, who starts new ones? Erlang uses Supervisors. A supervisor is
just a task whose only job is to watch other tasks. If a worker task dies, the supervisor sees it
instantly and starts a new one. Supervisors can even watch other supervisors. We call this a
"Supervision Tree."
Top Supervisor
Supervisor A
Worker
Worker
Restarting...
Supervisor B
Worker
Worker
Hot Code Swapping
Changing the engine while flying.
Usually, to give a program an update, you have to turn it off, load the new code, and turn it back
on.
Erlang allows you to change the code while the program is still running. It keeps
the old tasks running on the old code, but any new tasks start using the new code. Eventually, the
old code fades away. The system never goes dark.
The BEAM Machine
Erlang code doesn't talk directly to your computer. It runs on a special program called the BEAM
Virtual Machine.
BEAM acts like a strict traffic cop.
It makes sure no single task uses too much computer power.
If one task is being slow, BEAM pauses it and lets another task have a turn. This keeps the
whole system fast and smooth.
Who uses it today?
WhatsApp
Uses Erlang to send billions of messages instantly to users all over the world.
Online Games
Games use it to keep millions of players connected without the server crashing.
Banking
Used to process credit card payments where losing a transaction is not an option.
What does it look like?
Here is a very simple example of an Erlang program that just says Hello.
Don't worry about understanding it all now. Just notice that it looks a bit different from languages
like Python or JavaScript.
Core Idea 5: Fitting the Pieces
In most languages, the equals sign (=) means "make this equal to that." In Erlang, it
means something completely different. It is called Pattern Matching.
It acts like a shape sorter toy for toddlers. If you have a square peg, you can only put it in the
square hole. If you try to match a triangle to a square, the program instantly stops and says "no
match." This catches errors immediately.
X
5
Perfect
Match
Core Idea 6: Written in Stone
In a normal language, a variable is like a whiteboard. You can write "5" on it, then later erase it and
write "10". Erlang does not have whiteboards. It only has stone tablets and a chisel.
Other Languages
Data can be changed anytime. This causes messy bugs if two tasks try to
change the same data at once.
Erlang (Immutable)
Once you save a piece of data, it can never be changed. If
you want a new number, you must make a brand new stone tablet.
Core Idea 7: Computers Talking to Computers
Remember how processes send messages to each other? Erlang takes this a step further. A process on your
laptop in New York can send a message to a process on a server in Tokyo exactly the same way it sends a
message to a process on the same laptop.
Node 1 (USA)
Node 2 (Japan)
The Secret Weapon: OTP
If you write Erlang, you will hear about OTP (Open Telecom Platform). Despite the name,
it has nothing to do with modern telecom anymore. It is just a massive toolbox of pre-built code.
GenServerBuilds safe servers for you
SupervisorBuilds the "Let it crash" manager
ApplicationPackages your code to run perfectly
OTP provides the bulletproof foundation so you don't have to build it from scratch.
Core Idea 8: The Loop That Isn't
Erlang does not have for loops or while loops. Instead, it uses
Recursion. This means a task finishes its job by simply calling itself again from the
very beginning.
Do
Work
Call "Do Work" again
Tiny Trash Cans
When a program runs, it uses memory. When it's done, it has to clean that memory up (Garbage
Collection). In normal languages, a giant garbage truck pauses the entire program to clean
everything at once.
In Erlang, every tiny process has its own tiny trash can.
They clean up their own mess without pausing anyone else. This is another reason Erlang never
"freezes" or lags.
CLEANING
No global pauses.
Core Idea 10: Everyone is an Actor
Erlang's design is known as the Actor Model. Think of an Actor as a person sitting in a
room.
1. Private State
The Actor has a notebook only they can read. No one else can see or touch their private data.
2. A Mailbox
The Actor has a mailbox. Other Actors can drop letters (messages) in the mailbox at any time.
3. One at a time
The Actor reads their mail one letter at a time, does the work, and then checks the next letter.
Free Speed Boosts
Modern computers have many "cores" (mini-brains inside the main chip). Many programming languages
struggle to use all these cores effectively without messy code. Because Erlang processes don't share
memory, the BEAM machine automatically spreads them across every core you have.
1 Core
4 Cores
You just buy a better server, and Erlang instantly uses all
of it. No code changes needed.
Holding on to Memory
If variables are immutable (cannot change), how does a game server remember your score?
A process remembers state by passing its current data to itself in the next loop.
Score: 0
Message: "+1"
Score: 1
Message: "+10"
Score: 11
When to use Erlang
Chat apps, online games, banking, telephone systems, or anything where a crash
means lost money or angry users.
When NOT to use Erlang
Heavy math, 3D graphics, or simple scripts. It is built for moving data safely,
not crunching heavy numbers.
Your Next Step
Look into Elixir. It is a modern language built on top of
Erlang. It has all the exact same powers, but it is easier to read and has modern web tools
(like the Phoenix Framework).
More in this series
Master more skills with other tutorials from the Systems Programming series.