Actor Model in Erlang

Concurrency
- Definition: The ability of an executing program to run decomposed instructions in a parallelized fashion efficiently utilizing available resources.
- Joe Armstrong's definition:

Traditional Concurrency
- Typically through the use of threads.
- Lack of atomicity
- Locking primitives such as semaphores or mutexes are required.
- Can create difficult to program and debug situations, such as deadlock.
Parallelized programming
- Moore's Law
- Use of parallelization becomes more and more attractive
- Amdahl's Law
- A program is only parallelizable in terms of it's slowest part.

Actor Model
- Concurrency in actors is constrained only by the availability of hardware resources and by the logical-dependence inherent in the computation. - Gul Agha
- First proposed by Carl Hewitt in 1973
- Further solidified over many years by a number of other computer scientists and by Gul Agha in his dissertation on the Actor Model in 1985.
Actor Model
- Actors are a concurrency primitive that do not share resources with another actor.
- Actors share state/data via message passing.
- An Actor:
- Has a mailbox and a behavior
-
Takes a message and can:
- Send messages to other actors,
- Create new actors
- Return another actor describing its next behavior
- Process one message at a time
- Actors come in systems
- Everything is an actor
Actor Model Concepts
- No shared state between actors
- Functions asynchronously
- All about message passing for communication
- Only way to share/change state is through this message passing
Enter Erlang...
- The actor model is inherent to Erlang and is built into the language and somewhat fundamental to understanding the distributed nature of Erlang.
- It was designed for the telephony industry, so some of the driving ideals such as reliability demanded that Erlang utilize a model that didn't incur potential for data loss, that is possible in the more traditional concurrency models discussed above.
Erlang...
- An Erlang program describes a series of functions.
-
Each "thread" of execution is a lightweight process.
-
It utilizes pattern matching to process messages.
- Each function uses pattern matching to determine which function to execute.
The Erlang view of the world is that everything is a process and that processes can interact only by exchanging messages. - Joe Armstrong
Creating a process
- To create a process in Erlang, just call `spawn`, which will return a process id (pid)
-module(actor).
...
start() ->
spawn(actor, run, []).1> c(actor).
2> Pid = actor:start().
3> Pid.
<0.80.0>From the shell:
Erlang process
-
Not a process in the OS definition.
-
It's an Erlang process, so it runs in the Erlang VM.
-
It runs in user space, not bound to the kernel, so is scheduled by the Erlang Scheduler.
Process (cont'd)
-
And it's a lightweight process...how lightweight:
-
A newly spawned Erlang process uses 309 words of memory (a word is 4 or 8 bytes)
-
-
Check how much memory our process(es) are using:
-
-
And they are very quick to spawn...from Joe Armstrong:
-
Spawning 20,000 processes took an average of 3.0 microseconds/process of CPU time and 3.4 microseconds of elapsed (wall-clock) time.
-
{_,Bytes} = process_info(actor_file:start(), memory).
{memory,2720}Erlang scheduler
- Erlang uses a preemptive scheduler.
- A preemptive scheduler works by running tasks, preempting, then resuming them based on a specific metric, such as priority, time or reduction.
- Many other async languages, such as Node.js and Python's Twisted, utilize cooperative scheduling, which requires tasks to release themselves voluntarily.
Scheduler (cont'd)
- Erlang's preemptive scheduler uses a reduction count.
- A reduction is typically a function call, garbage collection, message sending, etc.
- Erlang's preemptive scheduler uses a reduction count.
- A reduction is typically a function call, garbage collection, message sending, etc.
- The reduction count for an Erlang process is 2000, which can go quite quickly.
Scheduler (cont'd)
- Processes do and can be flagged with priorities: 'max', 'high', 'normal', and 'low'.
- The scheduler makes Erlang a great choice for low-latency, parallelized programs as it can manage multi-tasking efficiently.
Further Reading
- Gul Agha, Actors: A Model of Concurrent Computation in Distributed Systems
- Ruben Vermeersch, Concurrency in Erlang & Scala: The Actor Model
- Fred Hebert, Learn You Some Erlang: for great good!
- Erlang/OTP System Documentation
- Joe Armstrong, Programming Erlang: Software for a Concurrent World
- Bruce Tate, Crossing borders: Concurrent programming with Erlang
- Hamidreza Soleimani, Erlang Scheduler Details and Why It Matters
- Jesper Louis Andersen, How Erlang does scheduling
Want to speak at the next Meetup?
Actor Model in Erlang
By ralucas
Actor Model in Erlang
- 1,616