Horizontal Scaling

Hasura Cloud lets you scale your applications automatically without having to think about the number of instances, cores, memory, thresholds, etc. You can keep increasing your number of concurrent users and the number of API calls and Hasura Cloud will figure out the optimizations automagically. But you could have a bottleneck at the database level. In that case, you would need to scale the database.

Horizontal Scaling of Postgres

Hasura Cloud can load balance queries and subscriptions across read replicas while sending all mutations and metadata API calls to the master. To perform horizontal scaling:

  • Create read-replicas of your Postgres instances
  • Configure routing, connection pooling, and load balancing

In our example, we deployed a Postgres database on Heroku. We can add a follower database (read-only) to Heroku PostgreSQL by following the documentation.

To add read replicas to your managed database providers, check the following:

Adding read replica URLs

Once you configure read replicas for your Postgres instance, the load balancing at the database layer is automatically taken care of. At the Hasura API layer, load balancing is done seamlessly with multiple instances of your Hasura app running in different regions across the globe.

After configuring Postgres with the read replica, 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

With Heroku, you can obtain the above URL by running the following command in the terminal:

heroku pg:info

This would output DATABASE_URL, HEROKU_POSTGRESQL_PURPLE_URL info. The second one with the format HEROKU_POSTGRESQL_COLOR_URL gives the read replica info.

Make sure to replace the database credentials appropriately.

Hasura Cloud handles automatic routing for queries, subscriptions, and mutations across master and read replicas.

You can use the same database for multiple Hasura instances. Do note that this is taken care of in Hasura Cloud automatically.

Did you find this page helpful?
Start with GraphQL on Hasura for Free
  • ArrowBuild apps and APIs 10x faster
  • ArrowBuilt-in authorization and caching
  • Arrow8x more performant than hand-rolled APIs
Promo
footer illustration
Brand logo
© 2024 Hasura Inc. All rights reserved
Github
Titter
Discord
Facebook
Instagram
Youtube
Linkedin