Skip to content

Add docs for read replicas #511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Welcome! `AshPostgres` is the PostgreSQL data layer for [Ash Framework](https://
- [Expressions](documentation/topics/advanced/expressions.md)
- [Manual Relationships](documentation/topics/advanced/manual-relationships.md)
- [Schema Based Multitenancy](documentation/topics/advanced/schema-based-multitenancy.md)
- [Read Replicas](documentation/topics/advanced/using-multiple-repos.md)

## Reference

Expand Down
56 changes: 56 additions & 0 deletions documentation/topics/advanced/using-multiple-repos.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Using Multiple Repos

When scaling PostgreSQL you may want to setup _read_ replicas to improve
performance and availability. This can be achieved by configuring multiple
repositories in your application.

## Setup Read Replicas

Following the [ecto docs](https://hexdocs.pm/ecto/replicas-and-dynamic-repositories.html), change your Repo configuration:

```elixir
defmodule MyApp.Repo do
use Ecto.Repo,
otp_app: :my_app,
adapter: Ecto.Adapters.Postgres

@replicas [
MyApp.Repo.Replica1,
MyApp.Repo.Replica2,
MyApp.Repo.Replica3,
MyApp.Repo.Replica4
]

def replica do
Enum.random(@replicas)
end

for repo <- @replicas do
defmodule repo do
use Ecto.Repo,
otp_app: :my_app,
adapter: Ecto.Adapters.Postgres,
read_only: true
end
end
end
```

## Configure AshPostgres

Now change the `repo` argument for your `postgres` block as such:

```elixir
defmodule MyApp.MyDomain.MyResource do
use Ash.Resource,
date_layer: AshPostgres.DataLayer

postgres do
table "my_resources"
repo fn
_resource, :read -> MyApp.Repo.replica()
_resource, :mutate -> MyApp.Repo
end
end
end
```
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ defmodule AshPostgres.MixProject do
"documentation/topics/development/upgrading-to-2.0.md",
"documentation/topics/advanced/expressions.md",
"documentation/topics/advanced/schema-based-multitenancy.md",
"documentation/topics/advanced/using-multiple-repos.md",
"documentation/topics/advanced/manual-relationships.md",
{"documentation/dsls/DSL-AshPostgres.DataLayer.md",
search_data: Spark.Docs.search_data_for(AshPostgres.DataLayer)},
Expand Down