# FlyMultiRegion
A library for using Fly.io's Multi-Region Databases with Ecto.
This library assumes that:
- You have an app hosted on Fly.io
- You've already created and configured read-replica databases on Fly. Refer to [Fly.io's documentation](https://fly.io/docs/getting-started/multi-region-databases/) for more information on how to do 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](https://nathanwillson.com/blog/posts/2021-09-25-fly-multi-db/)
## Usage
### Installation
The package can be installed by adding `fly_multi_region` to your list of dependencies in `mix.exs`:
```elixir
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:
```elixir
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:
- region: generated by Fly.io by default
- regions: a list of replicas in your cluster
- url: database url
- opts: configuration options for Ecto
### Supervision
Add FlyMultiRegion to your Application's supervision tree
As mentioned in [Ecto's documentation for replica databases](https://hexdocs.pm/ecto/replicas-and-dynamic-repositories.html) 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)
```elixir
def start(_type, _args) do
children =
[
...,
FlyMultiRegion
]
...
Supervisor.start_link(children, opts)
end
```
### Repo
Add the following to your projects main Repo module:
```elixir
use FlyMultiRegion.Repo
```
## Future Improvements
- tests
- make replica's more configurable (for example, the assume Postgres at the moment)
- remove Ecto as a dependancy (it's only used to parse a database url)