Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up
for free
Elixir - Part 1
Matthew Rudy Jacobs
August 12, 2015
Technology
1
130
Elixir - Part 1
A talk about Elixir and OTP given at Codeaholics in Hong Kong on August 12th 2015
Matthew Rudy Jacobs
August 12, 2015
Tweet
Share
More Decks by Matthew Rudy Jacobs
See All by Matthew Rudy Jacobs
matthewrudy
3
180
matthewrudy
0
72
matthewrudy
0
47
matthewrudy
0
28
matthewrudy
0
96
matthewrudy
0
47
matthewrudy
3
290
matthewrudy
0
76
matthewrudy
0
29
Other Decks in Technology
See All in Technology
hayatan
0
200
110y
2
11k
tosh2230
3
320
simosako
1
150
hmatsu47
5
740
pakio
0
140
joytomo
0
110
dena_tech
0
190
htomine
0
190
mahito
0
250
ippey
2
210
armaniacs
0
360
Featured
See All Featured
hannesfritz
28
950
lara
15
2.7k
moore
125
21k
jlugia
217
16k
philhawksworth
190
17k
chriscoyier
145
20k
carmenhchung
31
1.5k
jponch
103
5.1k
sugarenia
233
860k
lara
590
61k
jonrohan
1021
380k
philnash
9
590
Transcript
Real-time Apps With Elixir Matthew Rudy Jacobs @ Codeaholics HK
THE PROBLEM SPACE
Line 100s of millions of online users Can’t lose a
message Don’t want downtime
Uber 100,000s online users Can’t lose a message Don’t want
downtime
GoGoVan ? online users Can’t lose a message Don’t want
downtime
THE SOLUTION?
Erlang The Movie
Ericsson (in 1998) 99.9999999% UPTIME 32MS / YEAR
Ericsson (in 1998) 99.9999999% UPTIME < 1SECOND / 20 YEARS
None
Whatsapp 64 billion messages per day 700 million monthly active
users 1000 servers doing the work
4 Parts
Erlang Elixir OTP Phoenix
Part 1 Erlang
WHAT IS ERLANG?
Erlang • functional • concurrent • realtime
Erlang • pattern matching • message passing • actors •
hot code reloading
-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?
None
Part 2 OTP
OTP IS A FRAMEWORK
OTP IS A SET OF CONVENTIONS
YOUR APPLICATION IS MADE OF “SERVERS” “AGENTS” AND “STATE MACHINES”
KEPT ALIVE BY A COMPLICATED TREE OF SUPERVISORS
None
Part 3 Elixir
Jose Valim
#4 Rails Contributor
Creator of Devise
Creator of Elixir
“I felt the same way I felt when I first
saw Ruby.”
ERLANG AND RUBY HAD A BABY
%%% 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
# 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
# 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
“str”<>”ings”
:atoms
"with #{:interpolation}"
[:list, “type”]
{:tuple, “type”}
[key: “word”, list: “…”]
%{map: “is”, like: “a hash”}
defmodule My.Module do def exported() do 42 end defp private()
do 76 end end
[head|tail] = [:a, :b, :c]
defmodule Math do def sum([head|tail], acc) do sum(tail, head +
acc) end def sum_list([], acc) do acc end end
bessy = %{ type: “cow”, name: “bessy” } %{name: name}
= bessy name == “bessy”
fn x -> 2*x end
list = [1,3,3,7] Enum.sum( Enum.map(list, fn x -> x*x end)
) # 68
[1,3,3,7] |> Enum.map(fn x -> x*x end) |> Enum.sum #
68
> iex
Part 4 Phoenix
Elixir on Phoenix
Phoenix Ecto for the database Plug for routing Channels for
websockets
mix phoenix.new phoenix_play
MORE NEXT TIME
Appendix A Thoughts
Gateway Todos Users Auth SERVICES OVER HTTP Mailer Search •HTTP
•Ports •Monitoring •Scaling •Logging Issues
Gateway Todos Users Auth A SINGLE OTP SYSTEM Mailer Search
•HTTP •Ports •Monitoring •Scaling •Logging Issues
Ruby people finally with technology that can scale
PEPE Postgres Elixir Phoenix Ember
Appendix B Addendum
None
THANKS
None
COME WORK AT GOGOVAN!!!