Elixir: Why Processes Trump Objects

Elixir: Why Processes Trump Objects

Over the past quarter-century, we’ve witnessed the rise and widespread adoption of object-oriented (OO) languages. We bought into the promise that objects would solve all our technical challenges. That class hierarchies would bring order to our tangled messes. But something has gone horribly wrong.

The technical requirements of our software continue to rise, and our trusty OO languages aren’t keeping up. They’re unable to climb the ladder of complexity. How do we scale the complexity of a system without being crushed by the cognitive overhead it takes to keep it all in our head?

Kevin Rockwood explores how process-oriented architecture embodies the principles that object-oriented architecture always intended and explains how you can use processes to tackle complex problems that other languages could only dream about. For the past 30 years, Erlang, along with its pioneering runtime, has been quietly fighting alongside its OO brethren. Although its approach is drastically different from the OO languages of today, it’s actually much more closely aligned to object-oriented guiding principles. Now, Elixir is catapulting the pioneering approach of Erlang into the mainstream. The wave is approaching. Choose processes over objects, and embrace the magic of Elixir.

180276ea8f2063445af08593e20f53ec?s=128

Kevin Rockwood

May 19, 2016
Tweet

Transcript

  1. teamgaslight.com Elixir Why processes trump objects @kevinrockwood Kevin Rockwood

  2. Elixir

  3. I work for Gaslight in Cincinnati Hi, I'm Kevin

  4. What is Object Oriented?

  5. • Classes • Methods • Instances • Inheritance

  6. • Java • Python • Ruby

  7. None
  8. None
  9. None
  10. None
  11. This is a lot like the internet

  12. ALAN KAY “I thought of objects being like biological cells

    and/or individual computers on a network, only able to communicate with messages”
  13. 1986

  14. • Distributed • Fault-tolerant • Real-time • Highly available What

    Ericsson Needs:
  15. Erlang / OTP Framework

  16. • Dynamic • Compiled • Functional • Immutable • Everything

    is a Processtm Erlang Language
  17. • Implements the actor model with Erlang processes • Libraries

    for managing the process lifecycle • Hot code swapping • Data persistance OTP Framework
  18. Switch Phone Phone Phone Phone Switch Phone Phone Phone Switch

  19. Server Browser iOS Android PI Server Arduino Browser iOS Server

  20. 2011

  21. Elixir

  22. Processes Communicate Via Messages

  23. Processes Have State > {:ok, pid} = MyProcess.start() > MyProcess.push(pid,

    "Hello World") [] [] ["Hello World"]
  24. Processes Have State defmodule MyProcess do def init() do {:ok,

    []} end def handle_call({:push, message}, state) do {:ok, [message | state]} end end > {:ok, pid} = MyProcess.start_link // state = [] > MyProcess.push(pid, "Hello World") // state = ["Hello World"]
  25. Let's Build a Web Crawler

  26. None
  27. Web Server Socket Manager Crawlers Crawler Registry Connection Pool What

    Do We Need? App 1 App 2 Umbrella
  28. Browser crawl(example.com) push("/page_1") ... Phoenix Channel Crawler Connection Pool

  29. Supervisor Registry Connection Pool Crawler github.com Crawler oscon.com Crawler example.com

  30. // Phoenix Channel def handle_in("crawl", %{"url" => url}, socket) do

    Crawler.start(url) receive do {:page, new_page} -> push(socket, "new_page", new_page) {:link, new_link} -> push(socket, "new_link", new_link) end end
  31. %{ root: "example.com", pages: %{ "example.com" => %{id: 1} "example.com/page_1"

    => %{id: 2} "example.com/page_3" => %{id: 3} }, links: [ %{target: 1, source: 2}, %{target: 2, source: 1}, %{target: 2, source: 3}, %{target: 3, source: 1}, ] } Crawler Connection Pool
  32. Demo!

  33. What is Object Oriented?

  34. • Objects should be completely isolated • Objects should only

    communicate via message passing • Objects should be fault tolerant What is Object Oriented?
  35. • Processes should be completely isolated • Processes should only

    communicate via messaging • Processes should be fault tolerant What is Object Oriented?
  36. Processes are what OO always intended

  37. teamgaslight.com Elixir Why processes trump objects @kevinrockwood Kevin Rockwood