NinekoTheCat

NinekoTheCat

Why isn't the actor model used in the wild?

I am learning elixir, and I am looking everywhere but for some reason the… actor model isn’t used at all?

There’s no other processes aside from… endpoints and repos??

here there’s a bit more child processes but like… why isn’t there more??

Most Liked

dimitarvp

dimitarvp

You are making a very classic mistake here – you are trying to find a simple formula.

There is no simple formula though.

The actor model (Erlang processes) is about being able to model runtime entities. Which entities, how many, and when are they spawned depends on your business requirements.

Actors are just one more tool in your belt.

This article might help a little bit: The Erlangelist - To spawn, or not to spawn?

But again, don’t expect a simple formula.

That’s part of the entire answer – and here the partial answer is “yes”.

But there’s also the aspect of “I want to control access to a resource” (you already mentioned locking) and “I want to make sure this entity receives messages only one by one, sequentially” (what GenServers do).

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Actors in Elixir generally map to runtime subcomponents of the system, not business entities. So for example when using phoenix channels or live view every connected page is an actor that manages messages in and data out, but then the business logic processing on that data favors functional data patterns.

dimitarvp

dimitarvp

Well, roughly – yes, that’s one factor.

But there’s more to it. So imagine your app is for example about reserving hours to doctors. And in your controller you have to consult a 3rd party API (for example Microsoft Calendar).

  • If you want to give people immediate confirmation or rejection, you do the 3rd party API call (a network request) inside your controller function without spawning a process, so your customers will have to wait in the browser until that request completes.
  • If you opt for customers to never wait you will spawn this 3rd party network API call in a new process and immediately return something to the customers like “We have requested reservation of the hours of this doctor. You should receive an email within a few minutes telling you if the hours are free.”

As you can see, this is not about the technical capabilities, this is about what the business wants.

dimitarvp

dimitarvp

  1. Processes (the actors) are used plenty in every Phoenix project, it was just made invisible to you and for your convenience. For example: the DB repo is basically a pool of connections but each connection is held up by a single process.
  2. Each request is in a new process. If you have 50 currently active requests, you have 50 extra processes. If you have 2000 currently active requests then you have 2000 extra processes.
  3. Libraries like Oban (for background jobs) use processes pervasively.

Frameworks like Phoenix already make use of processes. From then on it’s on you if you want to use extra processes.

As others have said, don’t use processes to model logic. Use processes to model separate runtime entities (like a task that needs to download stuff from 3rd party API, for example).

No, they are used for other purposes as well. See above.

sodapopcan

sodapopcan

It’s used all the time. Elixir code can’t run outside of a process. Even just booting up a vanilla REPL ($ iex) spawns 60 processes.

I’m no expert on the topic and I would search this forum for discussion about it as there is a bunch.

The TL;DR is model your business domain in pure functions, then run those functions in processes. Many of your examples are correct, like yes, a chatroom would run inside its own process. If you’re using LiveView in Phoenix, every user connection is a process.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement