Elixir — Notes on Processes
I have recently started reading Elixir in Action. While I would not recommend the book for those new to Elixir, I definitely think it is a great resource for those that want to learn about what makes Elixir / Erlang special and what programming constructs are used to achieve concurrency, fault-tolerance, and scalability.
Saša Jurić does an incredible job of introducing how Elixir / Erlang achieve these goal, and it all starts with the idea of processes. Below are my high-level takeaways of Elixir in Action’s Chapter 5 — Concurrency Primitives:
- Processes are the basic building blocks of Elixir / Erlang’s concurrency and parallelism powers.
- Concurrency means that each processes runs in its own execution environment, without sharing any memory with any other process.
- Parallelism means that multiple processes can execute at the same time.
- The spawn/1 function is used to create new processes. The spawn/1 function takes a zero-arity function as its argument. As a result, we must use the closure mechanism to “pass” data into the new process being spawned:
def async(data) do
spawn fn ->
# Execute Something
# data variable is available here thanks to closure
- Because processes are concurrent — i.e., share no memory — data made available from Process A to Process B is a deep copy of the data. In the example above, the data variable is copied into the execution context of the spawned process.
- Processes communicate with one another via message passing. Messages can be sent to another process via the send/2 function.
- Messages sent to a process will be stored in that process’s mailbox until two things happen. First, the process must check its mailbox using the receive macro. Secondly, the message in the mailbox must match a pattern.
- While multiple processes can execute in parallel, each process is itself sequential. It is either processing a message, or it is checking its mailbox for a message.