DEV Community

Cover image for Handling state between multiple processes with elixir

Handling state between multiple processes with elixir

Cherry Ramatis on September 26, 2023

Elixir works really well for concurrent code because of it's functional nature and ability to run in multiple processes, but how we handle state wh...
Collapse
 
tandrieu profile image
Thibaut Andrieu

I saw a talk about Elixir at a conferences. The guy implemented Conway's Game of Life on 1000x1000 grid with one process per cells. So 1 million processes running in parallel. Quite impressive indeed !

Collapse
 
brunonovais profile image
Bruno Rezende Novais • Edited

It's accurated to say that Elixir have lightweight threads then? Idk how exactly the elixir compiler deals with physical threads (1:1 or 1:n, for virtual threads example).
But seeing this point that one function means one process in the concurrency scenario makes me think about lightweight and heavy threads

Collapse
 
cherryramatis profile image
Cherry Ramatis

I think we can say that elixir have lightweight threads indeed, the BEAM Virtual machine manage these multiple processes like an linux OS basically, if it's throwing any errors it kills the process and immediately restarts (following a supervisor tree, I'll write about on the next article maybe :D )

Collapse
 
zoedsoupe profile image
Zoey de Souza Pessanha

Actually the Erlang BEAM virtual machine starts a Schedulers (an especial type of process that supervises and manage tons of others processes, including processes supervisors) for each core in your CPU. So in my case I have 8 cores, so when I start a BEAM (erlang/elixir/gleam) application it will start 8 Schedulers. Its good to notice that all BEAM schedulers are preemptive, that means that each Scheduler can pause/continue each process execution on the “Run Queue”.

So i don;t think the term “lightweight threads” is a good fit for the BEAM… The Erlang virtual machine acts exactly like an operational system, where there are processes, applications (like ExUnit or hex for example), and so on. These Schedulers would then pick some processes from the “Run Queue” and let them been executed, paused or continued.

Each Scheduler represents a real thread!

An example image can be found here: kagi.com/images?q=eralng+beam+sche...

Thread Thread
 
zoedsoupe profile image
Zoey de Souza Pessanha

I think that would be more appropriate to say that the BEAM uses real threads, and those Schedulers contexts and processes management can be seen as “lightweight threads”, but they are semantically different, althouth share some similarities and can be easier to understand comparing both (:

Collapse
 
zoedsoupe profile image
Zoey de Souza Pessanha

Awesome article 🍒! I really liked the approach you followed writing about processes and message passing!

Collapse
 
cherryramatis profile image
Cherry Ramatis

It means a lot coming from you! Thanks

Collapse
 
canhassi profile image
Canhassi

Nice article!!

Collapse
 
cherryramatis profile image
Cherry Ramatis

thanks a lot <3

Collapse
 
elixir_utfpr profile image
Elixir UTFPR (por Adolfo Neto)

Great post! I created a livebook for the code in this post gist.github.com/adolfont/a928aff18...

Collapse
 
cherryramatis profile image
Cherry Ramatis

Wow!!! Thanks a lot this is so useful

Collapse
 
g33knoob profile image
G33kNoob

I just learned elixir by instal blockcscout great article

Collapse
 
cherryramatis profile image
Cherry Ramatis

Happy that was useful for you! soon I'll be making more about this whole processes series

Collapse
 
ilonavarro profile image
Ilo Navarro

Nice article, thanks a lot!

Collapse
 
cherryramatis profile image
Cherry Ramatis

it's a pleasure to bring cool content <3

Collapse
 
artenlf profile image
Luís Felipe Arten

Bravo, Cherry 🍒!!! 👏🏻👏🏻👏🏻👏🏻
Always posting great articles, full of details and technical content! Thank you for sharing your knowledge with the community!

Collapse
 
cherryramatis profile image
Cherry Ramatis

It's always a pleasure to share cool stuff !

Collapse
 
phenriquesousa profile image
Pedro Henrique

The queen

Collapse
 
cherryramatis profile image
Cherry Ramatis

omg thanks cousin! <3 <3

Collapse
 
yayaflc profile image
Yasmin Felício

Elixir 😱 Nice article, prima! 💜

Collapse
 
cherryramatis profile image
Cherry Ramatis

thanks sweeheart <3 <3

Collapse
 
renanvidal profile image
Renan Vidal Rodrigues

Perfect content as always, congratulations Cherry!!

Collapse
 
cherryramatis profile image
Cherry Ramatis

Thanks a lot <3 hope it's useful for you

Collapse
 
fransborges profile image
Fran Borges

I've saved it, so I can read it slowly and learn as much as I can from the knowledge I know there is in this article

Collapse
 
cherryramatis profile image
Cherry Ramatis

If you need anything just reach out <3 <3

Collapse
 
vegadvalentin profile image
Valentin Vegad

Thank you