Whirlwind Tour of Elixir

A presentation at Buffer Snack Chat in June 2019 in by Brooklyn Zelenka

Slide 1

Slide 1

A W H I R LW I N D TO U R O F E L I X I R B U F F E R “S N AC K C H AT ” 📬⚗ PIPES, MAILBOXES, & THE ZEN OF JUST LETTING YOUR APP CRASH ☠🚀

Slide 2

Slide 2

BACKGROUND

Slide 3

Slide 3

BACKGROUND A B I T O F I N F O A B O U T T H E TA L K A N D C O N T E X T

Slide 4

Slide 4

BACKGROUND B R O O K LY N Z E L E N K A

Slide 5

Slide 5

BACKGROUND B R O O K LY N Z E L E N K A

Slide 6

Slide 6

BACKGROUND B R O O K LY N Z E L E N K A •Cofounder of •Web3 tools for a Web 2.0 world •Early access: https://tools.fission.codes •Code: https://github.com/fission-suite

Slide 7

Slide 7

BACKGROUND B R O O K LY N Z E L E N K A •Cofounder of •Web3 tools for a Web 2.0 world •Early access: https://tools.fission.codes •Code: https://github.com/fission-suite •PLT & VM enthusiast

Slide 8

Slide 8

BACKGROUND B R O O K LY N Z E L E N K A •Cofounder of •Web3 tools for a Web 2.0 world •Early access: https://tools.fission.codes •Code: https://github.com/fission-suite •PLT & VM enthusiast •Ethereum Core Developer

Slide 9

Slide 9

BACKGROUND B R O O K LY N Z E L E N K A •Cofounder of •Web3 tools for a Web 2.0 world •Early access: https://tools.fission.codes •Code: https://github.com/fission-suite •PLT & VM enthusiast •Ethereum Core Developer •Previously a FT Elixir developer •Exceptional •Witchcraft Suite •https://github.com/witchcrafters •Keynotes •Elixir.LDN 2017 / Empex 2018 / ElixirConf 2019

Slide 10

Slide 10

BACKGROUND GOALS

Slide 11

Slide 11

BACKGROUND GOALS • Situate Elixir • Main characteristics & concepts • Briefly touch on Phoenix

Slide 12

Slide 12

BACKGROUND WHY ELIXIR?

Slide 13

Slide 13

BACKGROUND WHY ELIXIR? • According to the 2019 Stack Overflow developer survey: • Developers who work with Rust, WebAssembly, and Elixir contribute to open source at the highest rates • Globally, respondents who use Clojure, F#, Elixir, and Rust earn the highest salaries

Slide 14

Slide 14

BACKGROUND FUNCTIONAL PROGRAMMING

Slide 15

Slide 15

BACKGROUND FUNCTIONAL PROGRAMMING Alan Turing Alonzo Church

Slide 16

Slide 16

BACKGROUND FUNCTIONAL PROGRAMMING Alan Turing Alonzo Church Imperative instructions Expressions & analytic truths

Slide 17

Slide 17

BACKGROUND FUNCTIONAL PROGRAMMING Alan Turing Alonzo Church Imperative instructions Expressions & analytic truths Global state — “memory” Manual, localized state — “stateless”

Slide 18

Slide 18

BACKGROUND FUNCTIONAL PROGRAMMING Alan Turing Alonzo Church Imperative instructions Expressions & analytic truths Global state — “memory” Manual, localized state — “stateless” Mechanistic, Turing machines Semantic, mathematical models

Slide 19

Slide 19

ERLANG & ELIXIR

Slide 20

Slide 20

ERLANG & ELIXIR A B R I E F H I S T O R Y O F T H E B E A M & O P E N T E L E C O M P L AT F O R M

Slide 21

Slide 21

BEAM ERLANG

Slide 22

Slide 22

BEAM ERLANG

Slide 23

Slide 23

BEAM ● ERLANG HIGH LEVEL

Slide 24

Slide 24

BEAM ● ERLANG HIGH LEVEL • Name • Agner Krarup Erlang • “Ericsson Language” • High performance 1980s telecom switches • Prolog-ish syntax • Same VM as Elixir (BEAM) • Full introp • Actor model

