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.

Where Next?

Popular Backend topics Top

AstonJ
Or which features of current frameworks you use you feel you couldn’t live without?
New
New
New
CommunityNews
The Magic of Python Context Managers. Recipes for using and creating awesome Python context managers, that will make your code more read...
New
First poster: bot
The Complete AWS Lambda Handbook for Beginners (Part 1). In the first part of our Complete AWS Lambda Handbook for Beginners, we explain...
New
First poster: bot
proposal: Go 2: permit types to say they may only be created by containing package · Issue #43123 · golang/go. It would be useful to per...
New
Jsdr3398
I love how elixir works and some of its perks, but I’m still pretty uncomfortable, especially when mix/hex gets involved. Did anyone els...
New
mafinar
I did not add this to a “this weekend I’ll learn” like my few other journals as I am decided on using this in the long term. Last I work...
New
mafinar
I wrote a blog post! On F#. I usually don’t write things but figured i should try it out, also experimenting with F# and C# lately, love ...
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

AstonJ
A thread that every forum needs! Simply post a link to a track on YouTube (or SoundCloud or Vimeo amongst others!) on a separate line an...
New
brentjanderson
Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
New
AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
AstonJ
poll poll Be sure to check out @Dusty’s article posted here: An Introduction to Alternative Keyboard Layouts It’s one of the best write-...
New
AstonJ
In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first: asdf plugin-upd...
New
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New
CommunityNews
A Brief Review of the Minisforum V3 AMD Tablet. Update: I have created an awesome-minisforum-v3 GitHub repository to list information fo...
New
OSZAR »