Multiple Databases with Ecto and Phoenix
At work we have one db master and some slaves. It would alleviate the load of the master database if we could send some (or all!) of the reads to the slave(s) and the writes to the master.
Seeing that Ecto uses the repository pattern it seemed to me that adding another database connection to my phoenix project was a trivial thing.
That was my guess, so I asked in #elixir if adding another DB connection with Ecto was as trivial as it looked, and this is the response I got:
<jeregrine> edmz: it is exactly that trivial
<jeregrine> part of what makes ecto great is the query generation
and the repo are decoupled
So I decided to give it a try. I created a new phoenix project:
mix phoenix.new dual_ecto --database mysql
I added the credentials to dev.exs for the other database:
Now, notice that the second set of credentials refers to a repo called RepoReadOnly. We need to create that module and also ensure that it is started.
First, add the module to the supervision tree of your main module, which in my case is called DualEcto:
And then create the actual module, which looks like this:
Now, it is only a matter of using it in your model (or ‘schema’, as they are called in Ecto 2.0).
I created a very naive model that just does simple selects, but there is nothing stopping you for using one module when doing updates and another one when doing selects.