Slide 25

Slide 25

BEAM ELIXIR

Slide 26

Slide 26

BEAM ELIXIR

Slide 27

Slide 27

BEAM ● ELIXIR HIGH LEVEL

Slide 28

Slide 28

BEAM ● ELIXIR HIGH LEVEL • Released in 2011 • BDFL José Valim & co • Several Rails core members • Runs on Erlang’s BEAM VM • Fully interoperable with Erlang • Concurrent, fault tolerant, &c

Slide 29

Slide 29

BEAM ● ELIXIR P H I LO S O P H Y

Slide 30

Slide 30

BEAM ● ELIXIR P H I LO S O P H Y • Readability • Consistency • Fault tolerance • Light weight processes • Soft realtime • Modernity (ex. UTF-8 support out of the box) • Extension / metaprogarmming

Slide 31

Slide 31

BEAM ● ELIXIR RUBY INFLUENCE 💎

Slide 32

Slide 32

BEAM ● ELIXIR RUBY INFLUENCE 💎 • Philosophy • Superficial aesthetics • Friendly • do blocks • Pragmatic • Dot syntax • Readable (“like English”) • Truthiness • Permissive • Dynamic

Slide 33

Slide 33

BEAM IN THE WILD

Slide 34

Slide 34

BEAM IN THE WILD • WhatsApp (2 million connections on a single node) • “Every phone call since 1986” • Discord • Amazon • Facebook • Motorola • Ericsson (obviously) • Heroku • RabbitMQ • Nerves • Parallela

Slide 35

Slide 35

BEAM M O T I VAT I N G C A S E S

Slide 36

Slide 36

BEAM M O T I VAT I N G C A S E S • Performance free lunch is over • Soft realtime • Networking (ie: internet) • Fault tolerant • High availability • Concurrency • “Nine nines” of uptime • Distributed computing • 99.9999999% • Hot code reloading • ~32ms total downtime per year • Clustering

Slide 37

Slide 37

S A L I E N T S Y N TA X

Slide 38

Slide 38

S A L I E N T S Y N TA X F I R S T T H I N G S T H AT Y O U ’ L L N O T I C E

Slide 39

Slide 39

S A L I E N T S Y N TA X PAT T E R N M AT C H I N G & M U LT I P L E F U N C T I O N H E A D S

Slide 40

Slide 40

S A L I E N T S Y N TA X VA R I A D I C F U N C T I O N S

Slide 41

Slide 41

S A L I E N T S Y N TA X PIPES

Slide 42

Slide 42

S A L I E N T S Y N TA X PIPES • Reorder the flow of functions • Linearize function calls for readability • Goes into the first argument position of the next function • Conceptually there is a “subject”

Slide 43

Slide 43

S A L I E N T S Y N TA X P ROTO C O LS

Slide 44

Slide 44

S A L I E N T S Y N TA X P ROTO C O LS

Slide 45

Slide 45

CONCURRENCY

Slide 46

Slide 46

CONCURRENCY THE BEAM’S RAISON D’ÊTRE

Slide 47

Slide 47

CONCURRENCY LIGHTWEIGHT

Slide 48

Slide 48

CONCURRENCY LIGHTWEIGHT • Lightweight virtual green threads • Spin up millions of processes easily • spawn ~ fork

Slide 49

Slide 49

CONCURRENCY “ WAR & PEACE” WORD FREQUENCY

Slide 50

Slide 50

CONCURRENCY “ WAR & PEACE” WORD FREQUENCY

Slide 51

Slide 51

CONCURRENCY 1 0 0 , 0 0 0 + C O N C U R R E N T TA S K S ( O N A L A P T O P )

Slide 52

Slide 52

CONCURRENCY 1 0 0 , 0 0 0 + C O N C U R R E N T TA S K S ( O N A L A P T O P )

Slide 53

Slide 53

CONCURRENCY 1 0 0 , 0 0 0 + C O N C U R R E N T TA S K S ( O N A L A P T O P )

