ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - azranel/ecto_facade: Separate your read and write operations in replicated database setup
Separate your read and write operations in replicated database setup - azranel/ecto_facade
Visit Site

GitHub - azranel/ecto_facade: Separate your read and write operations in replicated database setup

GitHub - azranel/ecto_facade: Separate your read and write operations in replicated database setup

EctoFacade

EctoFacade can be used as interface through which you can query multiple repositories. Main objective of this is to use separate separate repository for writes and separate repository for reads.

For example:

Lets say you have PostgreSQL database replicated to 4 different slave nodes that should be used for reads only. With this small library you can do that!

Installation

def deps do
  [
    {:ecto_facade, "~> 0.2.0"}
  ]
end

Usage

Here is example on how to use EctoFacade in your application:

defmodule MyApp.FacadeRepo do
  use EctoFacade.Repo,
    master_repo: MyApp.Repo,
    read_repos: [MyApp.ReadReplicaOne, MyApp.ReadReplicaTwo],
    algorithm: MyApp.Repo.ReadAlgorithm,
    fallback_to_master: false
end

MyApp.FacadeRepo.all(MyApp.SomeSchema) # and other operations that you would normally do with ecto repo

Options you can pass when using EctoFacade.Repo are:

  • master_repo - this is master repository that will be used for all write operations
  • read_repos - this is list of read repositories that will be used for all read operations. Which repository will be used depends on algorithm.
  • algorithm - this is module that implements behaviour EctoFacade.Algorithm and picks read repository for next read operation.
  • fallback_to_master - if set to true then read operation that won't work on read repository, will fallback to master repostiory. If set to false, it will throw exception.

Possible algorithms for selecting repository for read operation:

  • EctoFacade.Algorithms.Random - selects randomly repository
  • EctoFacade.Algorithms.Roundrobin - selects next repository in list till last and then goes again from beginning
  • EctoFacade.Algorithms.WeightRoundrobin - selects next repository in list depending on weight. Check module documentation for more info.

or your own that implement behaviour EctoFacade.Algorithm.

for more information please check documentation

How to write my own algorithm?

This is pretty straightforward. Here is implementation of EctoFacade.Algorithms.Random:

defmodule EctoFacade.Algorithms.Random do
  @moduledoc """
  Default algorithm that just returns random read repository
  """
  @behaviour EctoFacade.Algorithm

  def get_repo(repos) do
    Enum.random(repos)
  end
end

Usage (testing)

When in test just configure you FacadeRepo to use only master repository. This way you won't have issues with separate repositories with Sandbox Ecto adapter.

Documentation

Hex.pm

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory