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

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.

Kevin Rockwood

May 19, 2016
Tweet

More Decks by Kevin Rockwood

Other Decks in Technology

Transcript

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

    View Slide

  2. Elixir

    View Slide

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

    View Slide

  4. What is Object Oriented?

    View Slide

  5. • Classes
    • Methods
    • Instances
    • Inheritance

    View Slide

  6. • Java
    • Python
    • Ruby

    View Slide

  7. View Slide

  8. View Slide

  9. View Slide

  10. View Slide

  11. This is a lot like the internet

    View Slide

  12. ALAN KAY
    “I thought of objects being like biological cells
    and/or individual computers on a network, only
    able to communicate with messages”

    View Slide

  13. 1986

    View Slide

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

    View Slide

  15. Erlang / OTP Framework

    View Slide

  16. • Dynamic
    • Compiled
    • Functional
    • Immutable
    • Everything is a Processtm
    Erlang Language

    View Slide

  17. • Implements the actor model with Erlang processes
    • Libraries for managing the process lifecycle
    • Hot code swapping
    • Data persistance
    OTP Framework

    View Slide

  18. Switch
    Phone
    Phone
    Phone
    Phone
    Switch
    Phone
    Phone
    Phone
    Switch

    View Slide

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

    View Slide

  20. 2011

    View Slide

  21. Elixir

    View Slide

  22. Processes Communicate Via Messages

    View Slide

  23. Processes Have State
    > {:ok, pid} = MyProcess.start()
    > MyProcess.push(pid, "Hello World")
    []
    []
    ["Hello World"]

    View Slide

  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"]

    View Slide

  25. Let's Build a Web Crawler

    View Slide

  26. View Slide

  27. Web Server
    Socket Manager
    Crawlers
    Crawler Registry
    Connection Pool
    What Do We Need?
    App 1
    App 2 Umbrella

    View Slide

  28. Browser
    crawl(example.com) push("/page_1")
    ...
    Phoenix
    Channel
    Crawler
    Connection
    Pool

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  32. Demo!

    View Slide

  33. What is Object Oriented?

    View Slide

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

    View Slide

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

    View Slide

  36. Processes are what OO
    always intended

    View Slide

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

    View Slide