All posts

Understanding Elixir Process

This and the other “Deck” posts are a repurposing of flashcard study decks to Q&A blog posts. Google was not showing love to this content as a set of flashcards and I didn’t want to delete them entirely, I hope you find it useful.

What are BEAM Processes?

They are lightweight execution contexts that run Elixir code in their own memory space. Processes can communicate with one another through message passing.

How are BEAM Processes created?

There are multiple modules and mechanisms for creating processes but they use spawn/1 function under the hood.

What is a PID in relation to BEAM processes?

It is the process identifier. Anytime a process is spawned it will return a PID you can use to inspect the process and send messages to it.

How can you check if a specific BEAM Process still exists?

There is an is_alive? function in the Process module that accepts a pid and returns a true or false boolean.

How does Elixir's `receive` block relate to processes?

A receive block is waiting for a message to be sent to the process that defines it. When a receive block is encountered further processing is paused until a message is sent to the process and executed by the receive block.

Is there a function to send a message to another BEAM Process?

Yes, the Kernels send/1 function.

What are BEAM Supervisor's used for?

They help manage processes. Another way to think about it is they are linked to the child processes they supervise. Supervisors are frequently used to spawn new processes if one fails, but other strategies exist as well.

What is a supervision tree?

There can be Supervisors for other Supervisors. This creates a network/tree of supervisor processes known as a supervision tree.

Is it possible to get a graphical view of an Elixir applications supervision tree?

Yes. When you start an Elixir application in interactive mode using iex you can call :observer.start in the terminal and a GUI will appear. If you navigate to the applications tab it will show you the supervision tree of your application.

What are process pools?

Process pools are a group of long running processes that are waiting to be used. A good example is the Ecto database management library. It will spawn a pool of ten connections at startup time to be used by your application. It does this instead of spawning a new process for each connection because establishing a database connection can be a heavy task in terms of resource utilization.

What are some examples of processes in a Phoenix application?

  1. Each HTTP request spawns a new process using the Cowboy and Ranch libraries.
  2. Ecto uses a process pool to handle database connections.

For your continued enjoyment: