Octo

Hex.pmAPI DocsBuild Status

Octo uses the facade pattern to distribute reads and writes to different ecto repos.

Installation

If available in Hex, the package can be installed by adding octo to your list of dependencies in mix.exs:

def deps do
  [
    {:octo, "~> 0.1.1"}
  ]
end

Usage

A simple setup might look something like this:

# my_app/write_repo.ex
defmodule MyApp.WriteRepo do
  use Ecto.Repo, otp_app: :my_app
end

# my_app/read_one_repo.ex
defmodule MyApp.ReadOneRepo do
  use Ecto.Repo, otp_app: :my_app
end

# my_app/read_two_repo.ex
defmodule MyApp.ReadTwoRepo do
  use Ecto.Repo, otp_app: :my_app
end

# my_app/repo.ex
defmodule MyApp.Repo do
  use Octo.Repo,
    master_repo: MyApp.WriteRepo,
    replica_repos: [MyApp.ReadOneRepo, MyApp.ReadTwoRepo],
    algorithm: Octo.Algorithms.RoundRobin
end

Custom Algorithms

By default, the replica repo to use is chosen randomly, but you can define your own selection algorithms by implementing the Octo.Algorithm behaviour.

defmodule MyApp.MyAlgorithm do
  @behaviour Octo.Algorithm

  def get_repo(replica_repos) do
    # Your code here...
  end
end

Configuration is simple:

defmodule MyApp.Repo do
  use Octo.Repo,
    master_repo: MyApp.WriteRepo,
    replica_repos: [MyApp.ReadOneRepo, MyApp.ReadTwoRepo],
    algorithm: MyApp.MyAlgorithm