Playing with AWS RDS (#2)
Tags: aws, beginners, postgres, tutorial
Published: 2022-12-27 12:19:57

Ok, let's start from the previous post in this series..
[Playing with AWS RDS (#1)](https://dev.to/rajdwipchaki/playing-with-aws-rds-1-578c)
Quick recap of what we have done..
1. Create a RDS instance
2. Connect from pgadmin
3. Change the instance class (resizing)
4. Multi-AZ Failover

In this post, we'll focus on read-replicas.

As per AWS documentations, Amazon RDS Read Replicas provide enhanced performance and durability for Amazon RDS database (DB) instances. They make it easy to elastically scale out beyond the capacity constraints of a single DB instance for read-heavy database workloads. 
![Read-replica](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zied5a8ccjfnw4g7xcwx.png)

a) You can create one or more replicas of a given source DB Instance and serve high-volume application read traffic from multiple copies of your data, thereby increasing aggregate read throughput. 
b) Read replicas can also be promoted when needed to become standalone DB instances.

So let's first create a read-replica and test out the replication. Click on the primary DB instance identifier in the RDS console. From actions, select 'Create read replica'.

![Create RR](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ylhvlvxobwgl6alhwaa1.png)

Set Multi-AZ to 'No', Publicly accessible to 'Yes'.
Give a name to the replica DB instance (e.g., mydbinstancerr). Leave other attributes to their default values and click on the 'Create read replica' button.

![RR attributes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zrjfyvn07wsoi10j9vjb.png)

It would take some time for the new instance (yes, read-replica will be a new DB instance) to be up and running.

![RR status](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/92u39qpl007k13xn2dyf.png)

While it's still being created, let us quickly see what is being shown in the replication status. 
Select the primary DB instance (which is 'mydbinstance' for my case) and go to the 'Replication' card. You can find that the replication state and replication lag information is not yet populated.

![RR state](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z0whzx2c59d6ugmh78yo.png)

Wait until the read-replica instance is available..

Now, let's test out the replication.
For this, we would create a table with random data in primary DB instance and then check in the replica DB instance if the data gets replicated. Here is our sample SQL.

```
	CREATE TABLE Results (
	rollno NUMERIC NOT NULL,
	marks NUMERIC NOT NULL);
	
	insert into Results (rollno, marks) 
	SELECT  num.*,ceil (random()*100)
	FROM    generate_series(1000,1500) num;

```
Let's execute this SQL query in pgadmin.

![exe query1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/odi11ywwv49rdhau1opp.png)

Query is successful. The 'Results' table is created with 501 records.

![all rows](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dtectgpkcnrjjttphab9.png)

Now check in the replica DB instance to find out if this information is replicated or not. First, create a DB connection in pgadmin (follow the post#1 in this series) and then search for the 'Results' table.


![rr all rows](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ifdyso2rm1ox34vtgljm.png)

This is great, we can find the records have already been replicated in the replica DB instance.

At this point, let's go back to the 'Replication' card in the RDS console to find out the replication state and lag information.

![rr state current](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x5wpbqwoaoe095hob3jq.png)

Note that the read-replica can also be promoted to a standalone database instance, but you can't use it as a replication target. So the replication would stop. Let's test this out now.

Promote read-replica

![prmote rr](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ajt1sn8ossxj2x0lv3ko.png)

Ignoring backup for time-being.

![promote rr final](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pe3a1gl96nifsdes26p9.png)

See both the DB instances are being shown in the same level now..
![prmote in prgs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b8hcw6uyhq8iol0dlvm8.png)

Go to the 'replication' card.

![rep card after promote](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dghcb9c9l126sb1jykn7.png)

'mydbinstancerr' instance is not there anymore. You can try out the sample SQL query (change the table name to 'Results1') again and check yourself through pgadmin whether replication is still happening or not.