Connect to a read replica database

⚠️ This tutorial is for SQL databases only.

Please be sure of your agent type and version and pick the right documentation accordingly.

This is the documentation of the forest-express-sequelize and forest-express-mongoose Node.js agents that will soon reach end-of-support.

forest-express-sequelize v9 and forest-express-mongoose v9 are replaced by @forestadmin/agent v1.

Please check your agent type and version and read on or switch to the right documentation.

Connect to a read replica database

A read replica is a copy of the master that reflects changes to the master instance in almost real time. For performance reasons, you can specify one or more servers to act as read replicas, and one server to act as the write master, which handles all writes and updates and propagates them to the replicas. For example, your read replica will be used while displaying the table view of your records or accessing your Forest Admin dashboard.

As your Admin Backend relies on the Sequelize ORM, it's quite easy to configure a read replication.

Those code snippets are an example. It is strongly advised to use environment variables for your database connection credentials.

config/databases.js
const path = require('path');

let databaseOptions = {
  logging: process.env.NODE_ENV === 'development' ? console.log : false,
  dialect: 'postgresql',
  port: 5435,
  replication: {
    read: [
      {
        host: 'ec2-52-219-116-175.us-west-1.compute.amazonaws.com',
        username: 'userRead',
        password: 'passwordUserRead',
        database: 'databaseReplicate',
      },
    ],
    write: {
      host: 'ec2-52-219-125-185.eu-west-1.compute.amazonaws.com',
      username: 'userWrite',
      password: 'passwordUserWrite',
      database: 'databaseMaster',
    },
  },
  pool: {
    maxConnections: 10,
    minConnections: 1,
  },
  dialectOptions: {},
};

module.exports = [
  {
    name: 'default',
    modelsDir: path.resolve(__dirname, '../models'),
    connection: {
      options: { ...databaseOptions },
    },
  },
];
models/index.js
...
databasesConfiguration.forEach((databaseInfo) => {
  let connection;
  if(databaseInfo.connection.options.replication) {
    connection = new Sequelize(databaseInfo.connection.options);
  } else {
    connection = new Sequelize(databaseInfo.connection.url, databaseInfo.connection.options);
  }

  connections[databaseInfo.name] = connection;

  ...
});
...

Last updated