Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Elixir & Phoenix
Functional |> Concurrent |> Pragmatic |> Fun
Who am I?
• Jared Smith
• Software Engineer at One Month
• @sublimecoder
• www.sublimecoding.com
Lets get started
First lets talk about Erlang
• Elixir runs on the Erlang VM
• Many of the standard lib function calls in Elixir
call Erlan...
Why Erlang VM?
• 30 year old battle tested VM
• Highly available, fault tolerant, and distributed
History of Erlang
• Developed by Ericsson
• Used and developed for Telecommunications
• In fact its standard library is th...
Telecommunications
• High availability is king
• Remote Telecom switches need to run for years
sometimes with out being di...
Built with Erlang
• 50 person engineering team
• managing 900 million users.
• Up to 2 million active users per server.
ht...
2 Million Active Users?
• Intel Xeon CPU x5675 @ 3.07GHz

24 Cores - 96GB Ram
• Using only 40% of the CPU/Memory
https://b...
So Erlang is pretty awesome
• Wouldn’t it be nice if we could harness this
power?
• Why isn’t everyone using Erlang?
Elixir
• Elixir utilizes much of the power of Erlang
• While giving us a much nicer syntax and api to
work with.
History of Elixir
• Created by Josè Valím - (Joe-zay Val-eem)
• First released in 2012
Elixir’s Strengths
• Concurrency
• Meta-programming Capability
• Transformation of data as a core ideal
Plus it has Ruby like syntax
Pipe Operator
• The following code is valid elixir.
• With the pipe operator we can write our code this
way instead. Keepi...
Pattern Matching
• Pattern matching is a core part of Erlang and
Elixir
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]
Immutability
• Once you set a value 

it can never change.
Immutability
You’re already familiar with
immutability you just don’t know it.
• a = 99
• We all know 99 is always 99.
• integers are i...
Why is Immutability a good
thing?
• Immutability provides a contract that each piece
of information has one source of trut...
Does this mean I can never
change a variable once I set it?
• The short answer is no, you can reassign
variables as much a...
So how is it immutable if I
can change things?
• You can change things, elixir will just copy the
values you don’t change,...
Isn’t this inefficient?
• At first glance it would seem that way, but the
opposite is true.
• Won’t all the old reference va...
Concurrency
Concurrency fixes
everything
• Because the data is immutable we can rely on it
always being a certain value
• which means w...
Concurrency
• Elixir will eat as many cores/computers as you
can throw at it.
• It treats each core as though it were a co...
Phoenix Framework
Productive |> Reliable |> Fast
Phoenix Framework
• Created by Chris McCord
• Release 1.0 August 28th 2015
Phoenix Framework
• It’s not rails for Elixir
• It has been influence heavily by rails as well as
many other frameworks.
• ...
Speed
• Web request response time is often measured in
micro seconds and not milliseconds.
Benchmarks
• Recently benchmarked 2 million+ web socket
connections
• With 2 - 3 second broadcast times to all 2 million
c...
Benchmarks
https://github.com/mroth/phoenix-showdown
iMac Intel Core i7 (4.0 GHz 4 Cores) 32 GB RAM
https://github.com/mroth/phoenix-showdown
2.8 Ghz, 10 core Intel® Xeon 128 GB RAM
Easy Startup
• mix phoenix.new ./app/path/my_app
• mix ecto.create && mix ecto.migrate
• mix phoenix.server
Directory Structure
Web Dir Structure
Mix
• Mix is essentially bundler and rake combined
• Additionally mix is your test runner.
• It’s included with Elixir
Rails like Generators
• mix phoenix.gen.html Post posts title body:text
• mix phoenix.gen.json User users name:string
• mi...
Concurrency
• Async processing with out the need for
DelayedJob or Resque/sidekiq
• We can write code that looks like this.
Isolated & Concurrent
• Crashes are isolated
• Data is isolated 

(GC is per process, no global pauses)
• Load Balances on...
Garbage Collection
• No triggering massive garbage collection
• GC per process / end of process life cycle
• Ensuring top ...
Request Pipeline
• Just a series of function calls that transform
data.
• connection 

|> endpoint

|> router