Slide 54

Slide 54

THE ACTOR MODEL

Slide 55

Slide 55

THE ACTOR MODEL A SANE MODEL OF CONCURRENCY & A CLEANER O.O.

Slide 56

Slide 56

THE ACTOR MODEL STO RY T I M E 📖

Slide 57

Slide 57

THE ACTOR MODEL STO RY T I M E 📖 ‘

Slide 58

Slide 58

THE ACTOR MODEL STO RY T I M E 📖 📈 ‘

Slide 59

Slide 59

THE ACTOR MODEL STO RY T I M E 📖 📈 ’ ) 📊

Slide 60

Slide 60

THE ACTOR MODEL STO RY T I M E 📖 📈 ’ ✉ 🤔 ) 📊

Slide 61

Slide 61

THE ACTOR MODEL STO RY T I M E 📖 📈 ’ ✉ 🤔 ✉ ✉ ) 📨 📊

Slide 62

Slide 62

THE ACTOR MODEL STO RY T I M E 📖 📈 ’ ✉ 🤔 . ✨ ✉ ✉ ) 📨 📊

Slide 63

Slide 63

THE ACTOR MODEL STO RY T I M E 📖 📈 ’ ✉ ✉ ✉ 🤔 📚 ✉ ✉📨 ✉ . ✨ ) 📨 📊

Slide 64

Slide 64

THE ACTOR MODEL STO RY T I M E 📖 📈 ✉ ✉ ✉ 🤔 ’ ✨ ✉ 📚 ✉📨 ✉ . ✨ ✉ ) 📨 📊

Slide 65

Slide 65

THE ACTOR MODEL STO RY T I M E 📖 📈 ✉ ✉ ’ 📨 ✉ ✉ ✉ 🤔 ✨ ✉ 📚 ✉📨 ✉ . ✨ ✉ ) 📨 📊

Slide 66

Slide 66

THE ACTOR MODEL ABILITIES

Slide 67

Slide 67

THE ACTOR MODEL ABILITIES 📈 ’ ✉ ✉ 📨

Slide 68

Slide 68

THE ACTOR MODEL ABILITIES ✉ 🤔 📈 ’ ✉ ✉ 📨 2 👶👶👶 ✉ 🔥

Slide 69

Slide 69

THE ACTOR MODEL MAILBOXES

Slide 70

Slide 70

THE ACTOR MODEL MAILBOXES • Literally what it sounds like • Elixir is async, so message passing needs a message queue buffer • Message queue • Optional timeout

Slide 71

Slide 71

SUPERVISION

Slide 72

Slide 72

SUPERVISION WHEN GOOD CODE GOES BAD 😈

Slide 73

Slide 73

SUPERVISION LET IT CRASH 🤯

Slide 74

Slide 74

SUPERVISION LET IT CRASH 🤯 • Don’t defensively program for extreme edge cases 😊 • Does not mean that you can sidestep error handling completely 😡 • Can ignore unknown unknowns • High load, concurrent systems • 1/1,000,000 bug may happen every few seconds

Slide 75

Slide 75

SUPERVISION D E C L A R AT I V E C O N F I G U R AT I O N

Slide 76

Slide 76

SUPERVISION D E C L A R AT I V E C O N F I G U R AT I O N

Slide 77

Slide 77

SUPERVISION D E C L A R AT I V E C O N F I G U R AT I O N

Slide 78

Slide 78

SUPERVISION R E S TA R T S T R AT E G I E S ♻

Slide 79

Slide 79

SUPERVISION R E S TA R T S T R AT E G I E S ♻

Slide 80

Slide 80

SUPERVISION R E S TA R T S T R AT E G I E S ♻ • :one_for_one • :simple_one_for_one • :one_for_all • :rest_for_one

Slide 81

Slide 81

SUPERVISION :ONE_FOR_ONE . 👷 ; 👷 💀

Slide 82

Slide 82

SUPERVISION :ONE_FOR_ONE . 👷 ; 💀

Slide 83

Slide 83

SUPERVISION :ONE_FOR_ONE . 💀 👷 ;

