Phoenix Framework

38
@josevalim / phoenixframework.org

Transcript of Phoenix Framework

Page 1: Phoenix Framework

@josevalim / phoenixframework.org

Page 2: Phoenix Framework

Glossary

• Phoenix (web framework) • Elixir (programming language) • Erlang VM

Page 3: Phoenix Framework
Page 4: Phoenix Framework

http://blog.whatsapp.com/index.php/2012/01/1-million-is-so-2011/

2 million connections on a single node

Page 5: Phoenix Framework

Intel Xeon CPU X5675 @ 3.07GHz 24 CPU - 96GB Using 40% of CPU and Memory

Page 6: Phoenix Framework

Phoenix Channels

Page 7: Phoenix Framework

var socket = new Phoenix.Socket("/ws"); socket.connect();

var channel = socket.channel(“chat:lobby");

channel.on("user_joined", function(message){ // ... });

channel.on("new_message", function(msg){ // ... });

$input.on("enter", function(e){ channel.push("new_message", { content: $input.val(), username: App.username }); });

channel.join();

Page 8: Phoenix Framework

defmodule Chat.UserSocket do use Phoenix.Socket

channel "chat:lobby", Chat.LobbyChannel channel "room:*", Chat.RoomChannel

# def connect(params, socket) # def id(socket) end

Page 9: Phoenix Framework

defmodule Chat.LobbyChannel do use Phoenix.Channel

def join("chat:lobby", message, socket) do broadcast! socket, “user_joined”, %{username: message[“username"]} {:ok, socket} end

def handle_in("new_message", message, socket) do broadcast socket, "new_message", %{content: message["content"], username: messages[“username"]} socket end end

Page 10: Phoenix Framework

Server

Browser

Native Mobile

Embedded Device

“Browser" (IE)

Outside viewServer

Page 11: Phoenix Framework

Inside view

Client Server

Transport socket.connect()

Channels socket.join(channel)

Pubsub

• Distributed Erlang • Redis • PostgreSQL? • XMPP?

• Isolated • Concurrent

Page 12: Phoenix Framework

Subscribers per second

Time (s)

Clients

Page 13: Phoenix Framework

htop

Page 14: Phoenix Framework

phoenixframework.org

Page 15: Phoenix Framework
Page 16: Phoenix Framework

Performance

Page 17: Phoenix Framework

HTTP(S) Performance

Page 18: Phoenix Framework

Library Throughput (req/s) Latency (ms)

Plug (elixir) 198 328 0.63

Phoenix (elixir) 179 685 0.61

Gin (go) 176 156 0.65

Play (scala) 171 236 1.89

Express Cluster (node) 92 064 1.24

Martini (go) 32 077 3.35

Sinatra (ruby) 30 561 3.50

Rails (ruby) 11 903 8.50

$ wrk -t20 -c100 -d30S --timeout 2000 https://github.com/mroth/phoenix-showdown

Page 19: Phoenix Framework

Inside view

Client Server

• Isolated • Concurrent

/users

/users

/

/admin

/search?q=elixir

Page 20: Phoenix Framework

Isolated and Concurrent

• Crashes are isolated • Data is isolated

(GC is per process, no global pauses) • Load balances on IO and CPU

(efficient on multicore)

Page 21: Phoenix Framework

Productivity

Page 22: Phoenix Framework

Productivity• Short-term productivity

• Documentation / Guides • Workflows / Generators

• Long-term productivity • Introspection • Maintainability

Page 23: Phoenix Framework
Page 24: Phoenix Framework
Page 25: Phoenix Framework

Generators as learning tools

$ mix phoenix.gen.html $ mix phoenix.gen.json $ mix phoenix.gen.channel

Page 26: Phoenix Framework

More…• Form builders • Static build tools with ES6 as default • Live reloading • Pretty error pages • First-class concurrent test tools • Packages via hex.pm

Page 27: Phoenix Framework

Long term productivity: Applications

Page 28: Phoenix Framework

Application

Pubsub

Super visor

TCP

Client

Page 29: Phoenix Framework

Applications• Package and run our code • Can be started and stopped as a unit • Provide unified configuration • Hold processes and state in the

supervision tree

Page 30: Phoenix Framework

Observer Demo

Page 31: Phoenix Framework

Applications

• Introspection & Monitoring • Visibility of the application state • Easy to break into "components" • Reasoning when things go wrong

Page 32: Phoenix Framework

Summing up

Page 33: Phoenix Framework

phoenixframework.org

Page 34: Phoenix Framework
Page 35: Phoenix Framework

elixir-lang.org

Page 36: Phoenix Framework
Page 37: Phoenix Framework

consulting and software engineering

Page 38: Phoenix Framework

@josevalim / phoenixframework.org