Search…
Database
We use Sequelize as an ORM for our database.

Setup

To set up Sequelize in your backend service take a look at reference-backend.

Read replicas

You can enable an application to use read replicas to reduce the load of the database. A read replica is an exact matching replica of your current database that is kept in sync with the original one. This replica is read only and thus called a read replica while the original database allows writes and is called a writer. There can only be one writer but there can be many read replicas.
We can configure Sequelize to use read replicas, it handles the logic of sending all the read commands to the read replicas instead of the writer.
All we need to do is add the connection config for the readers. AWS provides us with a single host name for all of the read replicas. AWS directs the connecting server to an available read replica.
Edit your sequelize.config.js and setup the replication object like this:
1
{
2
production: {
3
replication: {
4
read: [
5
{
6
host: process.env.DB_REPLICAS_HOST,
7
username: process.env.DB_USER,
8
password: process.env.DB_PASS,
9
database: process.env.DB_NAME,
10
},
11
],
12
write: {
13
host: process.env.DB_HOST,
14
username: process.env.DB_USER,
15
password: process.env.DB_PASS,
16
database: process.env.DB_NAME,
17
},
18
},
19
}
20
}
Copied!
By default Sequelize creates a connection with each of the hosts in the config file and keeps the connection alive as long as possible. That's usually fine but because of autoscaling this usually results in a very uneven distribution of connections between the readers.
To handle that we can recycle the connections periodically. Change sequelizeConfig.service.ts and pass in the recycleConnections property like this:
1
getOptions({ recycleConnections: true }),
Copied!
Copy link