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

GenStage in the Kitchen

GenStage in the Kitchen

Slides for Elixir.LDN 2016 talk.

Abstract:

GenStage is the Elixir core team’s effort to provide a set of flexible, composable primitives for concurrent, demand-driven event processing.

Our use case is a restaurant simulation, with tables placing orders, a waiter, a chef and line cooks ready to prepare amazing dishes.
We’ll map GenStage’s core concepts to constraints in our restaurant simulation and see how our system copes by stressing its different components, isolating some useful principles along the way.

Claudio Ortolina

September 22, 2016
Tweet

More Decks by Claudio Ortolina

Other Decks in Programming

Transcript

  1. www.erlang-solutions.com TABLE 1 TABLE 2 TABLE 3 TABLE 4 WAITER

    CHEF LINE COOK (GRILL) LINE COOK (OVEN) ... ...
  2. www.erlang-solutions.com TABLES ▸ People take some time to choose a

    dish from the menu ▸ When everyone has decided, they call the waiter; they get upset and leave if the waiter doesn't reply in 5 seconds ▸ After ordering, they wait until 30 seconds for the first dish to be delivered. When they receive it, they can wait another 30 seconds. ▸ If dishes aren't delivered on time, they will leave.
  3. www.erlang-solutions.com WAITER ▸ Collects orders from tables ▸ Delivers orders

    to the chef ▸ When dishes are ready, delivers them to the appropriate table
  4. www.erlang-solutions.com LINE COOK ▸ Receives a dish request ▸ Prepares

    dish and informs the waiter so that it can be picked up
  5. www.erlang-solutions.com TABLE 1 TABLE 2 TABLE 3 TABLE 4 WAITER

    CHEF LINE COOK (MEAT) LINE COOK (OVEN) ... ... PLACE ORDERS DEMANDS ORDERS DEMAND DISHES
  6. www.erlang-solutions.com TABLE WAITER CHEF LINE COOK LINE COOK TABLE Icons

    Designed by Freepik and distributed by Flaticon GenStage
  7. www.erlang-solutions.com TABLE WAITER CHEF LINE COOK LINE COOK TABLE Icons

    Designed by Freepik and distributed by Flaticon PRODUCER BROADCAST DISPATCHER GenStage
  8. www.erlang-solutions.com TABLE WAITER CHEF LINE COOK LINE COOK TABLE Icons

    Designed by Freepik and distributed by Flaticon PRODUCER/CONSUMER PARTITION DISPATCHER GenStage
  9. www.erlang-solutions.com TABLE WAITER CHEF LINE COOK LINE COOK TABLE Icons

    Designed by Freepik and distributed by Flaticon CONSUMER PARTITION CONSUMER PARTITION GenStage
  10. www.erlang-solutions.com TABLES - LOOK AT THE MENU defmodule Osteria.Table do

    use GenServer def init([number, size]) do … send(self(), :look_at_menu) … end def handle_info(:look_at_menu, state) do schedule_decide() {:noreply, state} end end
  11. www.erlang-solutions.com TABLES - DECIDING DISHES defp schedule_decide do time =

    Enum.random(50..500) Process.send_after(self(), :decide, time) end def handle_info(:decide, state = %__MODULE__{number: number, size: size, dishes: dishes}) do case length(dishes) do ^size -> inform_waiter(number, dishes) {:noreply, %{state | phase: :waiting}, waiting_time()} _ -> schedule_decide() {:noreply, %{state | dishes: [random_dish() | dishes]}} end end
  12. www.erlang-solutions.com DEMAND IS DRIVEN BY LINE COOKS The faster they

    cook, the faster food can be served at the table