Slide 1

Slide 1 text

F R O M R A I L S T O P H O E N I X B R I A N C A R D A R E L L A

Slide 2

Slide 2 text

W H Y P H O E N I X ?

Slide 3

Slide 3 text

W H Y P H O E N I X ? Performance!

Slide 4

Slide 4 text

W H Y P H O E N I X ?

Slide 5

Slide 5 text

W H Y P H O E N I X ?

Slide 6

Slide 6 text

W H Y P H O E N I X ?

Slide 7

Slide 7 text

W H Y P H O E N I X ? Performance! This is the gateway drug for Phoenix

Slide 8

Slide 8 text

W H Y P H O E N I X ? Is Performance Enough?

Slide 9

Slide 9 text

W H Y P H O E N I X ? I see a lot of “Hello World” benchmarks that [respond] in nanoseconds.

Slide 10

Slide 10 text

W H Y P H O E N I X ? Who gives a flying fuck?

Slide 11

Slide 11 text

W H Y P H O E N I X ? Have you ever met a customer who said “Your page rendered in 100ms, that is not good enough. I need nanoseconds!”

Slide 12

Slide 12 text

W H Y P H O E N I X ? No one ever asks for that. - DHH https://www.youtube.com/watch?v=ktZLpjCanvg&t=9m49s

Slide 13

Slide 13 text

W H Y P H O E N I X ? If the goal is 100ms how much effort is required to reach that goal?

Slide 14

Slide 14 text

W H Y P H O E N I X ?

Slide 15

Slide 15 text

W H Y P H O E N I X ? Eight year old Rails application (~50 models, ~40 controllers) Rewrite in Elixir/Phoenix began in 2014 Before: 150 AWS Instances Log-jammed responses Multiple engineers per app Multiple complex caching strategies

Slide 16

Slide 16 text

W H Y P H O E N I X ? Eight year old Rails application (~50 models, ~40 controllers) Rewrite in Elixir/Phoenix began in 2014 Results: 1/15th of the servers previously required 10ms - 30ms avg response times Largest average spike 400ms Largest outlier spike 800ms About one engineer per app No caching

Slide 17

Slide 17 text

W H Y P H O E N I X ? No caching?

Slide 18

Slide 18 text

W H Y P H O E N I X ? What Rails developers are told 1. Call to the database as little as possible 2. Render from cache as much as possible

Slide 19

Slide 19 text

W H Y P H O E N I X ?

Slide 20

Slide 20 text

W H Y P H O E N I X ?

Slide 21

Slide 21 text

W H Y P H O E N I X ?

Slide 22

Slide 22 text

W H Y P H O E N I X ? How?

Slide 23

Slide 23 text

W H Y P H O E N I X ? 1. Elixir is must at faster garbage collection

Slide 24

Slide 24 text

W H Y P H O E N I X ? 2. Elixir consumes all CPU cores

Slide 25

Slide 25 text

W H Y P H O E N I X ? 3. Elixir is a compiled language

Slide 26

Slide 26 text

W H Y P H O E N I X ? 4. Phoenix makes smart use of the BEAM

Slide 27

Slide 27 text

W H Y P H O E N I X ? http://www.evanmiller.org/elixir-ram-and-the-template-of-doom.html

Slide 28

Slide 28 text

W H Y P H O E N I X ? 5. The database is not slow

Slide 29

Slide 29 text

W H Y P H O E N I X ?

Slide 30

Slide 30 text

W H Y P H O E N I X ? http://sorentwo.com/2016/02/02/caching-what-is-it-good-for.html

Slide 31

Slide 31 text

W H Y P H O E N I X ? Ruby/Rails • Ruby 2.3 / Rails 4.2.5.1 with ActiveRecord • Fronted by a Redis cache that combines Perforated and Readthis • Content serialized with ActiveModelSerializers, unused with a warm cache • Tuned to fetch as little data as possible from the database • All associations are preloaded • All content is cached as strings, no marshalling or serialization is performed • All data is generic, not customized to the current user • The request is paginated to 100 primary records, without a limit on side loads • The payload is a hefty 160k, un-gzipped http://sorentwo.com/2016/02/02/caching-what-is-it-good-for.html

