diff --git a/self-host/advanced/database-options.mdx b/self-host/advanced/database-options.mdx index 0a44d53..f18ebb0 100644 --- a/self-host/advanced/database-options.mdx +++ b/self-host/advanced/database-options.mdx @@ -97,3 +97,47 @@ This example is not necessarily production-ready. Adjust the configuration accor Do not use `latest` tags in production. Use specific version tags for stability. +### Read Replicas + +Pangolin can distribute read queries across one or more PostgreSQL read replicas while always sending writes to the primary database. This is useful for scaling read-heavy workloads. + + +Replicas are chosen at random for each read query (not round-robin). Writes (`insert`, `update`, `delete`) always go to the primary database. A small number of time-sensitive reads (where the app must see its own recent writes) are also routed directly to the primary database regardless of replicas being configured. + + +#### Using the Configuration File + +Add a `replicas` array under `postgres` in your `config.yml`, with one entry per replica: + +```yaml title="config.yml" +postgres: + connection_string: postgresql://:@:/ + replicas: + - connection_string: postgresql://:@:/ + - connection_string: postgresql://:@:/ +``` + +#### Using Environment Variables + +You can instead provide replica connection strings with the `POSTGRES_REPLICA_CONNECTION_STRINGS` environment variable, as a comma-separated list. This must be used together with `POSTGRES_CONNECTION_STRING` for the primary database — the two env vars replace the entire `postgres.connection_string` / `postgres.replicas` config as a unit. + +```bash title=".env" +POSTGRES_CONNECTION_STRING=postgresql://:@:/ +POSTGRES_REPLICA_CONNECTION_STRINGS=postgresql://:@:/,postgresql://:@:/ +``` + +```yaml title="docker-compose.yml" +services: + pangolin: + image: fosrl/pangolin:postgresql-latest # Don't use latest in production + environment: + POSTGRES_CONNECTION_STRING: postgresql://:@:/ + POSTGRES_REPLICA_CONNECTION_STRINGS: "postgresql://:@:/,postgresql://:@:/" +``` + + +The same pattern applies to the optional dedicated logs database: `postgres_logs.replicas` in the config file, or the `POSTGRES_LOGS_REPLICA_CONNECTION_STRINGS` environment variable (comma-separated) alongside `POSTGRES_LOGS_CONNECTION_STRING`. + + +See the [`postgres.replicas` reference](/self-host/advanced/config-file#database-configuration) for the full config schema, and the [Environment Variables reference](/self-host/advanced/config-file#environment-variables) for all supported variables. +