51
Phoenix Framework for the realtime web Pedro Medeiros

Phoenix Framework for the realtime web

Embed Size (px)

Citation preview

Page 1: Phoenix Framework for the realtime web

Phoenix Framework for the realtime webPedro Medeiros

Page 2: Phoenix Framework for the realtime web

Summary

Page 3: Phoenix Framework for the realtime web

Summary

● Erlang model● Brief Elixir● Phoenix motivation● Phoenix Channels

Page 4: Phoenix Framework for the realtime web
Page 5: Phoenix Framework for the realtime web

Erlang

● Created in 1986● Solves a specific Hard problem at the time● Limited Resources● In Brief

○ Fault-Tolerance○ High Disponibility○ Concurrent Oriented

Page 6: Phoenix Framework for the realtime web

Switch

Switch

phones

Page 7: Phoenix Framework for the realtime web

Erlang

● Achievements○ No shared state concurrency focused○ Live code reloading○ Clustering through message passing (Actor

Model)○ High Availability 99.9999999 % (nine Nines)○ Used in GPRS, 3G and LTE networks worldwide

Page 8: Phoenix Framework for the realtime web

Erlang

Page 9: Phoenix Framework for the realtime web
Page 10: Phoenix Framework for the realtime web

Switch

Switch

phones

Page 11: Phoenix Framework for the realtime web

Server

Browser

App

IoT

Endpoints

Page 12: Phoenix Framework for the realtime web

2 Million connections on a single nodehttps://blog.whatsapp.com/196/1-million-is-so-2011?

Page 13: Phoenix Framework for the realtime web
Page 14: Phoenix Framework for the realtime web

Erlang

Fibonacci example:

Page 15: Phoenix Framework for the realtime web

Erlang

World Counter example:

Page 16: Phoenix Framework for the realtime web
Page 17: Phoenix Framework for the realtime web
Page 18: Phoenix Framework for the realtime web

Elixir

● Functional Programming language.● On top of Erlang VM● Immutable structures● Concurrent● Distributed● Fault Tolerant

Page 19: Phoenix Framework for the realtime web

Elixir

● Design Goals○ Extensibility○ Productivity○ Compatibility

Page 20: Phoenix Framework for the realtime web

Elixir

● Extensibility○ Ad hoc Polymorphism (data type)○ Strict and lazy enumeration APIs○ Macros for metaprogramming

Page 21: Phoenix Framework for the realtime web

● Macros

Elixir

Page 22: Phoenix Framework for the realtime web

● Macros

Elixir

Page 23: Phoenix Framework for the realtime web

Elixir

● Productivity○ Out of the box tools to startup a project○ Nice syntax for test and documentation○ Easy to distribute and to create libs

Page 24: Phoenix Framework for the realtime web

● Testing asserts

Elixir

Page 25: Phoenix Framework for the realtime web

Elixir

● Compatibility○ Compiles to ErlangVM○ Elixir runs Erlang code Erlang runs Elixir code○ Community grown

Page 26: Phoenix Framework for the realtime web

● Calling Erlang from elixir

● Calling Elixir from Erlang

Elixir

Page 27: Phoenix Framework for the realtime web

● Exchange Code between those two.

Elixir

Page 28: Phoenix Framework for the realtime web

Disclaimer

Page 29: Phoenix Framework for the realtime web

Elixir is not about a better syntax for the Erlang VM

Page 30: Phoenix Framework for the realtime web

2 Million connections on a single nodehttps://blog.whatsapp.com/196/1-million-is-so-2011?

Page 31: Phoenix Framework for the realtime web
Page 32: Phoenix Framework for the realtime web

Phoenix

● Started with Phoenix Channels○ Inspired by the whatsapp case○ Real time communication○ Beyond browsers (native apps and IoTs)

Page 33: Phoenix Framework for the realtime web

Phoenix

Easy Abstraction in javascript

Page 34: Phoenix Framework for the realtime web

Phoenix

Simple Backend code

Page 35: Phoenix Framework for the realtime web

Server

Server

Browser

Native

IoTs

Phoenix

Page 36: Phoenix Framework for the realtime web

PubSub

Phoenix

client server

Channels

socket.connect()

Distributed ErlangRedisPostgres?XMPP?

- Isolated- Concurrent

Page 37: Phoenix Framework for the realtime web

The road to 2 million connections

Page 38: Phoenix Framework for the realtime web

○ 40 core 128 gb Rack space machine○ Accomplish it with minor changes.

Phoenix

http://www.phoenixframework.org/blog/the-road-to-2-million-websocket-connections

Page 39: Phoenix Framework for the realtime web

PhoenixThe road to 2 million connections

Page 40: Phoenix Framework for the realtime web

Phoenix Benchmarks

"There are three kinds of lies: lies, damned lies, and statistics."- Clarisse Linspector

Page 41: Phoenix Framework for the realtime web

Phoenix Benchmarks

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

Library Throughput (req/s) Latency (m/s)

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

Page 42: Phoenix Framework for the realtime web

Phoenix

client server

/index

/permissions

/settings

- Isolated- Concurent

Page 43: Phoenix Framework for the realtime web
Page 44: Phoenix Framework for the realtime web

Productivity

Page 45: Phoenix Framework for the realtime web

Short term productivity vsLong term productivity

Page 46: Phoenix Framework for the realtime web

Short term productivity

● Great documentation as first class citizen● Guides● Code generators (as a learning tool)

○ mix phx.gen.html○ mix phx.gen.json○ mix phx.gen.channel

Page 47: Phoenix Framework for the realtime web

Short term productivity

● HTML Form Builders● Great live code reloading● Assets build tools with ES6 as default● Pretty error pages● First Class concurrent test tools

Page 48: Phoenix Framework for the realtime web

Long term productivity

● OTP Applications○ Functional Programming○ Introspection○ Monitoring○ Fault Tolerant○ Supervisors○ Visibility of application state

Page 49: Phoenix Framework for the realtime web

Long term productivity

Page 50: Phoenix Framework for the realtime web

Wrapping up

Page 51: Phoenix Framework for the realtime web

Thanks ;)