Slide 32

Slide 32 text

W H Y P H O E N I X ? Elixir/Phoenix • Elixir 1.2.1 / Phoenix 1.1 • No entity cache • All fields are fetched from the database, SELECT * • All JSON responses are serialized on the fly, directly in views • Includes customized data based on the current user • The request isn’t paginated at all, there are 250 primary records • The payload is a massive 724k, un-gzipped http://sorentwo.com/2016/02/02/caching-what-is-it-good-for.html

Slide 33

Slide 33 text

W H Y P H O E N I X ? “These response times are not characteristic of either system, they are at the extreme upper limit. Even so, serving up 2.5x the records with 4.5x the data, without any caching, the Phoenix API response times are 1.5x-2.5x faster.” — Parker Selbert http://sorentwo.com/2016/02/02/caching-what-is-it-good-for.html Rails Phoenix response time (ms)

Slide 34

Slide 34 text

W H Y P H O E N I X ? Why does this matter?

Slide 35

Slide 35 text

W H Y P H O E N I X ? Eight year old Rails application (~50 models, ~40 controllers) Rewrite in Elixir/Phoenix began in 2014 Results: 1/15th of the servers previously required 10ms - 30ms avg response times Largest average spike 400ms Largest outlier spike 800ms About one engineer per app No caching

Slide 36

Slide 36 text

W H Y P H O E N I X ? About one engineer per app

Slide 37

Slide 37 text

W H Y P H O E N I X ? Can Phoenix’s performance benefits free engineers to work on bigger ideas?

Slide 38

Slide 38 text

W H Y P H O E N I X ?

Slide 39

Slide 39 text

W H Y R A I L S ?

Slide 40

Slide 40 text

W H Y R A I L S ? Build something in half the time for half the cost. Optimize for developer happiness

Slide 41

Slide 41 text

W H Y R A I L S ? Optimize for developer happiness

Slide 42

Slide 42 text

W H Y R A I L S ? The problem is state

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

Are you really happy working with your old code?

Slide 45

Slide 45 text

Are you really happy working with other people’s old code?

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

Math.add(1, 2)

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

Functions are “pure” Variables are immutable Explicit > Implicit

Slide 50

Slide 50 text

Simpler system to hold in your head Documentation is easier to find Code is easier to test

Slide 51

Slide 51 text

Elixir took the best of other languages

Slide 52

Slide 52 text

defmodule Person do def say(words) do IO.puts(words) end end Person.say("Hello world!") Syntax (Ruby)

Slide 53

Slide 53 text

