$30 off During Our Annual Pro Sale. View Details »

Let's create stateful systems, by Elixir

Let's create stateful systems, by Elixir

さっちゃん

August 24, 2019
Tweet

More Decks by さっちゃん

Other Decks in Programming

Transcript

  1. Let's create stateful
    systems, by Elixir

    View Slide

  2. .
    。oO(
    さっちゃんですよヾ(
    〃l _ l)
    ノ゙☆)

    View Slide

  3. I'll show you a number.

    View Slide

  4. 2

    View Slide

  5. Two

    View Slide


  6. View Slide


  7. View Slide

  8. 2 is a number of collison & collaboration.

    View Slide

  9. I & you
    hot & ice
    stand up & lie down
    inside & outside

    View Slide

  10. stateless & stateful

    View Slide

  11. Function is stateless. f : x -> y
    e.g. Web server

    View Slide

  12. Dialogue is stateful.
    Alice : X.
    Bob : Y.
    Alice : X.
    Bob : Z.
    Most of the system is stateful.

    View Slide

  13. Elixir

    View Slide

  14. Elixir is a functional programming language.
    Functional programming is stateless.
    All data of Elixir is immutable.
    [x, 3, x] = Enum.map([1,2,1], &(&1 + 1))
    assert 2 == x
    // Many lines that don't reassign to `x`.
    assert 2 == x

    View Slide

  15. Elixir has processes.
    29 =
    Task.async(fn -> 29 end)
    |> Task.await

    View Slide

  16. Process has state.
    agent = Agent.start_link(fn -> 29 end)
    29 = Agent.get(agent, &(&1))
    Agent.update(agent, &(&1 + 13))
    42 = Agent.get(agent, &(&1))

    View Slide

  17. State should be updated in a transaction.
    State shold interact & isolate each other.
    Accessing to state should be scaled in-out.
    Server with state should be updated gracefully.
    Elixir has all of these!

    View Slide

  18. State should be updated in a transaction. (No shared
    state)
    State shold interact & isolate each other. (Application,
    Supervisor)
    Accessing to state should be scaled in-out. (GenStage,
    Task, Register, Erlang cluster)
    Server with state should be updated gracefully. (Hot
    code swap)

    View Slide

  19. We can create stateful system easy, by constructing
    stateless parts.

    View Slide

  20. How we can start Elixir?
    Let's create some applications.
    * Web application with Phoenix.
    * Slack bot.

    View Slide

  21. Phoenix is a Web application framework that familiar
    with WebSocket.
    https://hexdocs.pm/phoenix/overview.html

    View Slide

  22. React is available (✿
    >ヮ ╹
    )-♡
    Let's create a realtime interaction ∩
    (> ◡
    <*)∩

    https://speakerdeck.com/ne_sachirou/phoenix-livereact

    View Slide

  23. Create a Slack bot.
    https://github.com/BlakeWilliams/Elixir-Slack
    (Easy, so no topic.)

    View Slide

  24. Installing Elixir.
    asdf https://asdf-vm.com/
    asdf plugin-add erlang
    asdf install erlang 22.0.7
    asdf plugin-add elixir
    asdf install elixir 1.9.1

    View Slide

  25. Text editor?
    VSCode with mjmcloug.vscode-elixir
    works.

    View Slide

  26. Format & lint.
    InnerCotton https://github.com/ne-sachirou/inner_cotton is a set
    of standard development tools.
    mix cotton.init
    mix cotton.lint --fix

    View Slide

  27. Docker image (You need Docker :-)
    Fix major ver.s both Erlang & Elixir.
    docker run -it --rm nesachirou/elixir:1.9_erl22

    View Slide

  28. Textbook & document.
    Official guide : https://elixir-lang.org/
    Official document : https://hexdocs.pm/elixir/Kernel.html
    Online textbook : https://elixirschool.com/ja/
    Textbook : https://tatsu-zine.com/books/programming-elixir-ja

    View Slide

  29. https://speakerdeck.com/ne_sachirou/elixirwan-quan-nili-jie-sita

    View Slide

  30. Advanced resources

    View Slide

  31. Erlang in Anger
    https://ymotongpoo.github.io/erlang-in-anger/text-ja.pdf
    You want to read this… before production.

    View Slide

  32. https://speakerdeck.com/ne_sachirou/sutetohurudeda-gui-mo-
    akusesufalsearusoft-realtimenagemusabawoeasynitukuru

    View Slide

  33. https://speakerdeck.com/ne_sachirou/elixir-on-containers

    View Slide

  34. https://speakerdeck.com/ne_sachirou/phoenix-at-scale

    View Slide

  35. https://speakerdeck.com/ne_sachirou/ddd-data-driven-development

    View Slide