Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Concurrency Basics for Elixir
Search
Maciej Kaszubowski
August 02, 2018
Programming
160
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Concurrency Basics for Elixir
Slides from internal presentation at
https://appunite.com
Maciej Kaszubowski
August 02, 2018
More Decks by Maciej Kaszubowski
See All by Maciej Kaszubowski
Error-free Elixir
mkaszubowski
0
460
Modular Design in Elixir (ElixirConf EU 2019)
mkaszubowski
2
920
The Big Ball of Nouns
mkaszubowski
0
150
Modular Design in Elixir
mkaszubowski
1
420
Our three years with Elixir
mkaszubowski
0
290
Distributed Elixir
mkaszubowski
0
200
Software Architecture
mkaszubowski
0
170
Let it crash - fault tolerance in Elixir/OTP
mkaszubowski
0
540
CRDTs - The science behind Phoenix Presence
mkaszubowski
2
320
Other Decks in Programming
See All in Programming
ECSアプリログをFireLensでコスト削減しようとしたけど諦めた話 in Fargate×Node.js
akihisaikeda
2
4.2k
Go1.27で導入されるジェネリクスメソッドでできること
mackee
0
140
ADKを使って簡単にAIエージェントを作ってみよう
k1mu21
0
270
メソッドのジェネリクスでGoの夢は広がるか? / Kyoto.go #65
utgwkk
3
840
エンジニアと一緒にテストコードの設計と実装を改善した話
mototakatsu
0
200
技術的負債解消で開発者の未来を開く- AIの力でコード刷新
kmd2kmd
0
110
Signal Forms: Details & Live Coding @enterJS 2026 in Mannheim
manfredsteyer
PRO
0
160
JJUG CCC 2026 Spring: JSpecify で実現する Kotlin フレンドリーな Java API 設計
ternbusty
1
180
スマートグラスで並列バイブコーディング
hyshu
0
160
Dataformのリポジトリを立ち上げるときにまずやること / dataform-day0-2026
snhryt
0
170
Semantic Version 単位で戦略を柔軟に変えて、パッケージアップデートを自動化する
daitasu
1
260
ユニットテストの先へ:テスト技法で要求・仕様を整理するJava開発実践 / Beyond_Unit_Testing_Practical_Java_Development_Techniques_for_Organizing_Requirements_and_Specifications
shimashima35
0
410
Featured
See All Featured
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
230
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
250
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
490
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
330
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
210
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
620
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.1k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.6k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
240
Transcript
Concurrency basics For Elixir-based Systems
None
So, what’s concurrency?
Sequential Execution (3 functions, 1 thread)
Sequential Execution (3 functions, 1 thread) Concurrent Execution (3 functions,
3 threads)
Sequential Execution (3 functions, 1 thread) Concurrent Execution (3 functions,
3 threads) Preemptive scheduling
Where’s the benefit?
Req1 Req2 Req3 Resp Sequential Execution time Waiting time
Req1 Req2 Req3 Resp Req1 Resp Req2 Req3 Sequential Concurrent
Execution time Waiting time
CPU bound Re Re Re Res Re Res Re Re
I/O bound
Concurrent or Parallel What’s the difference?
Concurrent Execution (3 functions, 3 threads)
Concurrent Execution (3 functions, 3 threads) Parallel Execution (3 functions,
3 threads, 2 cores) core 1 core 2
root@kingschat-api-c8f8d6b76-4j65j:/app# nproc 12 root@tahmeel-api-prod-b5979bdc6-q5wz6:/# nproc 1 How many cores?
Concurrent Execution (3 functions, 3 threads) Parallel Execution (3 functions,
3 threads, 2 cores) core 1 core 2 (by default) One erlang scheduler per core
:observer_cli.start()
None
Req1 Req2 Req3 Resp Req1 Resp Req2 Req3 Sequential Concurrent
Execution time Waiting time Req1 Resp Req2 Req3 Parallel
Sequential execution
Phoenix Request Req 1
Phoenix Request Resp
Phoenix Request Req 2
Phoenix Request Resp
Phoenix Request Req 3
Phoenix Request Resp
Concurrent execution
Phoenix Request
Phoenix Request Task 1 Task 2 Task 3
Phoenix Request Task 1 Task 2 Task 3 Req 1
Req 2 Req 3
Phoenix Request Task 1 Task 2 Task 3 Resp Resp
Resp
Phoenix Request Task 1 Task 2 Task 3
R1 APP Server DB Server (3 cores) R2 R1 R2
Time Execution time Waiting time
R1 APP Server DB Server (3 cores) Send resp R2
R3 R1 R2 R3 Time Execution time Waiting time
How much can we gain?
Amdahl’s Law
Amdahl’s Law
Amdahl’s Law in a nutshell The more synchronisation, the less
benefit from multiple cores
R1 APP Server Send resp R2 R3 R1 R2 R3
Time Execution time Waiting time Almost 100% parallel (almost no synchronisation) DB Server (3 cores)
But…
R1 APP Server Send resp R2 R3 R1 R2 R3
Time Execution time Waiting time This is not constant DB Server (3 cores)
R1 APP Server Send resp R2 R3 R1 R2 R3
Time Execution time Waiting time This is not infinite DB Server (3 cores)
R1 APP Server R2 R3 R1 R2 R3 R4 Time
Execution time Waiting time DB Server (3 cores)
R1 APP Server R2 R3 R1 R2 R3 R4 Time
Execution time Waiting time DB Server (3 cores)
R1 APP Server R2 R3 R1 R2 R3 R4 R4
Time Execution time Waiting time DB Server (3 cores)
R1 APP Server R2 R3 R1 R2 R3 R4 R4
Time Execution time Waiting time DB Server (3 cores)
R1 APP Server R2 R3 R1 R2 R3 R4 R4
Time Execution time Waiting time DB Server (3 cores)
R1 APP Server Send resp R2 R3 R1 R2 R3
R4 R4 Time Execution time Waiting time DB Server (3 cores)
R1 APP Server R2 R3 R1 R2 R3 R4 R4
Time Execution time Waiting time R5 R6 R7 R5 R6 R7 DB Server (3 cores)
Phoenix Request Task 1 Task 2 Task 3 Req 1
Req 2 Req 3 Remember this?
This isn’t exactly true
None
Connection pool (Prevents from overworking the DB)
Pool Manager (Blocks until a free worker is available)
None
Pool Manager (Blocks until a free worker is available)
None
It gets worse
Pool Manager Mailbox Has to be synchronised
Pool Manager Message Passing Is just copying data in shared
memory
Pool Manager Remember semaphores?
Logger Metrics Sentry
Network stack
Network stack
Network stack
Network stack Sentry Metrics
OS Threads (Garbage Collection) Data Bus Virtual Machines Memory characteristics
(e.g. processor caches) … Other synchronisation points
That’s hard
That’s REALLY hard
That’s REALLY hard Seriously, people spend their entire careers on
this
So, what to do?
Measure
Measure Measure
Measure Measure Measure
Measure ON PRODUCTION
Measure ON PRODUCTION You WILL get false results on staging/locally
Measure Entire system You WILL get false results for single
functions
Measure ONLY IF YOU HAVE TRAFFIC
“premature optimization is the root of all evil”
If something takes X ms, it will always take X
ms.
Async execution cannot “remove” this time It can only hide
it
BACK PRESSURE
Producent Consumer Consumer
Producent Consumer Consumer
Producent Consumer Consumer
Producent Consumer Consumer
Producent Consumer Consumer
Producent Consumer Consumer
Producent Consumer Consumer
Producent Consumer Consumer
Producent Consumer Consumer
Producent Consumer Consumer
Producent Consumer Consumer
Producent Consumer Consumer
Producent Consumer Consumer
Producent Consumer Consumer Stop
Producent Consumer Consumer
Producent Consumer Consumer
Producent Consumer Consumer
Producent Consumer Consumer
Producent Consumer Consumer OK, give me more
Producent Consumer Consumer
None
Back pressure
Thanks!