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:
- Amazon RDS Postgres Read Replica
- Google Cloud SQL Read Replica
- Azure Postgres Read Replica
- DigitalOcean Postgres Read Replica
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:
With Heroku, you can obtain the above URL by running the following command in the terminal:
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.
- Build apps and APIs 10x faster
- Built-in authorization and caching
- 8x more performant than hand-rolled APIs