Read replicas

Introduction

Hasura Cloud can load balance queries and subscriptions across read replicas while sending all mutations and metadata API calls to the primary.

Adding read replica urls

Currently it is only possible to add read replicas for a database at the time of connecting.

Head to Data -> Manage -> Connect database

Connect database with read replica

You can add read replicas for a database by adding their config to the /metadata/databases/database.yaml file:

- 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

Apply the metadata by running:

hasura metadata apply

Currently it is only possible to add read replicas for a database at the time of connecting using the pg_add_source metadata API

POST /v1/metadata HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin

{
  "type": "pg_add_source",
  "args": {
    "name": "<db_name>",
    "configuration": {
      "connection_info": {
        "database_url": {
          "from_env": "<DATABASE_URL_ENV>"
        }
      },
    "read_replicas": [
      {
        "database_url": {
          "from_env": "<DATABASE_REPLICA_URL_ENV>"
        },
        "pool_settings": {
          "retries": 1,
          "idle_timeout": 180,
          "max_connections": 50
        }
      }
    ]
  }
}

For existing v1.3 projects

If you have configured your Postgres instances with replicas, the replica URLs can be added to Hasura using the following environment variable in your project ENV Vars tab:

HASURA_GRAPHQL_READ_REPLICA_URLS=postgres://user:password@replica-host:5432/db

If you have multiple replicas, their urls can be added as comma separated values.

Additional environment variables for read replicas specifically:

HASURA_GRAPHQL_CONNECTIONS_PER_READ_REPLICA

HASURA_GRAPHQL_STRIPES_PER_READ_REPLICA