Slide 1

Slide 1 text

teamgaslight.com Elixir Why processes trump objects @kevinrockwood Kevin Rockwood

Slide 2

Slide 2 text

Elixir

Slide 3

Slide 3 text

I work for Gaslight in Cincinnati Hi, I'm Kevin

Slide 4

Slide 4 text

What is Object Oriented?

Slide 5

Slide 5 text

• Classes • Methods • Instances • Inheritance

Slide 6

Slide 6 text

• Java • Python • Ruby

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

This is a lot like the internet

Slide 12

Slide 12 text

ALAN KAY “I thought of objects being like biological cells and/or individual computers on a network, only able to communicate with messages”

Slide 13

Slide 13 text

1986

Slide 14

Slide 14 text

• Distributed • Fault-tolerant • Real-time • Highly available What Ericsson Needs:

Slide 15

Slide 15 text

Erlang / OTP Framework

Slide 16

Slide 16 text

• Dynamic • Compiled • Functional • Immutable • Everything is a Processtm Erlang Language

Slide 17

Slide 17 text

• Implements the actor model with Erlang processes • Libraries for managing the process lifecycle • Hot code swapping • Data persistance OTP Framework

Slide 18

Slide 18 text

Switch Phone Phone Phone Phone Switch Phone Phone Phone Switch

Slide 19

Slide 19 text

Server Browser iOS Android PI Server Arduino Browser iOS Server

Slide 20

Slide 20 text

2011

Slide 21

Slide 21 text

Elixir

Slide 22

Slide 22 text

Processes Communicate Via Messages

Slide 23

Slide 23 text

Processes Have State > {:ok, pid} = MyProcess.start() > MyProcess.push(pid, "Hello World") [] [] ["Hello World"]

Slide 24

Slide 24 text

Processes Have State defmodule MyProcess do def init() do {:ok, []} end def handle_call({:push, message}, state) do {:ok, [message | state]} end end > {:ok, pid} = MyProcess.start_link // state = [] > MyProcess.push(pid, "Hello World") // state = ["Hello World"]

Slide 25

Slide 25 text

Let's Build a Web Crawler

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Web Server Socket Manager Crawlers Crawler Registry Connection Pool What Do We Need? App 1 App 2 Umbrella

Slide 28

Slide 28 text

Browser crawl(example.com) push("/page_1") ... Phoenix Channel Crawler Connection Pool

Slide 29

Slide 29 text

Supervisor Registry Connection Pool Crawler github.com Crawler oscon.com Crawler example.com

Slide 30

Slide 30 text

// Phoenix Channel def handle_in("crawl", %{"url" => url}, socket) do Crawler.start(url) receive do {:page, new_page} -> push(socket, "new_page", new_page) {:link, new_link} -> push(socket, "new_link", new_link) end end

Slide 31

Slide 31 text

%{ root: "example.com", pages: %{ "example.com" => %{id: 1} "example.com/page_1" => %{id: 2} "example.com/page_3" => %{id: 3} }, links: [ %{target: 1, source: 2}, %{target: 2, source: 1}, %{target: 2, source: 3}, %{target: 3, source: 1}, ] } Crawler Connection Pool

Slide 32

Slide 32 text

Demo!

Slide 33

Slide 33 text

What is Object Oriented?

Slide 34

Slide 34 text

• Objects should be completely isolated • Objects should only communicate via message passing • Objects should be fault tolerant What is Object Oriented?

Slide 35

Slide 35 text

• Processes should be completely isolated • Processes should only communicate via messaging • Processes should be fault tolerant What is Object Oriented?

Slide 36

Slide 36 text

Processes are what OO always intended

Slide 37

Slide 37 text

teamgaslight.com Elixir Why processes trump objects @kevinrockwood Kevin Rockwood