intro to elixir and phoenix

56
Elixir & Phoenix Functional |> Concurrent |> Pragmatic |> Fun

Upload: jared-smith

Post on 09-Feb-2017

2.381 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: Intro to elixir and phoenix

Elixir & PhoenixFunctional |> Concurrent |> Pragmatic |> Fun

Page 2: Intro to elixir and phoenix

Who am I?

• Jared Smith

• Software Engineer at One Month

• @sublimecoder

• www.sublimecoding.com

Page 3: Intro to elixir and phoenix

Lets get started

Page 4: Intro to elixir and phoenix

First lets talk about Erlang

• Elixir runs on the Erlang VM

• Many of the standard lib function calls in Elixir call Erlang functions directly.

• Elixir piggy backs on the Erlang ecosystem

Page 5: Intro to elixir and phoenix

Why Erlang VM?

• 30 year old battle tested VM

• Highly available, fault tolerant, and distributed

Page 6: Intro to elixir and phoenix

History of Erlang

• Developed by Ericsson

• Used and developed for Telecommunications

• In fact its standard library is the OTP (Open Telecom Platform)

Page 7: Intro to elixir and phoenix

Telecommunications

• High availability is king

• Remote Telecom switches need to run for years sometimes with out being disturbed

Page 8: Intro to elixir and phoenix

Built with Erlang

• 50 person engineering team

• managing 900 million users.

• Up to 2 million active users per server.

https://blog.whatsapp.com/196/1-million-is-so-2011

Page 9: Intro to elixir and phoenix

2 Million Active Users?

• Intel Xeon CPU x5675 @ 3.07GHz 24 Cores - 96GB Ram

• Using only 40% of the CPU/Memory

https://blog.whatsapp.com/196/1-million-is-so-2011

Page 10: Intro to elixir and phoenix

So Erlang is pretty awesome

• Wouldn’t it be nice if we could harness this power?

• Why isn’t everyone using Erlang?

Page 11: Intro to elixir and phoenix

Elixir

• Elixir utilizes much of the power of Erlang

• While giving us a much nicer syntax and api to work with.

Page 12: Intro to elixir and phoenix

History of Elixir

• Created by Josè Valím - (Joe-zay Val-eem)

• First released in 2012

Page 13: Intro to elixir and phoenix

Elixir’s Strengths

• Concurrency

• Meta-programming Capability

• Transformation of data as a core ideal

Page 14: Intro to elixir and phoenix

Plus it has Ruby like syntax

Page 15: Intro to elixir and phoenix

Pipe Operator• The following code is valid elixir.

• With the pipe operator we can write our code this way instead. Keeping us out of nested function hell.

Page 16: Intro to elixir and phoenix

Pattern Matching

• Pattern matching is a core part of Erlang and Elixir

Page 17: Intro to elixir and phoenix

What is Pattern Matching?

• Make left side look like the right side

• a = 99

• [a,b,c] = [1,2,3]

• [head | tail] = [1,2,3]

Page 18: Intro to elixir and phoenix

Immutability

• Once you set a value it can never change.

Page 19: Intro to elixir and phoenix

Immutability

Page 20: Intro to elixir and phoenix

You’re already familiar with immutability you just don’t know it.

• a = 99

• We all know 99 is always 99.

• integers are immutable values, even in Ruby.

• You’d be upset if someone monkey patched 99 to be 3 right?

Page 21: Intro to elixir and phoenix

Why is Immutability a good thing?

• Immutability provides a contract that each piece of information has one source of truth.

• Concurrency is a natural side effect of immutability.

Page 22: Intro to elixir and phoenix

Does this mean I can never change a variable once I set it?

• The short answer is no, you can reassign variables as much as you like.

• Erlang would not be so kind, but Elixir we have this luxury.

Page 23: Intro to elixir and phoenix

So how is it immutable if I can change things?

• You can change things, elixir will just copy the values you don’t change, but the original reference stays intact.

• The original reference can then be used in part or in whole later in the application.

Page 24: Intro to elixir and phoenix

Isn’t this inefficient?

• At first glance it would seem that way, but the opposite is true.

• Won’t all the old reference values balloon elixirs memory?

• Yes this means you could have thousands of unused variables floating in memory

Page 25: Intro to elixir and phoenix

Concurrency

Page 26: Intro to elixir and phoenix

