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
Supervisors, Links and Monitors
Search
Sean Cribbs
September 20, 2016
Technology
0
92
Supervisors, Links and Monitors
Using OTP tools in your Elixir application for great justice
Sean Cribbs
September 20, 2016
Tweet
Share
More Decks by Sean Cribbs
See All by Sean Cribbs
Adopting Stream Processing for Instrumentation
seancribbs
1
360
Getting the Word Out: Membership, Dissemination, and Population Protocols
seancribbs
0
330
Reliable High-Performance HTTP Infrastructure with nginx and Lua
seancribbs
0
570
The Refreshingly Rewarding Realm of Research Papers
seancribbs
0
360
Roll-your-own API Management Platform with NGINX and Lua
seancribbs
10
1.1k
Techniques for Metaprogramming in Erlang
seancribbs
2
1.4k
The Final Causal Frontier
seancribbs
6
910
Erlang - Building Blocks for Global Distributed Systems
seancribbs
4
790
A Brief History of Time in Riak
seancribbs
4
1.6k
Other Decks in Technology
See All in Technology
Nix入門パラダイム編
asa1984
1
160
ABEMA のコンテンツ制作を最適化!生成 AI x クラウド映像編集システム / abema-ai-editor
cyberagentdevelopers
PRO
1
130
Overview of file type identifiers
ange
0
210
Mobbing Practices
kawaguti
PRO
3
340
GitHub Universe: Evaluating RAG apps in GitHub Actions
pamelafox
0
130
新卒1年目が挑む!生成AI × マルチエージェントで実現する次世代オンボーディング / operation-ai-onboarding
cyberagentdevelopers
PRO
0
100
最速最小からはじめるデータプロダクト / Data Product MVP
amaotone
1
370
omakaseしないための.rubocop.yml のつくりかた / How to Build Your .rubocop.yml to Avoid Omakase #kaigionrails
linkers_tech
3
210
都市伝説バスターズ「WebアプリのボトルネックはDBだから言語の性能は関係ない」 - Kaigi on Rails 2024
osyoyu
13
4.8k
生成AI×マルチテナントSaaSな新規事業を立ち上げる上でテックリードとして気を使った点の紹介
lunastera
0
530
AIを使って小説を書こう!【2024/10/25講演資料】
kamomeashizawa
0
160
Kubernetes Summit 2024 Keynote:104 在 GitOps 大規模實踐中的甜蜜與苦澀
yaosiang
0
270
Featured
See All Featured
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
41
9.2k
Visualization
eitanlees
143
15k
Code Reviewing Like a Champion
maltzj
519
39k
The Art of Programming - Codeland 2020
erikaheidi
51
13k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
The Cult of Friendly URLs
andyhume
78
6k
What's new in Ruby 2.0
geeforr
342
31k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
37
1.8k
Fontdeck: Realign not Redesign
paulrobertlloyd
81
5.2k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
Speed Design
sergeychernyshev
24
560
Optimising Largest Contentful Paint
csswizardry
33
2.9k
Transcript
SUPERVISORS, LINKS & MONITORS Sean Cribbs
FLASHBACK: 2008 a more innocent time
PLAYSKOOL: MY FIRST ERLANG twitter client mochiweb handler mnesia
PLAYSKOOL: MY FIRST ERLANG ➤ Twitter API changes? CRASH twitter
client mochiweb handler mnesia
PLAYSKOOL: MY FIRST ERLANG ➤ Twitter API changes? CRASH ➤
Buggy request handler? CRASH twitter client mochiweb handler mnesia
PLAYSKOOL: MY FIRST ERLANG ➤ Twitter API changes? CRASH ➤
Buggy request handler? CRASH ➤ Mnesia acting weird? CRASH twitter client mochiweb handler mnesia
PLAYSKOOL: MY FIRST ERLANG ➤ Twitter API changes? CRASH ➤
Buggy request handler? CRASH ➤ Mnesia acting weird? CRASH twitter client mochiweb handler mnesia RESULT: Unpredictable behavior
SUPERVISORS are not for restarting processes
SUPERVISORS are for structuring your application
HINDSIGHT IS 20/20 twitter client mochiweb handler mnesia my_app (supervisor)
HINDSIGHT IS 20/20 ➤ mnesia as application dependency twitter client
mochiweb handler mnesia my_app (supervisor)
HINDSIGHT IS 20/20 ➤ mnesia as application dependency ➤ Client
and mochiweb handler as children twitter client mochiweb handler mnesia my_app (supervisor)
HINDSIGHT IS 20/20 ➤ mnesia as application dependency ➤ Client
and mochiweb handler as children ➤ Supervisor initializes mnesia tables twitter client mochiweb handler mnesia my_app (supervisor)
HINDSIGHT IS 20/20 ➤ mnesia as application dependency ➤ Client
and mochiweb handler as children ➤ Supervisor initializes mnesia tables twitter client mochiweb handler mnesia RESULT: Reliability my_app (supervisor)
:observer.start()
SUPERVISOR STRATEGIES ➤ :one_for_one ➤ :one_for_all ➤ :rest_for_one ➤ :simple_one_for_one
SUPERVISOR STRATEGIES ➤ :one_for_one ➤ :one_for_all ➤ :rest_for_one ➤ :simple_one_for_one
㱺 Task.Supervisor
SUPERVISORS use links and trap exits
LINKS intertwine fates of two processes
A B
A B Process.link(b)
file_server_2 Port (driver)
file_server_2 Port (driver) shutdown!
file_server_2 Port (driver) shutdown! EXIT close()
None
TRAP EXITS if you need to handle exit signals
TRAP EXITS if you need to handle exit signals Process.flag(:trap_exit,
true)
TRAP EXITS if you need to handle exit signals Process.flag(:trap_exit,
true) {:EXIT, pid, reason}
MONITORS notify of process exit without linking
A B
A B Process.monitor(b)
A B Process.monitor(b) {:DOWN, ref, :process, pid, reason}
A B GENSERVER.CALL/3
A B GENSERVER.CALL/3 ➤ Monitor the target {:ok, ref} =
Process.monitor(b)
A B GENSERVER.CALL/3 ➤ Monitor the target ➤ Send the
message tagged with the monitor reference {:ok, ref} = Process.monitor(b) {:'$gen_call', {a, ref}, :hi}
A B GENSERVER.CALL/3 ➤ Monitor the target ➤ Send the
message tagged with the monitor reference ➤ Receive {:ok, ref} = Process.monitor(b) {:'$gen_call', {a, ref}, :hi}
A B GENSERVER.CALL/3 ➤ Monitor the target ➤ Send the
message tagged with the monitor reference ➤ Receive ➤ {^ref, reply} ⇒ {:ok, reply} {:ok, ref} = Process.monitor(b) {:'$gen_call', {a, ref}, :hi}
A B GENSERVER.CALL/3 ➤ Monitor the target ➤ Send the
message tagged with the monitor reference ➤ Receive ➤ {^ref, reply} ⇒ {:ok, reply} ➤ :DOWN ⇒ exit(reason) {:ok, ref} = Process.monitor(b) {:'$gen_call', {a, ref}, :hi}
A B GENSERVER.CALL/3 ➤ Monitor the target ➤ Send the
message tagged with the monitor reference ➤ Receive ➤ {^ref, reply} ⇒ {:ok, reply} ➤ :DOWN ⇒ exit(reason) ➤ timeout ⇒ exit(:timeout) {:ok, ref} = Process.monitor(b) {:'$gen_call', {a, ref}, :hi}
registry conn sup app sup CUSTOM PROCESS REGISTRY
registry conn sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:
Lookup or start a connection to another node
registry conn sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:
Lookup or start a connection to another node ➤ Client (GenServer.call) ⇒ Registry client
registry conn sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:
Lookup or start a connection to another node ➤ Client (GenServer.call) ⇒ Registry ➤ Registry ⇒ Supervisor.start_child client
registry conn sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:
Lookup or start a connection to another node ➤ Client (GenServer.call) ⇒ Registry ➤ Registry ⇒ Supervisor.start_child conn client
registry conn sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:
Lookup or start a connection to another node ➤ Client (GenServer.call) ⇒ Registry ➤ Registry ⇒ Supervisor.start_child ➤ Registry ⇒ monitor connection conn client
registry conn sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:
Lookup or start a connection to another node ➤ Client (GenServer.call) ⇒ Registry ➤ Registry ⇒ Supervisor.start_child ➤ Registry ⇒ monitor connection ➤ Client ⇐ connection pid (maybe monitor?) conn client
registry conn sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:
Lookup or start a connection to another node ➤ Client (GenServer.call) ⇒ Registry ➤ Registry ⇒ Supervisor.start_child ➤ Registry ⇒ monitor connection ➤ Client ⇐ connection pid (maybe monitor?) ➤ DOWN ⇒ Registry cleans up client
TIPS AND TRICKS ➤ Don’t put too much on your
Supervisor ➤ Child order matters ➤ Use links and monitors ➤ Read the Elixir and Erlang source
REFERENCES ➤ Erlang Reference Manual: Processes http://erlang.org/doc/reference_manual/processes.html ➤ Erlang Reference
Manual: Errors http://erlang.org/doc/reference_manual/errors.html ➤ OTP Design Principles: Overview http://erlang.org/doc/design_principles/des_princ.html ➤ OTP Design Principles: Supervisors http://erlang.org/doc/design_principles/sup_princ.html ➤ OTP Design Principles: Applications http://erlang.org/doc/design_principles/applications.html