Pipe operator (F#) foo(bar(baz("qux"), opts), a, b, c)

Slide 54

Slide 54 text

Pipe operator result = "qux" result = baz(result) result = bar(result, opts) result = foo(result, a, b, c)

Slide 55

Slide 55 text

Pipe operator "qux" |> baz() |> bar(opts) |> foo(a, b, c)

Slide 56

Slide 56 text

Pattern matching def fast_multiply(0, n) do 0 end def fast_multiply(1, n) do n end def fast_multiply(x, n) do x * n end

Slide 57

Slide 57 text

Pattern matching def fast_multiply(0, n) do 0 end def fast_multiply(1, n) do n end def fast_multiply(x, n) do x * n end fast_multiply(0, 5)

Slide 58

Slide 58 text

Pattern matching def fast_multiply(0, n) do 0 end def fast_multiply(1, n) do n end def fast_multiply(x, n) do x * n end fast_multiply(2, 5)

Slide 59

Slide 59 text

Pattern matching def index(conn, params) do # ... end

Slide 60

Slide 60 text

Pattern matching def index(conn, %{"q" = query} = params) do # ... end http://example.com/foo?q=bar

Slide 61

Slide 61 text

Pattern matching def index(conn, %{"q" = “bar”} = params) do # ... end http://example.com/foo?q=bar def index(conn, %{"q" = query} = params) do # ... end

Slide 62

Slide 62 text

Elixir & Phoenix optimize for long- term developer happiness

Slide 63

Slide 63 text

How do Rails developers transition to Phoenix?

Slide 64

Slide 64 text

rails-app !"" Gemfile !"" README.md !"" app/ # !"" assets/ # !"" channels/ # !"" controllers/ # !"" helpers/ # !"" jobs/ # !"" mailers/ # !"" models/ # %"" views/ !"" config # %"" router.rb !"" db/ !"" log/ !"" public/ %"" test/ Rails App Structure phoenix_app !"" README.md !"" config/ !"" lib/ !"" mix.exs !"" priv/ !"" test/ %"" web/ !"" channels/ !"" controllers/ !"" models/ !"" static/ !"" templates/ !"" views/ !"" router.ex %"" web.ex Phoenix App Structure

Slide 65

Slide 65 text

HuhApp::Application.routes.draw do resources :profiles, only: [:show] get 'profiles/:id/reviews' => 'profiles#reviews' end Rails Router Phoenix Router defmodule HuhApp.Router do use HuhApp.Web, :router resources "/profiles", HuhApp.ProfileController, only: [:show] get "/profiles/:id/reviews", HuhApp.ProfileController, :reviews end

Slide 66

Slide 66 text

Rails Controller class ProfilesController < ApplicationController def show @profile = Profile.find(params[:id]) end end Phoenix Controller defmodule HuhApp.ProfileController do def show(conn, %{"id" => id}) do profile = Repo.get(Profile, id) render conn, "show.html", profile: profile end end

Slide 67

Slide 67 text

Rails Migration class CreateProfiles < ActiveRecord::Migration def change create_table :profiles do |t| t.string :name t.timestamps end end end Phoenix (Ecto) Migration defmodule HuhApp.Repo.Migrations.CreateProfile do use Ecto.Migration def change do create table(:profiles) do add :name, :string timestamps end end end

Slide 68

Slide 68 text

Rails Generator rails generate model User name:string bio:text Phoenix Generator mix phoenix.gen.model User users name:string bio:text

Slide 69

Slide 69 text

Rake & Bundler Mix

Slide 70

Slide 70 text

irb, ruby-debug, & PRY iex, Erlang debugger, & IEx.pry

Slide 71

Slide 71 text

Technical Requirement Server A Server B Http Server Nginx & Phusion Elixir Request Processing Ruby On Rails Elixir Long Running Requests Go Elixir Server-Wide State Redis Elixir Persistable Data Redis & Mongo Elixir Background Jobs Cron, Bash Scripts & Ruby Elixir Service Crash Recovery Upstart Elixir

Slide 72

Slide 72 text

Phoenix is inspired by Rails

Slide 73

Slide 73 text

Elixir is inspired by Ruby

Slide 74

Slide 74 text

Elixir == Ruby for Erlang Phoenix == Rails for Elixir

Slide 75

Slide 75 text

Elixir is not Ruby

Slide 76

Slide 76 text

Phoenix is not Rails

Slide 77

Slide 77 text

https://dockyard.com/blog/2015/11/18/phoenix-is-not-rails

Slide 78

Slide 78 text

Performance is the least interesting thing about Phoenix

Slide 79

Slide 79 text

Changed the world by enabling a generation of developers to economically build software-centric companies

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

OTP

Slide 83

Slide 83 text

Open Telecom Platform

Slide 84

Slide 84 text

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

Slide 85

Slide 85 text

How do you learn Phoenix?

Slide 86

Slide 86 text

Pre-existing domain knowledge speeds up adoption

Slide 87

Slide 87 text

Pre-existing domain knowledge speeds up adoption DockYard team one week to transition

Slide 88

Slide 88 text

Dave Thomas Hal Fulton Sasa Juric Chris McCord Bruce Tate Jose Valim

Slide 89

Slide 89 text

Dave Thomas Hal Fulton

Slide 90

Slide 90 text

W H Y P H O E N I X ?

Slide 91

Slide 91 text

W H Y P H O E N I X ? Order of magnitude performance Avoid having to wrap all code in caches Implementing features costs less money and requires less engineering support Optimizes for long-term developer happiness Takes about a week to re-train Rails developers Its going to change the world

Slide 92

Slide 92 text

Brian Cardarella @bcardarella If you want to build something great with Elixir or Phoenix hire @DockYard

Slide 93

Slide 93 text

Thank You!