AWS

How to configure Read Replicas for AWS RDS cluster

AWS RDS (relational database service) is the managed SQL database service that provides multiple engines for SQL databases like MySQL, Microsoft SQL Server, MariaDB, PostgreSQL, etc. The read replica of a database server is configured to distribute intensive workload across multiple database instances. Configuring the read replica creates a new database instance that accepts read-only queries, and hence the workload is distributed. Changes made to the primary database instance are asynchronously replicated to the read replica from the primary database instance.

A read replica of an RDS instance is created from a source database and can not be created independently. When you create a read replica, AWS takes a snapshot of the existing source database and creates a new database instance from that snapshot which receives read-only queries. The read replica has its own endpoint, which receives read-only queries, and the source database has its own endpoint, which receives both read and write queries.

AWS RDS read replica can not be used for disaster recovery even if it has a complete copy of the source database. In case, source database crashes, the read replica does not become the source database, and you have to configure the read replica to become the source database manually.

Prerequisites

In order to go along with this blog, you must have a MySQL RDS cluster already deployed on AWS. This blog will discuss only the creation of the read replica of an RDS instance. Read the following blog to create an RDS cluster on AWS.

https://linuxhint.com/create-rds-cluster-aws/

Creating read replica

First, log into the AWS management console and go to the RDS service.

Go to the databases to list all the deployed databases.

Before creating a read replica of an RDS instance, enable the automated backups on your RDS instance. AWS does not support creating read replicas without enabling the backups.

To enable the automated backups, select the RDS instance and click on the Modify button.

It will open the database configuration page to edit the configuration. Scroll down to the Backup field and set the Backup retention period of your choice.

While saving the configuration, it will ask for the time window when the changes will be applied. Select the Apply immediately option and save the configuration.

It will immediately start applying the changes, and after the changes have been applied, you can configure read replicas for the RDS instance.

In order to configure read replicas for the RDS instance, select the instance from the RDS console and click on the Actions button. From the dropdown list, click on the create read replica option.

It will open a new configuration page for the RDS read replica instance. From the Instance specifications, enter the required details.

The DB instance class is the RDS read replica database instance class. You can also configure Multi-AZ deployment for the read replica. Enabling the Multi-AZ deployment for the read replica will create a standby instance for the read replica. In case of reading replica failure, the standby instance will act as a read replica without bearing any downtime. The Storage type is the type of block storage that will be used by the read replica instance.

Now configure the network and security for the RDS instance read replica. The destination region is the AWS region where the read replica instance will be created. You can create the RDS instance and its read replica in different AWS regions. The destination subnet group is the group of subnets in the region where the RDS read replica will be deployed. The availability zone field defines the availability zone in which the RDS instance read replica will be created. You can set the No preference to automatically use any of the availability zones for the read replica.

Enable the publicly accessible option if you want to access the read replica from outside the VPC. If you want to access the read replica within the VPC in which the read replica is created, you can set the publicly accessible option to no. The security group will control the inbound and outbound traffic on the read replica instance.

The encryption option is used to enable or disable the encryption for the data stored in the read replica. For this demo, disable the encryption, for instance. Select the RDS instance you want to create the read replica for and set the unique identifier for the read replica instance.

You can configure monitoring, logging, maintenance, and deletion protection for additional configuration. Enhanced monitoring enables you to see the in-depth metrics of the database server. Logging includes different types of logs that are exported to the cloud watch logs. You can configure audit logs, error logs, general logs, and slow query logs. These logs then can be used for debugging purposes.

The Maintenance option allows AWS RDS read replica to automatically upgrade the new minor version of the database server. The Deletion protection option is used to prevent the database read replica from accidental deletion. You can not delete the RDS read replica instance when deletion protection is enabled.

After configuring the RDS read replica, now click on the create read replica button to create a new read replica instance of the primary database. The newly created read replica instance has the replica role assigned.

The newly created read replica will have its own endpoint, and you need to configure the endpoint of the read replica in your application. In case the primary database crashes, the read replica will not be promoted to the primary database. You need to promote the read replica as the primary database manually.

Limitations of read replica

Following are some limitations of read replica on AWS RDS on VMware.

  • Only MySQL and PostgreSQL support read replica
  • Read replicas are supported on MySQL version 5.7 and PostgreSQL version 10.9 and 10.10
  • Read replicas from source database instance can not be created in another AWS region
  • You can create only one read replica per database instance
  • Read replica will have the same resources (RAM, CPU, Storage) as that of the source database instance
  • Read replica can not be used for disaster recovery; it is used for improved performance

Conclusion

AWS RDS read replicas are used to improve the performance of the database server by distributing the workload on read replicas. Read replicas are independent database instances created from the snapshots of the source database and accept read-only queries. Changes made to the source database are asynchronously replicated to the read replica. This blog describes the detailed procedure to create a read replica instance from a primary AWS RDS instance.

About the author

Zain Abideen

A DevOps Engineer with expertise in provisioning and managing servers on AWS and Software delivery lifecycle (SDLC) automation. I'm from Gujranwala, Pakistan and currently working as a DevOps engineer.