2020-08-25 14:53:25 +03:00
.. meta ::
:description: Hasura Cloud read replicas
:keywords: hasura, docs, cloud, read replicas, connections, pool
.. _read_replicas:
Read replicas
=============
.. contents :: Table of contents
:backlinks: none
2022-01-04 14:53:50 +03:00
:depth: 2
2020-08-25 14:53:25 +03:00
:local:
Introduction
------------
2022-01-31 16:31:16 +03:00
Hasura Cloud can load balance queries and subscriptions across *read replicas* while sending all mutations and metadata API calls to the primary.
2020-08-25 14:53:25 +03:00
Adding read replica urls
------------------------
2022-01-04 14:53:50 +03:00
Postgres
^^^^^^^^
2021-03-24 19:37:27 +03:00
.. rst-class :: api_tabs
.. tabs ::
2020-08-25 14:53:25 +03:00
2021-03-24 19:37:27 +03:00
.. tab :: Console
2020-08-25 14:53:25 +03:00
2022-01-04 14:53:50 +03:00
Head to `` Data -> Manage -> <db-name> -> Edit ``
2021-04-06 20:04:53 +03:00
.. thumbnail :: /img/graphql/cloud/read-replicas/connect-db-with-replica.png
:alt: Connect database with read replica
:width: 1000px
2020-08-25 14:53:25 +03:00
2021-03-24 19:37:27 +03:00
.. tab :: CLI
2020-08-25 14:53:25 +03:00
2022-01-31 16:31:16 +03:00
You can add *read replicas* for a database by adding their config to the `` /metadata/databases/database.yaml `` file:
2020-08-25 14:53:25 +03:00
2021-03-24 19:37:27 +03:00
.. code-block :: yaml
:emphasize-lines: 11-17
2020-08-25 14:53:25 +03:00
2021-03-24 19:37:27 +03:00
- name: <db-name>
kind: postgres
configuration:
connection_info:
database_url:
from_env: <DATABASE_URL_ENV>
pool_settings:
idle_timeout: 180
max_connections: 50
retries: 1
read_replicas:
- database_url:
from_env: <DATABASE_REPLICA_URL_ENV>
pool_settings:
idle_timeout: 180
max_connections: 50
retries: 1
2020-08-25 14:53:25 +03:00
2021-03-24 19:37:27 +03:00
Apply the metadata by running:
2020-08-25 14:53:25 +03:00
2021-03-24 19:37:27 +03:00
.. code-block :: yaml
2020-08-25 14:53:25 +03:00
2021-03-24 19:37:27 +03:00
hasura metadata apply
.. tab :: API
2022-01-31 16:31:16 +03:00
You can add *read replicas* for a database using the :ref: `metadata_pg_add_source` metadata API.
2021-03-24 19:37:27 +03:00
.. code-block :: http
2022-01-04 14:53:50 +03:00
:emphasize-lines: 16-27
2021-03-24 19:37:27 +03:00
POST /v1/metadata HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin
{
2022-01-31 16:31:16 +03:00
"type":"pg_add_source",
"args":{
"name":"<db_name>",
"replace_configuration":true,
"configuration":{
"connection_info":{
"database_url":{
"from_env":"<DATABASE_URL_ENV>"
2021-03-24 19:37:27 +03:00
}
},
2022-01-31 16:31:16 +03:00
"read_replicas":[
{
"database_url":{
"from_env":"<DATABASE_REPLICA_URL_ENV>"
},
"pool_settings":{
"retries":1,
"idle_timeout":180,
"max_connections":50
}
2021-03-24 19:37:27 +03:00
}
2022-01-31 16:31:16 +03:00
]
}
2021-03-24 19:37:27 +03:00
}
2021-04-08 14:20:44 +03:00
}
2021-03-24 19:37:27 +03:00
.. admonition :: For existing v1.3 projects
2022-01-31 16:31:16 +03:00
If you have configured your Postgres instances with replicas; then the replica URLs can be added to Hasura using the following environment variable in your project ENV Vars tab:
2021-03-24 19:37:27 +03:00
.. code-block :: bash
HASURA_GRAPHQL_READ_REPLICA_URLS=postgres://user:password@replica-host:5432/db
2022-01-31 16:31:16 +03:00
In the case of multiple replicas, you can add the URLs of each replica as comma-separated values.
2021-03-24 19:37:27 +03:00
2022-01-31 16:31:16 +03:00
Additional environment variables for *read replicas* specifically:
2021-03-24 19:37:27 +03:00
`` HASURA_GRAPHQL_CONNECTIONS_PER_READ_REPLICA ``
`` HASURA_GRAPHQL_STRIPES_PER_READ_REPLICA ``
2022-01-04 14:53:50 +03:00
MS SQL Server
^^^^^^^^^^^^^
.. rst-class :: api_tabs
.. tabs ::
.. tab :: Console
Support will be added soon
.. tab :: CLI
2022-01-31 16:31:16 +03:00
You can add *read replicas* for a database by adding their config to the `` /metadata/databases/database.yaml `` file:
2022-01-04 14:53:50 +03:00
.. code-block :: yaml
2022-01-31 16:31:16 +03:00
:emphasize-lines: 10-15
2022-01-04 14:53:50 +03:00
- name: <db-name>
kind: mssql
configuration:
connection_info:
2022-01-31 16:31:16 +03:00
connection_string:
from_env: <DATABASE_URL_ENV>
2022-01-04 14:53:50 +03:00
pool_settings:
idle_timeout: 180
max_connections: 50
read_replicas:
2022-01-31 16:31:16 +03:00
- connection_string:
from_env: <DATABASE_REPLICA_URL_ENV>
2022-01-04 14:53:50 +03:00
pool_settings:
idle_timeout: 25,
max_connections: 100
Apply the metadata by running:
.. code-block :: yaml
hasura metadata apply
.. tab :: API
2022-01-31 16:31:16 +03:00
You can add *read replicas* for a database using the :ref: `mssql_add_source` metadata API.
2022-01-04 14:53:50 +03:00
.. code-block :: http
2022-01-31 16:31:16 +03:00
:emphasize-lines: 19-29
2022-01-04 14:53:50 +03:00
POST /v1/metadata HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin
{
"type":"mssql_add_source",
"args":{
2022-01-31 16:31:16 +03:00
"name":"<db_name>",
"replace_configuration":true,
"configuration":{
"connection_info":{
"connection_string":{
"from_env":"<DATABASE_URL_ENV>"
},
"pool_settings":{
"max_connections":50,
"idle_timeout":180
},
"read_replicas":[
{
2022-01-04 14:53:50 +03:00
"connection_string":{
2022-01-31 16:31:16 +03:00
"from_env":"<DATABASE_REPLICA_URL_ENV>"
2022-01-04 14:53:50 +03:00
},
"pool_settings":{
2022-01-31 16:31:16 +03:00
"idle_timeout":180,
"max_connections":50
}
}
]
2022-01-04 14:53:50 +03:00
}
2022-01-31 16:31:16 +03:00
}
2022-01-04 14:53:50 +03:00
}
}