|> pipeline...
Request Pipeline
• Phoenix makes it easy to add middleware or
plugs to this pipeline. We could easily add
authentication t...
Explicitness Over Magic
• Phoenix Removes a lot of the confusion and
“magic” found in rails and replaces it with
explicit ...
Phoenix Router
Phoenix Router
Phoenix Router
Phoenix Controllers
Phoenix Model
Phoenix View
Phoenix Template
Learn More
• Elixir-Lang on Slack 

https://elixir-slackin.herokuapp.com/
• #elixir-lang on Freenode IRC
• http://www.phoe...
Intro to elixir and phoenix
Intro to elixir and phoenix
Upcoming SlideShare
Loading in …5
×

Intro to elixir and phoenix

3,157 views

Published on

Introduction to Elixir and Phoenix given at the Bako Ruby Meetup on 4/5/2016

Published in: Technology

Intro to elixir and phoenix

  1. 1. Elixir & Phoenix Functional |> Concurrent |> Pragmatic |> Fun
  2. 2. Who am I? • Jared Smith • Software Engineer at One Month • @sublimecoder • www.sublimecoding.com
  3. 3. Lets get started
  4. 4. 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
  5. 5. Why Erlang VM? • 30 year old battle tested VM • Highly available, fault tolerant, and distributed
  6. 6. History of Erlang • Developed by Ericsson • Used and developed for Telecommunications • In fact its standard library is the OTP (Open Telecom Platform)
  7. 7. Telecommunications • High availability is king • Remote Telecom switches need to run for years sometimes with out being disturbed
  8. 8. 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
  9. 9. 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
  10. 10. So Erlang is pretty awesome • Wouldn’t it be nice if we could harness this power? • Why isn’t everyone using Erlang?
  11. 11. Elixir • Elixir utilizes much of the power of Erlang • While giving us a much nicer syntax and api to work with.
  12. 12. History of Elixir • Created by Josè Valím - (Joe-zay Val-eem) • First released in 2012
  13. 13. Elixir’s Strengths • Concurrency • Meta-programming Capability • Transformation of data as a core ideal
  14. 14. Plus it has Ruby like syntax
  15. 15. 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.
  16. 16. Pattern Matching • Pattern matching is a core part of Erlang and Elixir
  17. 17. 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]
  18. 18. Immutability • Once you set a value 
 it can never change.
  19. 19. Immutability
  20. 20. 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?
  21. 21. 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.
  22. 22. 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.
  23. 23. 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.
  24. 24. 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
  25. 25. Concurrency
  26. 26. 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.
  27. 27. 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.
  28. 28. Phoenix Framework Productive |> Reliable |> Fast
  29. 29. Phoenix Framework • Created by Chris McCord • Release 1.0 August 28th 2015
  30. 30. 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
  31. 31. Speed • Web request response time is often measured in micro seconds and not milliseconds.
  32. 32. Benchmarks • Recently benchmarked 2 million+ web socket connections • With 2 - 3 second broadcast times to all 2 million connections
  33. 33. Benchmarks
  34. 34. https://github.com/mroth/phoenix-showdown iMac Intel Core i7 (4.0 GHz 4 Cores) 32 GB RAM
  35. 35. https://github.com/mroth/phoenix-showdown 2.8 Ghz, 10 core Intel® Xeon 128 GB RAM
  36. 36. Easy Startup • mix phoenix.new ./app/path/my_app • mix ecto.create && mix ecto.migrate • mix phoenix.server
  37. 37. Directory Structure
  38. 38. Web Dir Structure
  39. 39. Mix • Mix is essentially bundler and rake combined • Additionally mix is your test runner. • It’s included with Elixir
  40. 40. 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
  41. 41. Concurrency • Async processing with out the need for DelayedJob or Resque/sidekiq • We can write code that looks like this.
  42. 42. 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
  43. 43. 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
  44. 44. Request Pipeline • Just a series of function calls that transform data. • connection 
 |> endpoint
 |> router
 |> pipelines
 |> controller
  45. 45. 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
  46. 46. 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.
  47. 47. Phoenix Router
  48. 48. Phoenix Router
  49. 49. Phoenix Router
  50. 50. Phoenix Controllers
  51. 51. Phoenix Model
  52. 52. Phoenix View
  53. 53. Phoenix Template
  54. 54. 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

×