We improved dashboard performance using fly-replay

Last Thursday, we started using fly-replay to improve performance of our dashboard.

Here are the ~20 lines we added (to endpoint.ex) to achieve this:

@replay_prefixes ~w(/phx/live /phx/socket /dashboard ...)

def call(conn, opts) do
  case replay_to_primary_region(conn, @replay_prefixes) do
    %Plug.Conn{halted: false} = conn ->
      super(conn, opts)
    %Plug.Conn{halted: true} = halted_conn -> halted_conn
  end
end

defp replay_to_primary_region(conn, prefixes) do
  region = System.get_env("FLY_REGION")
  primary? = region in ~w(iad ewr)

  if region && !primary? && Enum.find(prefixes, &String.starts_with?(conn.request_path, &1)) do
    conn
    |> put_resp_header("fly-replay", "region=iad")
    |> send_resp(200, "replaying to iad")
    |> halt()
  else
    conn
  end
end

fly-replay is really cool - it lets us send very specific parts of our app to a different region, leaving the rest served by the closest region. In 20 lines of code!!

We don’t have hard numbers, but Fly.io employees are globally distributed and we’ve all noticed the dashboard is significantly faster after deploying this change :confetti_ball:

9 Likes