I will introduce this through an example web interaction, a web request that runs some application code, which talks to a database. Each step of this interaction is a separate process.
The Phoenix web framework sets up an HTTP server using the libraries Cowboy and Ranch. When a request is sent to your application over HTTP a Cowboy process is spawned to handle it, once the request is received and validated another Elixir process is spawned by and therefore supervised by the Cowboy process. This newly spawned process is for the application code to handle any business logic required before the HTTP server responds. Once the application process has finished executing a message is sent back to the Cowboy/HTTP process that generated it and the connection is closed.
Now let’s say the application code called an Ecto.Repo database for a query, this case is a slightly different.
When your Phoenix application was started, assuming you are using Ecto for database interactions, a pool of (usually 10) long running processes are started immediately and waiting to be used. These are started immediately instead of being spawned each time the Repo is called because connecting to the database is a “heavy” task, meaning it takes more time and computer resources to execute than other processes that are not pooled. The Erlang and Elixir process pooling technology is Poolboy.
When your application code makes a query using Repo, it uses one of the established pooled process connections to do so. Upon query completion a message is sent to the application process with it’s response or error, then the application process continues any additional processing before sending the final response to the HTTP process.
There are many more processes running in any Phoenix application. My hope is after reading this it will give you confidence to explore more of the ecosystem.
To get a visualization of Elixir processes in your Phoenix application run the following command:
iex -S mix phx.server
This starts your server in interactive mode. From there you start Observer, Elixirs processes introspection tool through:
Once opened navigate to the applications tab to get a diagram of all running processes, you can try restarting processes to get more hands on experience with their behavior.