venomnert

venomnert

Tasks vs GenServer

Hey Everyone, hope everyone had a great Christmas and successful boxing day :grin:!

Context:

I want to each client to to be able to create send multiple emails. After multiple iterations I have landed on the following designs; furthermore, have settled on Tasks.

Questions:

  • Just out of curiosity, when would the GenServer be more beneficial than Tasks, based on the design?

  • The main benefit GenServer provides is the ability to maintain long running state. Apart from that are there any performance benefit that GenServer offers over Tasks?

First Post!

kokolegorille

kokolegorille

You have a better control with GenServer, while Task is a process meant to do something, then die.

But if it is just to send an email, You could just spawn a process, then You could ask why a Task over a Process?

One day You might decide to have a better control, a better error handling, or just support back pressure… You might send billions for Christmas, and less the rest of the year. In this case I would not choose Task.

Most Liked

LostKobrakai

LostKobrakai

Tasks are most useful for failure separation or asynchronous execution for a piece of code. If your current process does need to do something, which is prone to fail, possibly even because of things outside of your control, but hopefully in the control of your user, then Tasks are a good way to start. Same if you want to quickly execute something concurrently to your current process, where persistance of the job doesn’t matter. All a task does is basically inline executed code, but wrapped in another process. Only the process starting the task should be interested in the result or failure of the started task – with the only exception being the optional task supervisor. Personally I’d even say a task should not be very long running as well, because it’s unlike a job queue not sticking around in case of it being stopped by external factors.

I’m with @benwilson512 here, that for emails a proper job queue is a better fit, because you can be more certain that the email will indeed be sent. Sending emails are something once the initial webrequest is successful your system is in charge to fulfill.

Tasks imo are more useful for code, where your system is not in charge if there are failures. E.g. take processing an user uploaded file. If the processing fails (and your code works correctly) your system cannot do anything to make it work. The user needs to fix the input from their side.

I’d like to make this a bit more concrete. A task is meant to be an alternative to using spawn, but be otp conform (see :proc_lib), so it can be properly supervised/shut down. With that it’s less likely to leak processes with tasks. A task therfore is more than “just a process” given it implements otp’s messages, but it’s like spawn just meant to execute a piece of code and be done.

13
Post #5
axelson

axelson

Scenic Core Team

If you don’t want this behavior you can set ordered to false and then the output will not be buffered at all.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

@kokolegorille are you confusing Task with a job queue? A task is literally just a process. It doesn’t provide better control or back pressure, it provides marginally better error handling because it sets some standard values in the process dictionary, but that’s about it. It isn’t heavier weight than a process.

On the note of a job queue, those are often a good idea with something like email. You get retries, concurrency limits, and so on.

Where Next?

Popular in Discussions Top

AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31707 143
New
Crowdhailer
I’ve been hearing much about the new formatter and it’s something I have been keen to try. I find examples buy far the most illuminating...
248 19760 150
New
joeerl
I’m playing with Elixir - It’s fun. I think @rvirding does give Elixir courses these days. Re: files and database - when I given Erlang ...
New
AstonJ
If so I (and hopefully others!) might have some tips for you :slight_smile: But first, please say which area you’re finding most challen...
New
mmmrrr
Just saw that dhh announced https://hotwire.dev/ Is it just me or is this essentially live view? :smiley: Although I like the “iFrame-e...
New
cvkmohan
The upcoming Phoenix 1.6 release looks very interesting. Became a habit to watch the commits - and - what they are bringing in. phx.gen...
New
crispinb
On reading dhh’s latest The One Person Framework it strikes me that Phoenix with LiveView is already pretty much this. However, never hav...
New

Other popular topics Top

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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
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

We're in Beta

About us Mission Statement