Elixir - Part 1

Elixir - Part 1

A talk about Elixir and OTP given at Codeaholics in Hong Kong on August 12th 2015

E60b2dc57668b5662ce3f07781e41710?s=128

Matthew Rudy Jacobs

August 12, 2015
Tweet

Transcript

  1. Real-time Apps With Elixir Matthew Rudy Jacobs @ Codeaholics HK

  2. THE PROBLEM SPACE

  3. Line 100s of millions of online users Can’t lose a

    message Don’t want downtime
  4. Uber 100,000s online users Can’t lose a message Don’t want

    downtime
  5. GoGoVan ? online users Can’t lose a message Don’t want

    downtime
  6. THE SOLUTION?

  7. Erlang The Movie

  8. Ericsson (in 1998) 99.9999999% UPTIME 32MS / YEAR

  9. Ericsson (in 1998) 99.9999999% UPTIME < 1SECOND / 20 YEARS

  10. None
  11. Whatsapp 64 billion messages per day 700 million monthly active

    users 1000 servers doing the work
  12. 4 Parts

  13. Erlang Elixir OTP Phoenix

  14. Part 1 Erlang

  15. WHAT IS ERLANG?

  16. Erlang • functional • concurrent • realtime

  17. Erlang • pattern matching • message passing • actors •

    hot code reloading
  18. -module(roman). -export([to_roman/1]). to_roman(0) -> []; to_roman(X) when X >= 1000

    -> [$M | to_roman(X - 1000)]; to_roman(X) when X >= 100 -> digit(X div 100, $C, $D, $M) ++ to_roman(X rem 100); to_roman(X) when X >= 10 -> digit(X div 10, $X, $L, $C) ++ to_roman(X rem 10); to_roman(X) when X >= 1 -> digit(X, $I, $V, $X). digit(1, X, _, _) -> [X]; digit(2, X, _, _) -> [X, X]; digit(3, X, _, _) -> [X, X, X]; digit(4, X, Y, _) -> [X, Y]; digit(5, _, Y, _) -> [Y]; digit(6, X, Y, _) -> [Y, X]; digit(7, X, Y, _) -> [Y, X, X]; digit(8, X, Y, _) -> [Y, X, X, X]; digit(9, X, _, Z) -> [X, Z]. Complex? Ugly?
  19. None
  20. Part 2 OTP

  21. OTP IS A FRAMEWORK

  22. OTP IS A SET OF CONVENTIONS

  23. YOUR APPLICATION IS MADE OF “SERVERS” “AGENTS” AND “STATE MACHINES”

    KEPT ALIVE BY A COMPLICATED TREE OF SUPERVISORS
  24. None
  25. Part 3 Elixir

  26. Jose Valim

  27. #4 Rails Contributor

  28. Creator of Devise

  29. Creator of Elixir

  30. “I felt the same way I felt when I first

    saw Ruby.”
  31. ERLANG AND RUBY HAD A BABY

  32. %%% erlang -module(recursve). -export([fac/1]). fac(N) when N == 0 ->

    1; fac(N) when N > 0 -> N*fac(N-1). # Ruby module Recursive def fac(n) return 1 if n == 0 fail ArgumentError unless n > 0 n*fac(n-1) end end
  33. # Elixir defmodule Recursive do def fac(n) when n ==

    0, do: 1 def fac(n) when n > 0 do n*fac(n-1) end end
  34. # Elixir - with tail call defmodule Recursive do def

    fac(0), do: 1 def fac(n) when n > 0 do fac(n, 1) end defp fac(1, acc), do: acc defp fac(n, acc) do fac(n-1, acc*n) end end
  35. “str”<>”ings”

  36. :atoms

  37. "with #{:interpolation}"

  38. [:list, “type”]

  39. {:tuple, “type”}

  40. [key: “word”, list: “…”]

  41. %{map: “is”, like: “a hash”}

  42. defmodule My.Module do def exported() do 42 end defp private()

    do 76 end end
  43. [head|tail] = [:a, :b, :c]

  44. defmodule Math do def sum([head|tail], acc) do sum(tail, head +

    acc) end def sum_list([], acc) do acc end end
  45. bessy = %{ type: “cow”, name: “bessy” } %{name: name}

    = bessy name == “bessy”
  46. fn x -> 2*x end

  47. list = [1,3,3,7] Enum.sum( Enum.map(list, fn x -> x*x end)

    ) # 68
  48. [1,3,3,7] |> Enum.map(fn x -> x*x end) |> Enum.sum #

    68
  49. > iex

  50. Part 4 Phoenix

  51. Elixir on Phoenix

  52. Phoenix Ecto for the database Plug for routing Channels for

    websockets
  53. mix phoenix.new phoenix_play

  54. MORE NEXT TIME

  55. Appendix A Thoughts

  56. Gateway Todos Users Auth SERVICES OVER HTTP Mailer Search •HTTP

    •Ports •Monitoring •Scaling •Logging Issues
  57. Gateway Todos Users Auth A SINGLE OTP SYSTEM Mailer Search

    •HTTP •Ports •Monitoring •Scaling •Logging Issues
  58. Ruby people finally with technology that can scale

  59. PEPE Postgres Elixir Phoenix Ember

  60. Appendix B Addendum

  61. None
  62. THANKS

  63. None
  64. COME WORK AT GOGOVAN!!!