FlyMultiRegion

A library for using Fly.io's Multi-Region Databases with Ecto.

This library assumes that:

Warning

It's likely that Fly will implement their own version of this library. So this library should serve as a proof-of-concept. There are a number of assumptions and improvements that could be made. Please use this library at your own risk and know that I never intended it to be used in production.

This library was based on what I originally implemented in this blog post

Usage

Installation

The package can be installed by adding fly_multi_region to your list of dependencies in mix.exs:

def deps do
  [
    {:fly_multi_region, "~> 0.0.1"}
  ]
end

Configuration

If you're following the Fly.io guides then your database config will happen at runtime. Add the following to the runtime.exs file:

config :fly_multi_region,
  region: System.get_env("FLY_REGION"),
  regions: ["nrt", "ord"],
  url: System.get_env("DATABASE_URL"),
  opts: [
    socket_options: [:inet6],
    pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10")
  ]

Parameters:

Supervision

Add FlyMultiRegion to your Application's supervision tree

As mentioned in Ecto's documentation for replica databases it's necessary to add any Repos to your supervision tree. With that in mind, make sure to add FlyMultiRegion to your Application module (application.ex)

def start(_type, _args) do
    children =
      [
        ...,
        FlyMultiRegion
      ]

    ...
    Supervisor.start_link(children, opts)
  end

Repo

Add the following to your projects main Repo module:

  use FlyMultiRegion.Repo

Future Improvements