Concurrency fixes everything

• Because the data is immutable we can rely on it always being a certain value

• which means we can make our programs concurrent and not worry.

• Elixir uses its concurrency to help with garbage collections. Each process gets it own heap allocated and that heap is garbage collected or reclaimed as needed for that individual process.

Page 27: Intro to elixir and phoenix

Concurrency

• Elixir will eat as many cores/computers as you can throw at it.

• It treats each core as though it were a computer on the network with near zero latency.

Page 28: Intro to elixir and phoenix
Page 29: Intro to elixir and phoenix

Phoenix Framework Productive |> Reliable |> Fast

Page 30: Intro to elixir and phoenix

Phoenix Framework

• Created by Chris McCord

• Release 1.0 August 28th 2015

Page 31: Intro to elixir and phoenix
Page 32: Intro to elixir and phoenix

Phoenix Framework

• It’s not rails for Elixir

• It has been influence heavily by rails as well as many other frameworks.

• Focus on API and web sockets

Page 33: Intro to elixir and phoenix

Speed• Web request response time is often measured in

micro seconds and not milliseconds.

Page 34: Intro to elixir and phoenix

Benchmarks

• Recently benchmarked 2 million+ web socket connections

• With 2 - 3 second broadcast times to all 2 million connections

Page 35: Intro to elixir and phoenix

Benchmarks

Page 36: Intro to elixir and phoenix

https://github.com/mroth/phoenix-showdown

iMac Intel Core i7 (4.0 GHz 4 Cores) 32 GB RAM

Page 37: Intro to elixir and phoenix

https://github.com/mroth/phoenix-showdown

2.8 Ghz, 10 core Intel® Xeon 128 GB RAM

Page 38: Intro to elixir and phoenix

Easy Startup

• mix phoenix.new ./app/path/my_app

• mix ecto.create && mix ecto.migrate

• mix phoenix.server

Page 39: Intro to elixir and phoenix

Directory Structure

Page 40: Intro to elixir and phoenix

Web Dir Structure

Page 41: Intro to elixir and phoenix

Mix

• Mix is essentially bundler and rake combined

• Additionally mix is your test runner.

• It’s included with Elixir

Page 42: Intro to elixir and phoenix

Rails like Generators

• mix phoenix.gen.html Post posts title body:text

• mix phoenix.gen.json User users name:string

• mix phoenix.gen.model User users name:string

Page 43: Intro to elixir and phoenix

Concurrency

• Async processing with out the need for DelayedJob or Resque/sidekiq

• We can write code that looks like this.

Page 44: Intro to elixir and phoenix

Isolated & Concurrent

• Crashes are isolated

• Data is isolated (GC is per process, no global pauses)

• Load Balances on IO and CPU - Efficient on a multicore

Page 45: Intro to elixir and phoenix

Garbage Collection

• No triggering massive garbage collection

• GC per process / end of process life cycle

• Ensuring top 10 percent of requests are not vastly slower than any other request

Page 46: Intro to elixir and phoenix

Request Pipeline

• Just a series of function calls that transform data.

• connection |> endpoint|> router|> pipelines|> controller

Page 47: Intro to elixir and phoenix

Request Pipeline• Phoenix makes it easy to add middleware or

plugs to this pipeline. We could easily add authentication to this.

• connection |> endpoint|> router|> pipelines|> authenticate_user|> controller

Page 48: Intro to elixir and phoenix

Explicitness Over Magic

• Phoenix Removes a lot of the confusion and “magic” found in rails and replaces it with explicit calls.

• Models, Controllers and Views are singular named by convention.

Page 49: Intro to elixir and phoenix

Phoenix Router

Page 50: Intro to elixir and phoenix

Phoenix Router

Page 51: Intro to elixir and phoenix

Phoenix Router

Page 52: Intro to elixir and phoenix

Phoenix Controllers

Page 53: Intro to elixir and phoenix

Phoenix Model

Page 54: Intro to elixir and phoenix

Phoenix View

Page 55: Intro to elixir and phoenix

Phoenix Template

Page 56: Intro to elixir and phoenix

Learn More• Elixir-Lang on Slack

https://elixir-slackin.herokuapp.com/

• #elixir-lang on Freenode IRC

• http://www.phoenixframework.org

• http://elixir-lang.org/

• https://github.com/h4cc/awesome-elixir