Read Replicas( RDS )
Overview
Amazon RDS allows us to create replicas of database instances known as RDS Read Replicas. These read replicas support only read operations and can be created in one or more AWS regions which can be the same or different from the AWS region of the primary database instance, depending on need. RDS Read Replicas in AWS can be created for various database engines like PostgreSQL, MySQL, MariaDB etc.
Introduction to Amazon RDS Read Replicas
Amazon RDS Read Replicas is a feature of Amazon RDS that helps to replicate the primary Database within the same or different AWS regions. This replication process is asynchronously from the primary database to the RDS Read Replicas, i.e., when the primary database gets affected for any reason, RDS Read Replicas acts as the primary database. The diagram below explains the working of RDS Read Replicas.
The main aim of the Amazon RDS Read Replicas is to give users fault-tolerant access to data, even after any failure or disruption in the primary database. Fault-Tolerant, Load Balancing, Scalability, and Multiple Source Support are the benefits of using RDS Read Replicas.
RDS Read Replica in AWS creates a read-only version of the primary database. Amazon RDS uses PostgreSQL, MySQL, Aurora, and MariaDB engines to create RDS Read Replicas from a source DB instance.
Creating an RDS Read Replica
Let's look at the steps to create an RDS Read Replica. Before creating a replica, first, we have to build a primary/source DB instance.
Create a MySQL DB Instance
Follow these steps to create a MySQL DB instance.
-
Sign in to the AWS Management Console, search for the RDS service, and click on the service that appears on the list.
-
In the Amazon RDS, scroll down the page and click the Create Database button.
-
We are redirected to the Create Database page. Here we need to fill in the details required for creating the database. For the creation method, we select the Standard Create option, and from the list of database engines, we will choose MySQL as we are creating a MySQL Database instance.
-
Next, Choose the Free tier template from the template section.
-
Scroll down and set the name of the DB instance identifier. Give the administrator name and password to enjoy the admin privileges for the DB instances.
-
In the storage configuration, allocate 20 GiB and set the maximum threshold frequency to 22 GiB.
-
Leave all the other configurations as default and click on Create database.
-
It will take a few minutes to create the database, and the status of the database will change from Creating to Available.
Create an RDS Read Replica
- Select the created database and click on Actions. Choose Create read replica from the Action menu and click it.
- It redirected to the Create read replica DB instance page.
- We can set the region and Availability zone. Here, we create the replica in the same region.
- Choose the Read replica source and set the name of the DB instance identifier.
- Leave all the other configurations as default and click on Create read replica.
- It will take a few minutes to create the replica, and its status changes from Creating to Available.
We successfully created a read replica of the DB instance.
Creating RDS Read Replica in a Different AWS Region
In the previous section, we created a read replica of a DB instance in the same region. Let's create a cross-region read replica.
- Select the primary DB instance and click on Actions. Click on Create read replica from the Actions menu.
- It redirected to the Create read replica DB instance page.
- In the Network & Security, choose a different AWS region and a DB subnet group.
- Choose the Read replica source and set the name of the DB instance identifier.
- Leave all the other configurations as default and click on Create read replica.
- It will initiate the creation of a replica in the different regions. Click on Close.
- Click on the region name and open the other region in a new tab.
- We will see a replica is creating the other region.
- Go back to the previous tab. Here, we saw two connections are established from the primary database, one from the replica in the same region and the other from the replica in a different region.
- After a few minutes, the cross-region replica of the database is created and in Available status.
We successfully created a Read Replica in a different AWS Region.
Read Replicas Monitoring
AWS provides the feature to monitor the status of RDS Read Replica in various ways. Follow the given steps to use the replica monitoring feature.
- Go to the Replication section of the Connectivity & Security tab in the read replica details.
This status of the Replication state can be:
- Replicating
- Replication degraded
- Error
- Terminated
- Stopped
- Replication stop point set
- Replication stop point reached
- Click on the Monitoring tab to see the Graphical representation of the usage provided by Amazon Cloudwatch.
Read Replica Deletion & DB Failover
Read Replica Deletion
We can delete an RDS Read Replica using the same mechanism to delete a DB instance.
Let's delete the created RDS Read Replica.
- Select the RDS Read Replica instance and click the Actions button. Click on Delete from the Actions menu.
- Type delete me in the box and click Delete.
- Deleting that Read Replica in AWS will take some time. After a few minutes, the RDS Read Replica instance is successfully deleted.
DB Failover
Now, think about a situation when the primary DB instance fails or get crashes. What happened to its RDS Read Replica? This is the case of DB Failover. In this case, the RDS Read Replica created from the primary DB instances got converted into the standalone DB instance. Let's see this case with hands-on experience. In our case, we have a primary DB instance and created an RDS Read Replica. Let's delete the primary DB instance.
Follow these Steps:
- Select the primary DB instance and click on the Actions button. Click on Delete from the Actions menu.
- Mark the Checkbox accordingly and click on the Delete button.
- After a few minutes, we saw that the RDS Read Replica Role is converted into an instance from Replica, and the Primary DB instance is deleted.
Read Replica Storage & Compute Requirements
- By default, the configuration (storage type) of the read replica is the same as the database instance from which the replica is created.
- The storage and compute resources of the read replica are the same as the source database instance from which it is replicated.
- Having similar configuration and resources allows the RDS Read Replica in AWS to function properly.
- In case the source database instance is scaled, the RDS Read Replicas should also be scaled accordingly.
Read Replicas Promotion
We learn about the RDS Read Replicas in aws and create the replica in the same region and the cross-region. Let's learn how to promote an RDS Read Replica.
As the name suggests, promotion means to upgrade, i.e. we can promote an RDS Read Replica to become the standalone DB instance. The figures show After promoting a read replica, the Amazon RDS Read Replica is promoted to the Amazon RDS DB instance.
We perform RDS Read Replica promotion for the following reason:
- To perform DDL operations
- It helps in the Sharding of the database.
- To implement failure recovery.
Let's do a hands-on demo to convert the replica into a DB instance using the RDS Read Replica Promotion.
Follow these steps:
- Earlier, we created an RDS Read Replica in another region. Go to that page and select the RDS Read Replica. Click the Actions button and choose Promote from the Actions menu.
- It will redirect to the Promote read replica page. Set the Backup retention period.
- Leave other configurations as default and click on Promote read replica.
- In a few minutes, the Role changes from Replica to Instance, i.e. now, this became a standalone DB instance from an RDS Read Replica.
Cross-Region Read Replicas
AWS provides the feature to create the RDS Read Replica across different regions.
- MariaDB, MySQL, Oracle, and PostgreSQL support the cross-region read replicas but are not supported by the SQL server.
- Cross-Region RDS Read Replicas improve the disaster recovery capabilities and reduces RTO and RPO.
- It helps to migrate the data from a data center in one region to another region.
- We can create a cross-region RDS Read Replica from a source RDS DB instance. This database should not be a replica of another RDS DB instance.
- Due to the restriction on the amount of access control list (ACL) entries for a VPC, RDS cannot guarantee more than five Cross-region Read Replica instances.
- The RDS Read Replica uses the default security group.
- If we delete the source of the cross-region RDS Read Replica, then,
- For MariaDB, MySQL, and Oracle DB instance RDS Read Replica is promoted.
- In the case of the PostgreSQL DB instance, the RDS Read Replica is set to terminate.
Read Replica Features & Limitations
Features of RDS Read Replica
Let's discuss some of the features of RDS read replicas in AWS concerning PostgreSQL and MySQL/MariaDB.
S No. | Features | PostgreSQL | MySQL/MaraiDB |
---|---|---|---|
1. | Replication Method | Physical | Logical |
2. | Transaction Logs | It has a parameter named wal_keep_segments, which decides the number of logs to keep | Binary logs which are not applied doesn't get deleted |
3. | Replica operation | Here replica can only execute read operation | Here, we can enable writer mode for the replica, after which the replica can perform write operation as well |
4. | Backups for Replicas | We can create a backup by creating snapshots, but cannot enable automatic backups | We can enable automatic backups for MySQL and MariaDB Read replicas |
5. | Parallel Replication | It does not allow for parallel replication. It is capable of supporting only a single process handling the replication. | For any version of MariaDB read replica parallel replication process is supported. For MySQL version 5.6 or greater, that supports parallel replications. |
Limitations of RDS Read Replicas
Let's explore some of the limitations of RDS Read Replicas in AWS.
- Circular Replication
- There are three kinds of replication, single, parallel and circular.
- RDS Read replicas support single and parallel replication processes but do not support circular replication.
- Repliaction Chain
- We can create RDS read replicas from primary database instances and other RDS read replicas.
- However, we have a limitation to this. There can be a maximum of four instances involved in the replication chain. This is to avoid the lag experienced in the read replicas replicated from other read replicas.
- Replicate Back
- Suppose we create a read replica named Read_Replica_1 from a database instance. After some time, if we make changes to the database instance, we do not have any configuration to replicate the Read_Replica_1 to the database instance.
- To have a read replica with the latest changes, we need to create a new read replica, say Read_Replica_2.
RDS Read Replicas Use Cases
Let's discuss the use case of the RDS Read Replicas in AWS.
- Reading Database Workload:
- For database workloads where only reading operation is required, we can scale using RDS read replicas in AWS instead of creating single DB instances.
- Read Requests:
- In some cases, the single database instance can be under maintenance or suspension due to an increase in space consumption by I/O backups.
- In such cases, the database instances cannot respond to further requests, be they read or write requests.
- To resolve this, we can use RDS Read Replicas, as the read requests can be redirected to the read replicas, and they can respond to the requests.
- One of the drawbacks of redirecting the read requests to read replica is that the data might not be up-to-date.
- Reporting:
- In some scenarios like business report generation or data warehouse work, only read requests are made to the database instances.
- Here, instead of increasing the workload on database instances, we can use RDS read replicas in AWS.
- Disaster Recovery:
- If we have only one primary database instance that fails, read replicas can be used to recover the data loss. It serves as a backup in cases of disaster.
- Another case is that the primary DB instance is located in a region where a natural disaster hits. Here, if the read replicas are created in another AWS region, they can serve as backups.
Conclusion
- Amazon RDS helps us to create read replicas of primary database instances. These replicas are capable of performing read operations.
- RDS read replicas are helpful in disaster recovery and addressing read requests when the primary database is busy or not functioning.
- Read replicas in RDS supports single process and parallel process replication, but not circular replication.
- It creates physical replication for PostgreSQL-based database instances and logical replication for MySQL/MariaDB based instances.
- Replicas are created with the same computing and storage resources for the replica to function properly.
- Using the AWS Management Console, we can configure things to be done in case of database instance failover.