Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Erlang

Introduction to Erlang

Michał Ślaski

October 28, 2014
Tweet

More Decks by Michał Ślaski

Other Decks in Programming

Transcript

  1. Introduction  to  Erlang
    Michał  Ślaski          @michalslaski  
    !
    !
    !
         AKAI  Camp  Backend,  Poznań,  28.10.2014

    View full-size slide

  2. Erlang  timeline  and  my  background

    View full-size slide

  3. Erlang  timeline  and  my  background
    Started  out  in  the  Ericsson  software  lab  1987  (!)

    View full-size slide

  4. Erlang  timeline  and  my  background
    Started  out  in  the  Ericsson  software  lab  1987  (!)
    • I  was  six  years  old


    View full-size slide

  5. Erlang  timeline  and  my  background
    Started  out  in  the  Ericsson  software  lab  1987  (!)
    • I  was  six  years  old

    Released  as  open  source  in  1998

    View full-size slide

  6. Erlang  timeline  and  my  background
    Started  out  in  the  Ericsson  software  lab  1987  (!)
    • I  was  six  years  old

    Released  as  open  source  in  1998
    • I  started  computer  science  at  AGH  2  years  later


    View full-size slide

  7. Erlang  timeline  and  my  background
    Started  out  in  the  Ericsson  software  lab  1987  (!)
    • I  was  six  years  old

    Released  as  open  source  in  1998
    • I  started  computer  science  at  AGH  2  years  later

    Gains  SMP  support  in  2005

    View full-size slide

  8. Erlang  timeline  and  my  background
    Started  out  in  the  Ericsson  software  lab  1987  (!)
    • I  was  six  years  old

    Released  as  open  source  in  1998
    • I  started  computer  science  at  AGH  2  years  later

    Gains  SMP  support  in  2005
    • I  just  graduated  and  joined  Erlang  Solutions

    View full-size slide

  9. Erlang  properties

    View full-size slide

  10. Erlang  properties
    Declarative  
    • Functional  programming  language  
    • high  abstraction  level  
    • pattern  matching  
    • concise  and  readable  programs

    View full-size slide

  11. Erlang  properties  -­‐  functions
    Modules & Functions
    -module(demo).
    -export([double/1]).
    % This is a comment.
    % Everything after '%' is ignored.
    double(X) ->
    times(X, 2).
    times(X, N) ->
    X * N.

    View full-size slide

  12. Erlang  properties

    View full-size slide

  13. Erlang  properties
    Concurrency  
    • light-­‐weight  processes  (actor  model)  
    • highly  scalable


    View full-size slide

  14. Erlang  properties
    Concurrency  
    • light-­‐weight  processes  (actor  model)  
    • highly  scalable


    Soft  real-­‐time  
    • response  times  in  the  order  of  milliseconds  
    • per  process  garbage  collection

    View full-size slide

  15. Erlang  properties  -­‐  processes

    View full-size slide

  16. Erlang  properties  -­‐  processes

    View full-size slide

  17. Erlang  properties  -­‐  processes
    Pid = spawn(M, F, A)

    View full-size slide

  18. Erlang  properties  -­‐  processes
    Pid = spawn(M, F, A)

    View full-size slide

  19. Erlang  properties  -­‐  processes
    Pid = spawn(M, F, A)
    receive!
    {msg, From} ->!
    From ! ok!
    end

    View full-size slide

  20. Erlang  properties  -­‐  processes
    Pid = spawn(M, F, A)
    receive!
    {msg, From} ->!
    From ! ok!
    end
    Pid ! {msg, self()}

    View full-size slide

  21. Erlang  properties  -­‐  processes
    Pid = spawn(M, F, A)
    receive!
    {msg, From} ->!
    From ! ok!
    end
    Pid ! {msg, self()}
    ok

    View full-size slide

  22. Erlang  properties
    Robustness  
    • simple  and  consistent  error  recovery  
    • supervision  hierarchies  
    • program  for  the  correct  case  
    • let  it  crash

    View full-size slide

  23. Erlang  properties  -­‐  supervision  tree

    View full-size slide

  24. Erlang  properties  -­‐  supervision  tree

    View full-size slide

  25. Erlang  properties  -­‐  supervision  tree

    View full-size slide

  26. Erlang  properties  -­‐  supervision  tree
    supervisor

    View full-size slide

  27. Erlang  properties  -­‐  supervision  tree
    supervisor
    worker

    View full-size slide

  28. Erlang  properties  -­‐  supervision  tree
    supervisor
    worker

    View full-size slide

  29. Erlang  properties  -­‐  supervision  tree
    supervisor
    worker

    View full-size slide

  30. Erlang  properties  -­‐  supervision  tree
    supervisor
    worker

    View full-size slide

  31. Erlang  properties
    Distribution  
    • explicit  or  transparent  distribution  
    • network-­‐aware  runtime  system

    View full-size slide

  32. Erlang  properties  -­‐  distribution

    View full-size slide

  33. Erlang  properties  -­‐  distribution
    cat

    View full-size slide

  34. Erlang  properties  -­‐  distribution
    cat
    flea

    View full-size slide

  35. Erlang  properties  -­‐  distribution
    cat
    flea
    nodes() =
    [cat@home, flea@home]

    View full-size slide

  36. Erlang  properties  -­‐  distribution
    cat dog
    flea bird
    nodes() =
    [cat@home, flea@home]

    View full-size slide

  37. Erlang  properties  -­‐  distribution
    cat dog
    flea bird
    nodes() =
    [cat@home, flea@home]
    nodes() =
    [dog@home, bird@home]

    View full-size slide

  38. Erlang  properties  -­‐  distribution
    cat dog
    flea bird
    nodes() =
    [cat@home, flea@home]
    nodes() =
    [dog@home, bird@home]

    View full-size slide

  39. Erlang  properties  -­‐  distribution
    cat dog
    flea bird
    nodes() =
    [cat@home, flea@home, dog@work, bird@work]

    View full-size slide

  40. Erlang  properties

    View full-size slide

  41. Erlang  properties
    Hot  code  loading  
    • easily  change  code  in  a  running  system  
    • enables  non-­‐stop  operation  
    • simplifies  testing


    View full-size slide

  42. Erlang  properties
    Hot  code  loading  
    • easily  change  code  in  a  running  system  
    • enables  non-­‐stop  operation  
    • simplifies  testing

    SMP  support  
    • takes  advantage  of  multiple  CPU  architectures


    View full-size slide

  43. Erlang  properties
    Hot  code  loading  
    • easily  change  code  in  a  running  system  
    • enables  non-­‐stop  operation  
    • simplifies  testing

    SMP  support  
    • takes  advantage  of  multiple  CPU  architectures

    Erlang  Shell  
    • access  from  remote  nodes

    View full-size slide

  44. Erlang  properties

    View full-size slide

  45. Erlang  properties
    External  interfaces  
    • "Ports"  to  the  outside  world

    behave  as  Erlang  processes


    View full-size slide

  46. Erlang  properties
    External  interfaces  
    • "Ports"  to  the  outside  world

    behave  as  Erlang  processes

    Portability  
    • Erlang  runs  on  Unix,  Linux,  Mac  OS  X,  Windows  
    • RaspberryPi  and  Parallella  

    View full-size slide

  47. Demo  time!

    View full-size slide

  48. Demo  -­‐  SMP
    $ erl -smp disable
    Erlang/OTP 17 [erts-6.1] [source-d2a4c20] [async-threads:10]

    [hipe] [kernel-poll:false]
    !
    Eshell V6.1 (abort with ^G)

    1> F = fun(Int) -> pmap:factorial(Int) end.
    #Fun

    2> timer:tc(lists, map, [F,lists:seq(1, 5000)]).
    {12570092,
    [1,2,6,24,120,720,5040,40320,362880,3628800|...]}

    3> timer:tc(pmap, pmap, [F,lists:seq(1, 5000)]).
    {21511918,
    [1,2,6,24,120,720,5040,40320,362880,3628800|...]}

    4> element(2, v(2)) == element(2, v(3)).
    true

    View full-size slide

  49. Demo  -­‐  SMP
    $ erl -smp enable
    Erlang/OTP 17 [erts-6.1] [source-d2a4c20] [smp:4:4]

    [async-threads:10] [hipe] [kernel-poll:false]
    !
    Eshell V6.1 (abort with ^G)
    !
    1> F = fun(Int) -> pmap:factorial(Int) end.
    #Fun
    !
    2> timer:tc(lists, map, [F,lists:seq(1, 5000)]).
    {12535610,
    [1,2,6,24,120,720,5040,40320,362880,3628800|...]}
    !
    3> timer:tc(pmap, pmap, [F,lists:seq(1, 5000)]).
    {9159734,
    [1,2,6,24,120,720,5040,40320,362880,3628800|...]}

    View full-size slide

  50. Demo  -­‐  SMP
    no  SMP SMP
    map 12.5s 12.5s
    parallel  
    map
    21.5s 9.2s

    View full-size slide

  51. Demo  -­‐  SMP
    no  SMP SMP
    map 12.5s 12.5s
    parallel  
    map
    21.5s 9.2s

    View full-size slide

  52. Demo  -­‐  SMP
    no  SMP SMP
    map 12.5s 12.5s
    parallel  
    map
    21.5s 9.2s
    230%

    View full-size slide

  53. Open  source  projects

    View full-size slide

  54. Open  source  projects
    • RabbitMQ  message  broker  


    View full-size slide

  55. Open  source  projects
    • RabbitMQ  message  broker  

    • MongooseIM  XMPP  server


    View full-size slide

  56. Open  source  projects
    • RabbitMQ  message  broker  

    • MongooseIM  XMPP  server

    • Riak  database


    View full-size slide

  57. Open  source  projects
    • RabbitMQ  message  broker  

    • MongooseIM  XMPP  server

    • Riak  database

    • CouchDB  database

    View full-size slide

  58. Erlang  community

    View full-size slide

  59. Erlang  community
    Erlang  Central  
    • www.erlangcentral.org


    View full-size slide

  60. Erlang  community
    Erlang  Central  
    • www.erlangcentral.org

    Erlang  Factory  
    • Berlin,  December  4  
    • San  Francisco,  March  26-­‐27


    View full-size slide

  61. Erlang  community
    Erlang  Central  
    • www.erlangcentral.org

    Erlang  Factory  
    • Berlin,  December  4  
    • San  Francisco,  March  26-­‐27

    Lambda  Days  
    • Kraków,  February  26-­‐27

    View full-size slide

  62. Questions?
    !
    Michał  Ślaski          @michalslaski

    View full-size slide