Slide 84

Slide 84

SUPERVISION :ONE_FOR_ONE . 💀 👷 ; 👷

Slide 85

Slide 85

SUPERVISION :ONE_FOR_ALL . 👷 💀 ; 💀 👷 💀

Slide 86

Slide 86

SUPERVISION :ONE_FOR_ALL . 👷 💀 ; 💀 💀

Slide 87

Slide 87

SUPERVISION :ONE_FOR_ALL . 💀 👷 💀 ; 💀

Slide 88

Slide 88

SUPERVISION :ONE_FOR_ALL . 💀 💀 💀

Slide 89

Slide 89

SUPERVISION :ONE_FOR_ALL . 💀 👷 💀 ; 💀 👷

Slide 90

Slide 90

SUPERVISION :REST_FOR_ONE . ; 1 👷 💀 2 👷 💀 3 ; 💀 4

Slide 91

Slide 91

SUPERVISION :REST_FOR_ONE . 💀 ; 1 👷 💀 2 3 ; 💀 4

Slide 92

Slide 92

SUPERVISION :REST_FOR_ONE . 💀 ; 1 2 💀 💀 3 4

Slide 93

Slide 93

SUPERVISION :REST_FOR_ONE . 💀 ; 1 👷 2 👷 💀 3 ; 💀 4

Slide 94

Slide 94

PHOENIX 🐣🔥

Slide 95

Slide 95

PHOENIX 🐣🔥 A H I G H U P T I M E , B AT T E R I E S - I N C L U D E D W E B F R A M E W O R K

Slide 96

Slide 96

PHOENIX STRENGTHS

Slide 97

Slide 97

PHOENIX STRENGTHS • All of the OTP goodness (concurrent, fault tolerant, &c) • Generally no need for external queuing systems like Sidekiq or RabbitMQ • Convenient, Rails-y APIs • Familiar layout & style • Clean(er) MVC architecture • “A bunch of libraries”, rather than a monolithic system

Slide 98

Slide 98

PHOENIX T H E R A I L S P H O E N I X W AY

Slide 99

Slide 99

PHOENIX T H E R A I L S P H O E N I X W AY • Opinionated! • [M]VC+ • Explicit > automagic • …for the parts that you touch • Safe defaults • Consistency • Focus on • Developer experience & ease of use • Sockets • Performance

Slide 100

Slide 100

PHOENIX EXAMPLE REQUEST

Slide 101

Slide 101

PHOENIX EXAMPLE REQUEST

Slide 102

Slide 102

PHOENIX EXAMPLE REQUEST

Slide 103

Slide 103

PHOENIX EXAMPLE REQUEST

Slide 104

Slide 104

PHOENIX EXAMPLE REQUEST

Slide 105

Slide 105

PHOENIX FA M I L I A R S T R U C T U R E

Slide 106

Slide 106

PHOENIX FA M I L I A R S T R U C T U R E

Slide 107

Slide 107

PHOENIX FA M I L I A R S T R U C T U R E

Slide 108

Slide 108

PHOENIX EXAMPLE CONTROLLER

Slide 109

Slide 109

PHOENIX EXAMPLE CONTROLLER

Slide 110

Slide 110

FURTHER READING

Slide 111

Slide 111

FURTHER READING WHERE TO GO FROM HERE

Slide 112

Slide 112

FURTHER READING G E T T I N G S TA R T E D & I N T E R E S T R E A D I N G

Slide 113

Slide 113

FURTHER READING G E T T I N G S TA R T E D & I N T E R E S T R E A D I N G • https://elixir-lang.org • IEx & Mix • GenServer • Broadway • Phoenix LiveView • https://hex.pm & https://hexdocs.pm • http://blog.plataformatec.com.br/tag/elixir/ • https://github.com/expede/up_run • https://github.com/expede/todo-example • https://github.com/expede/quick_chat • Twitter’s #MyElixirStatus

Slide 114

Slide 114

🎉 THANK YOU, BUFFER! ✨ hello@brooklynzelenka.com g i t h u b . c o m /e x p e d e @expede