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

Elixir - Peeking into Processes, OTP & Supervisors

Elixir - Peeking into Processes, OTP & Supervisors

Talk I gave at Paypal Singapore on the 21st of March 2014 for Hackers and Painters.

F3fe8fa67ae6dbc1d19189e80c9e96a2?s=128

Benjamin Tan Wei Hao

March 21, 2014
Tweet

Transcript

  1. Elixir Peeking into Elixir’s Processes, OTP & Supervisors 21st March

    2014 /benjamintanweihao /bentanweihao benjamintanweihao@gmail.com
  2. What will we learn today? Elixir & Erlang In less

    than 5 minutes
  3. Elixir & Erlang In less than 5 minutes Processes 101

    The Basic Concurrency Primitive What will we learn today?
  4. Elixir & Erlang In less than 5 minutes OTP Framework

    and much more Processes 101 The Basic Concurrency Primitive What will we learn today?
  5. Elixir & Erlang In less than 5 minutes OTP Framework

    and much more Supervisors Fault Tolerance & Recovery Processes 101 The Basic Concurrency Primitive What will we learn today?
  6. Ohai, Elixir!

  7. Elixir & Erlang In less than 5 minutes

  8. Ohai, Erlang! Erlang is a general-purpose concurrent, garbage-collected programming language

    and runtime system. The sequential subset of Erlang is a functional language, with eager evaluation, single assignment, and dynamic typing. It was designed by Ericsson to support distributed, fault-tolerant, soft-real-time, non- stop applications. It supports hot swapping, so that code can be changed without stopping a system.
  9. Ohai, Erlang! Erlang is a general-purpose concurrent, garbage-collected programming language

    and runtime system. The sequential subset of Erlang is a functional language, with eager evaluation, single assignment, and dynamic typing. It was designed by Ericsson to support distributed, fault-tolerant, soft-real-time, non- stop applications. It supports hot swapping, so that code can be changed without stopping a system.
  10. • Free lunch is over Why Elixir ?

  11. Why Elixir ? • Free lunch is over • Hyper-threading

    & Multicore • Faster software means using all cores! • But … Concurrency -> Coordination • Functional makes this easier
  12. Design Goals of Elixir 1.Productivity 2.Extensibility 3.Compatibility

  13. Productivity Complete Elixir Application

  14. Productivity Includes Supervisor Chain

  15. Productivity Testing built-in

  16. Extensibility Macros & Meta-programming Implementing unless using if

  17. Extensibility Macros & Meta-programming Implementing unless using if

  18. Compatibility Elixir can call Erlang code, without any conversion cost

    at all.
  19. Compatibility Elixir can use Erlang libraries!

  20. The Actor Concurrency Model • Actor = Process

  21. The Actor Concurrency Model • Actor = Process • A

    process performs a specific task when it receives a message
  22. The Actor Concurrency Model • Actor = Process • A

    process performs a specific task when it receives a message • In turn, the process can reply to the sender
  23. The Actor Concurrency Model • Actor = Process • A

    process performs a specific task when it receives a message • In turn, the process can reply to the sender • All messages go to a processes’ mailbox – Q of unprocessed messages sent from other processes that are not yet consumed
  24. The Actor Concurrency Model • Actor = Process • A

    process performs a specific task when it receives a message • In turn, the process can reply to the sender • All messages go to a processes’ mailbox – Q of unprocessed messages sent from other processes that are not yet consumed Shared-nothing Async Message-passing
  25. Processes 101 The Basic Concurrency Primitive

  26. Creating Processes & Sending Messages in Elixir Creating a Process

  27. Creating Processes & Sending Messages in Elixir Module, Function, Arguments

  28. Creating Processes & Sending Messages in Elixir Process id

  29. Creating Processes & Sending Messages in Elixir Sending a Message

    to w1
  30. Creating Processes & Sending Messages in Elixir Process waits for

    a message …
  31. Creating Processes & Sending Messages in Elixir Pattern matches!

  32. Creating Processes & Sending Messages in Elixir Result is sent

    back to the calling process (self)
  33. Creating Processes & Sending Messages in Elixir Returns immediately

  34. Creating Processes & Sending Messages in Elixir Get result from

    self
  35. Creating Processes & Sending Messages in Elixir Demo

  36. OTP Framework and much more

  37. What is OTP? • Comes with Elixir/Erlang • Framework to

    build applications that are fault- tolerant, scalable, distributed • Databases + Profilers + Debuggers
  38. OTP Behaviours • GenServer • Supervisor • Application

  39. An Example GenServer Implement the GenServer Behaviour

  40. An Example GenServer Implement GenServer Callbacks

  41. An Example GenServer Callbacks are NOT called explicitly

  42. An Example GenServer OTP calls the callbacks.

  43. An Example GenServer Synchronous Call: Caller waits for reply

  44. An Example GenServer Asynchronous Call: Caller doesn’t wait for reply

  45. An Example GenServer Demo

  46. Supervisors Fault Tolerance & Recovery

  47. Supervisors for Fault Tolerance and Recovery one_for_one restart strategy

  48. Supervisors for Fault Tolerance and Recovery rest_for_all restart strategy

  49. Supervisors for Fault Tolerance and Recovery rest_for_one restart strategy

  50. An Example Supervisor Implement the Supervisor Behaviour

  51. An Example Supervisor Declaring the Supervision tree. Both Supervisors and

    Workers (e.g. GenServers) can be supervised.
  52. An Example Supervisor

  53. An Example Supervisor Declare the restart strategy

  54. Supervisor Demo Supervisor A Supervisor B Supervisor C Supervisor D

    Server D Server B Worker 1 Worker 1 Worker
  55. Supervisor Demo Supervisor A Supervisor B Supervisor C Supervisor D

    Server D Server B Worker 1 Worker 1 Worker one_for_one
  56. Supervisor Demo Supervisor A Supervisor B Supervisor C Supervisor D

    Server D Server B Worker 1 Worker 1 Worker one_for_all
  57. Supervisor Demo Supervisor A Supervisor B Supervisor C Supervisor D

    Server D Server B Worker 1 Worker 1 Worker simple_one_for_one
  58. Supervisor Demo Supervisor A Supervisor B Supervisor C Supervisor D

    Server D Server B Worker 1 Worker 1 Worker one_for_one
  59. Supervisor Demo Supervisor A Supervisor B Supervisor C Supervisor D

    Server D Server B Worker 1 Worker 1 Worker one_for_one simple_one_for_one one_for_all one_for_one
  60. Elixir & Erlang In less than 5 minutes OTP Framework

    and much more Supervisors Fault Tolerance & Recovery Processes 101 The Basic Concurrency Primitive
  61. Resources

  62. Work in Progress! ! Sign up at: http://www.exotpbook.com Me.

  63. Stickers!* *Very Limited Quantity

  64. Thanks! /benjamintanweihao /bentanweihao benjamintanweihao@gmail.com