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
Concurrent and Resilient Connections to Outside...
Search
Andrea Leopardi
May 11, 2016
Programming
1
320
Concurrent and Resilient Connections to Outside the BEAM
Andrea Leopardi
May 11, 2016
Tweet
Share
More Decks by Andrea Leopardi
See All by Andrea Leopardi
gen_statem - OTP's Unsung Hero
whatyouhide
2
250
The World is a Network (and We Are Just Nodes)
whatyouhide
1
220
BEAM: The Perfect Fit for Networks
whatyouhide
1
200
Update from the Elixir team - 2022
whatyouhide
0
400
Testing Asynchronous OTP
whatyouhide
1
520
Elixir Sightseeing Tour
whatyouhide
0
420
Mint - Disrupting HTTP clients
whatyouhide
0
250
BEAM Architecture Handbook
whatyouhide
7
2.8k
The Evolution of a Language
whatyouhide
0
160
Other Decks in Programming
See All in Programming
カクヨムAndroidアプリのリブート
numeroanddev
0
430
関数型まつり2025登壇資料「関数プログラミングと再帰」
taisontsukada
2
840
LINEヤフー データグループ紹介
lycorp_recruit_jp
0
760
エラーって何種類あるの?
kajitack
5
260
Claude Codeの使い方
ttnyt8701
1
130
TypeScript LSP の今までとこれから
quramy
1
510
赤裸々に公開。 TSKaigiのオフシーズン
takezoux2
0
140
つよそうにふるまい、つよい成果を出すのなら、つよいのかもしれない
irof
1
290
Gleamという選択肢
comamoca
6
740
第9回 情シス転職ミートアップ 株式会社IVRy(アイブリー)の紹介
ivry_presentationmaterials
1
190
Team topologies and the microservice architecture: a synergistic relationship
cer
PRO
0
910
GoのWebAssembly活用パターン紹介
syumai
3
10k
Featured
See All Featured
Measuring & Analyzing Core Web Vitals
bluesmoon
7
480
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
137
34k
Statistics for Hackers
jakevdp
799
220k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
700
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
281
13k
Fireside Chat
paigeccino
37
3.5k
Java REST API Framework Comparison - PWX 2021
mraible
31
8.6k
Designing for Performance
lara
609
69k
The Cost Of JavaScript in 2023
addyosmani
51
8.4k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
2.8k
Code Reviewing Like a Champion
maltzj
524
40k
Transcript
outside CONCURRENT RESILIENT and CONNECTIONS to the BEAM
ERLANG VM mnesia message passing ets
ERLANG VM key-value store relational db message queue
ERLANG VM key-value store relational db message queue
the outside world is SCARY
ANDREA LEOPARDI @WHATYOUHIDE
Gothenburg, Sweden
FOOTBALL ADDICTS
GENSERVER TCP
SILLY the way
defmodule Redis do def command(cmd) do end end
defmodule Redis do def command(cmd) do {:ok, sock} = :gen_tcp.connect(...)
:gen_tcp.send(sock, encode(cmd)) {:ok, data} = :gen_tcp.recv(sock, 0) :gen_tcp.close(sock) decode(data) end end
defmodule Redis do def command(cmd) do {:ok, sock} = :gen_tcp.connect(...)
:gen_tcp.send(sock, encode(cmd)) {:ok, data} = :gen_tcp.recv(sock, 0) :gen_tcp.close(sock) decode(data) end end
defmodule Redis do def command(cmd) do {:ok, sock} = :gen_tcp.connect(...)
:gen_tcp.send(sock, encode(cmd)) {:ok, data} = :gen_tcp.recv(sock, 0) :gen_tcp.close(sock) decode(data) end end
defmodule Redis do def command(cmd) do {:ok, sock} = :gen_tcp.connect(...)
:gen_tcp.send(sock, encode(cmd)) {:ok, data} = :gen_tcp.recv(sock, 0) :gen_tcp.close(sock) decode(data) end end
defmodule Redis do def command(cmd) do {:ok, sock} = :gen_tcp.connect(...)
:gen_tcp.send(sock, encode(cmd)) {:ok, data} = :gen_tcp.recv(sock, 0) :gen_tcp.close(sock) decode(data) end end
defmodule Redis do def command(cmd) do {:ok, sock} = :gen_tcp.connect(...)
:gen_tcp.send(sock, encode(cmd)) {:ok, data} = :gen_tcp.recv(sock, 0) :gen_tcp.close(sock) decode(data) end end
opening NEW connections is EXPENSIVE
GENSERVER keeping the socket in a
TWO WAYS blocking non-blocking
TWO WAYS blocking
CLIENT GENSERVER socket socket send() recv()
defmodule Redis do def command(conn, cmd) do sock = checkout(conn)
# send and recv checkin(conn, sock) end end
defmodule Redis do def command(conn, cmd) do sock = checkout(conn)
# send and recv checkin(conn, sock) end end
{:error, :checked_out} :queue.in() VS
POOLING :|
TWO WAYS non-blocking
active: true {:noreply, _} + GenServer.reply/2 +
client Redis client client GenServer
tcp is FULL DUPLEX
client Redis client client Sender Receiver
TWO WAYS blocking non-blocking copies less data + needs pooling
- doesn't use full duplex - enc/dec on client + - copies more data + doesn't need pooling + uses full duplex - enc/dec in server
RESIL IENCY
Redis GenServer
Redis GenServer
{:tcp_closed, reason} backoff + reconnect
hex.pm/packages/connection gen_
Connection GenServer
init/1 connect/2 disconnect/2 handle_call/3 handle_cast/2 handle_info/2 terminate/2 code_change/3 {:disconnect, reason,
state} {:backoff, timeout, state}
sync/async CONNECT
SYNC start_link {:ok, pid} init() connect
ASYNC start_link {:ok, pid} init() connect connect()
You now allow initializations with fewer guarantees: they went from
"the connection is available" to "the connection manager is available". Fred Hebert
CONNECTION TALKING CONCURRENCY RESILIENCY
@whatyouhide