Erlang is a programming language used to build distributed, fault-tolerant systems. It was developed in the late 1980s at Ericsson for building telecom applications. Erlang uses lightweight processes to model systems and provides features like process isolation, message passing, distribution, and hot code swapping. The Open Telecom Platform (OTP) is a set of libraries and design principles for building robust Erlang applications. Many companies use Erlang for building scalable and reliable systems, including WhatsApp, Klarna, and Ericsson.
11. Nope, Industrial Requests
Ericsson was looking for a
language to replace PLEX
(their development
language in the 80s)
Requests:
Distributed Declarative
Time to Market
Hot Code Swapping
Fault Tolerant
Soft Realtime Concurrent
12. Research is the key
At the Ericsson Lab they developed some pilot projects in various
programming languages...
Chill
ML Miranda
Others...
SmallT
alk
Prolog
Ada
13. But
There wasn't a language with all those features
Let's create our
language!
14. Good results!
The first version was a language implemented
on the top of Prolog.
After some good results, they wrote the JAM
Machine in C, boosting performance of 70%.
We are in the early 90s, and the “Erlang Era”
has just began.
15. But...
After lots of great results, Ericsson banned
Erlang from future projects in 1998.
They chose to follow competitors...
Ericsson released Erlang as Open Source
Core developers left Ericsson and
founded their companies
16. Ericsson comes back
Understanding the error made, they come back
to Erlang.
Ericsson re-hired Joe Armstrong in 2004
The OTP Team is now based in Ericsson.
20. The Erlang Way
The best way to understand the Erlang way is to
think at the real world.
World is concurrent,
this is the abstraction we want.
21. It is simple
To obtain scalability and fault tolerance is quite
easy. You took 2 things, you make them share
nothing and you get fault tolerance and you can
scale.
Joe Armstrong (London Erlang Factory 2011)
39. Processes and IPC
To create a process we use the spawn functions.
There are different versions of spawn, for all
the possible scenarios.
Every Process has its “MailBox” where
its messages are delivered.
To send a message to a process we use a one
character operator, !
43. How to deal with Errors
Erlang has an Exception system with 3 classes of
errors:
- error: It is a runtime error or it could be
generated from the call error(Reason)
- exit: generated from the code with the call
exit(Reason)
- throw: generated from the code with the call
throw(Reason)
45. Exit Signals
When a process terminates it emits
(with its last breath) an exit signal
with the reason of its termination
The reason could be normal
if things are terminated it the right way,
or it could be something else,
and in this case the termination is abnormal.
We can use processes to isolate errors, make that
part of the system fail fast, and eventually restart it.
46. Links
{'EXIT', Pid2, Reason}
P1 P2
We can link processes together with link/1 BIF or
directly with spawn_link BIFs.
Linked processes forms a linked set, and if one of
them terminates abnormally, the error propagates
the the linked set, taking processes down.
47. Trapping Exits
{'EXIT', Pid2, Reason}
P1 P2
We can trap exits, making a process become a
system process with:
process_flag(trap_exits, true).
Now it receives the message, and it can choose
what to do. Stopping the propagation of the error.
Only the Reason kill is untrappable!
48. Robustness
S
S W W
S → Supervisor
W W W → Worker
49. Monitors
While links are bidirectional,
monitors are unidirectional.
No link set, the monitor process is like a
“stalker”.
51. Are you kidding?
Nope, think about it...
No shared memory.
So, we copy data between processes
on the same machine.
And...
We copy data between processes
on different machines.
52. Ok, and the location?
! operator is location transparent
A Pid also contains information on the
location of the process.
53. Erlang Clusters
It is really simple to create
an Erlang cluster.
There are apposite libraries,
and when a cluster is set,
you work without headache.
54. Hot Code Swapping
No Downtime allowed
for bad guys like us!
So...
Hot Code Swapping is built into the
language itself!
56. How does it woks?
Instant 1
P
-vsn(1.0) -vsn(1.1)
57. How does it woks?
Instant 2
P
-vsn(1.0) -vsn(1.1)
58. How does it woks?
Instant 3
P
-vsn(1.0) -vsn(1.1) -vsn(1.2)
59. Warning!
Code upgrades on intra-module function
calls could bite you.
The function calls must be
fully qualified calls such as:
<module_name> : <function_name>(<args,....>)
If you follow OTP Principles you can get
Hot Code Swapping
the right way “without” headache
60. Warning!
Hot Code Swapping is a difficult task
Even if Erlang helps you...
Test Hard your solutions
before taking the system down.
61. ERTS and the VM
No clear separation between
ERTS and VM.
VM runs compiled byte-code (BEAM)
ERTS manages Processes, IPC, Memory,
Load Balancing, ecc...
We always call VM both two!
62. The Scheduler
Fundamental part of the
Erlang Runtime System.
It has been deeply changed during the
last years to rule multi-core hardware
the right way
63. In the past
The scheduler was there to manage
execution of Erlang processes inside an
O.S. Process called beam or werl
Run Queue
Scheduler
64. Then...
Symmetric Multiprocessing was
released in May 2006
Scheduler #1 Run Queue
Scheduler #..
Scheduler #N
65. Today
Bottleneck removed.
One run queue for scheduler.
Scheduler RQ
#1
Scheduler RQ
#... Migration
Logic
Scheduler RQ
#N
66. Garbage Collection
Per process Garbage Collection
Small processes means
quick garbage collection
Garbage Collection doesn't pause the system
as happens in other languages.
Soft Real-time feature is safe even with
million of processes running
67. OTP: Open Telecom Platform
Set of tools, libraries and design principles to
develop distributed applications
Do not reinvent the wheel every time, use
OTP behaviours instead!
Supervisor trees, Hot Code Swapping,
Packaging, abstracted away
68. OTP Benefits
Less code to write means, less bug and fast
time to market
Easy to test your specific functionality
Common coding style between developers
OTP is battle tested