jaeyson

jaeyson

When to use async and sync code in Elixir?

Sorry for the very vague noob question, I really want to ask this:

When do we use async or sync code in the context of Elixir? AFAIK genserver call is synchronous which is blocking code. Do you have a real example or when do you decide which one to use? I’ve read these article When should I use asynchronous code in JavaScript? – Nico Zerpa, Your JavaScript Friend, but I haven’t found a good example when it is better to use sync over async and vice versa. Any help/enlightenment is greatly appreciated :pray:.

Most Liked

Maartz

Maartz

Hey! That’s a very good question.
I’ve found this post from elixirforum very interesting.

If it doesn’t answer your question, it should give you some clues to decide whether when you should use sync or async.
Hope it will help.

dewetblomerus

dewetblomerus

Great question @jaeyson

Sync Example

Imagine you want to read data from a source data store and write it to a destination data store. The requirements are not real-time. As long as the data arrives within 24 hours, everyone is happy.

Imagine the source data store will happily let you read 100,000 records per second.

Imagine the source data is bursty. Once per hour 1000,000 records show up in a few seconds and then nothing for the rest of the hour. And imagine the destination data store slows down when you make concurrent writes and it can’t handle batches larger than 1000 without spending much more money and re-architecture.

You can have a single Genserver responsible for writing to the destination.

Your code that consumes from the source data does not need to know anything about rate limiting or slowing down because it will receive back-pressure from the Genserver every time it tries to use call, which will block the process until it is done writing the batch to the destination.

Async example

Imagine you need to make 5 API requests and present a combination of all the data to the user. You could use Task.async to make all 5 requests, and after that, Task.await all of them.

If the Sync example is too slow

If you started with the sync example and you realize that you need to process faster, Elixir has a very deep toolbox for speeding things up by using an unbounded or configurable number of processes.

OvermindDL1

OvermindDL1

Something more simple:

Call a sync function when you want to wait on the result before doing more.

Call an async function when you want to do the call, do other stuff, then handle the result later, or if you don’t care about the result at all.

Popular Backend topics Top

AstonJ
Partly interested in this so we can set up tags, but also because I’m out of touch with which frameworks are hot right now and I’m curiou...
New
New
New
bot
So you want to live-reload Rust - fasterthanli.me. Good morning! It is still 2020, and the world is literally on fire , so I guess we c...
New
New
New
New
First poster: bot
About Self Self is a prototype-based dynamic object-oriented programming language, environment, and virtual machine centered around the p...
New
AstonJ
And the blog: Rails has been unapologetically full stack since the beginning. We’ve continuously sought to include ever-more default an...
New
luigiluigiluigi
Hey everyone! :wave: We’re looking for a Back-End Developer Volunteer to help us build something impactful! If you want to gain real-wor...
New

Other popular topics Top

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
New
wolf4earth
@AstonJ prompted me to open this topic after I mentioned in the lockdown thread how I started to do a lot more for my fitness. https://f...
New
AstonJ
Or looking forward to? :nerd_face:
New
dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
New
AstonJ
If you are experiencing Rails console using 100% CPU on your dev machine, then updating your development and test gems might fix the issu...
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
PragmaticBookshelf
A Hero’s Journey with Chris Pine @chrispine Chris Pine, author of Learn to Program, Third Edition, discusses his journey to beco...
New
foxtrottwist
A few weeks ago I started using Warp a terminal written in rust. Though in it’s current state of development there are a few caveats (tab...
New
AstonJ
If you want a quick and easy way to block any website on your Mac using Little Snitch simply… File > New Rule: And select Deny, O...
New
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
New