Scaling databases with AWS RDS and read replicas
Tags: aws, cloud, database, tutorial
Published: 2024-06-30 07:41:42

Amazon Web Services (AWS) Relational Database Service (RDS) is a managed service that simplifies the process of configuring, operating, and scaling a relational database in the cloud. AWS RDS is a versatile choice that supports several database engines including PostgreSQL, MySQL, Oracle, MariaDB, Microsoft SQL Server, and Amazon's own Aurora.

### Advantages of using RDS over a self hosted database
- **Managed Service** - AWS RDS is a fully managed service, which means that AWS takes care of provisioning, patching, backup, recovery, and even scaling. This allows developers and database administrators to focus on the application development and optimization rather than managing the underlying infrastructure.
- **Continuous Backups** - RDS provides continuous backups based on a defined retention period. It ensures that your data is always protected and can be restored when necessary. This automatic backup process eliminates the risk of data loss due to unforeseen events.
- **Point in Time Restore** - With RDS, you can restore your database to any specific point within your backup retention period.
- **Read Replicas** - RDS supports the creation of read-only replicas of the master database. This helps in reducing the load on the primary database. Read replicas are ideal for read-heavy applications. This helps in distributing the read traffic and increasing the performance.
- **Monitoring Dashboards** - AWS CloudWatch integration allows you to monitor key metrics such as network traffic, CPU utilization, disk I/O, and memory usage without the need of any third-party tools.

### Creating a new database
AWS RDS provides two methods for creating a new database.
- **Standard create** - You can set all the configuration options including for availability, security, backups and maintenance. You have full control on configuring your database needs.
- **Easy create** - It uses recommended best-practice configurations. Some configurable options can be changed even after the database is created.

Let's create a new PostgreSQL database using easy create method.
- Select `Easy create` method for database creation.
  ![Database Creation Method](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5f6y5ggntk9jnm19gldv.png)
- Select `PostgreSQL` as a database engine.
- Select your preferred database instance size. You can select either production, dev/test, or free tier if applicable.
- Add a name for your database instance and for master user.
  ![Database Identifier](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tzs668gg6umhzdi5rnsx.png)
- You can configure the credentials manager as AWS Secrets Manager or self manage the credentials.
- Click on `Create database` option to create your new `PostgreSQL` database.
- It takes a couple of minutes to get a new database up and running on AWS cloud.

### Auto Scaling RDS
RDS Auto Scaling helps in dynamically increasing the storage capacity of your database instance without manual intervention. This feature is particularly useful for applications with unpredictable workloads. You can set `Maximum Storage Threshold`, which is the highest limit for database storage to prevent infinite scaling. 
![Auto Scaling RDS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x1sm9n1sg709mvun28md.png)
RDS auto scales under the following conditions:
1. Storage is less than 10%.
2. Low storage lasts for at least 5 minutes.
3. 6 hours have passed since the last storage modification.

### Read Replica
Read replica is one of the powerful feature of RDS that allow you to create up to 15 read-only copies of your primary database. These replicas can be located within the same availability zone (AZ), across different AZs, or even in different regions.

Key benefits of using Read Replicas:
1. You can offload the read traffic to read replicas. This significantly reduces the load on primary database thus enhances overall performance.
2. Read replicas can be promoted to a standalone database in case of a failure of the primary database. It helps in disaster recovery.
3. You can scale your read-heavy applications by distributing read traffic across multiple replicas.

Following things you should consider while creating a replica:
1. Data replication to read replicas is asynchronous. It means there could be a slight delay which is also known as replication lag before changes in the primary database are reflected in the replicas.
2. Applications must use the specific connections strings for read replicas to route the read traffic appropriately.
3. Replication within the same region is free but cross region replication incurs additional cost.

Let's create a read replica of the database that we created before.
- Select `Create read replica` option from the actions menu of database homepage.
![Action Menu](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x1cate1vf2f0xvllb75u.png)
- Select the replica source and add a replica database instance identifier.
![Replica Source](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mtqbt5ah0yipz93pxo3d.png)
- You can select the destination region where the replica will be launched.
![Replica Region](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93c78s0l2xa8pnm7w7rd.png)
- Configure other settings as required and click on `Create read replica` option to create a read replica of your primary database instance.

### RDS Multi AZ for Disaster Recovery
RDS multi AZ deployments provide high availability and reliability for database instances. In a multi AZ setup, any changes made to the primary database are synchronously replicated to a standby instance in a different availability zone (AZ). This ensures that the standby instance in another AZ is always up-to-date with the primary database.

Key features of multi AZ deployments:
- **Automatic Failover** - In the event of a failure in the primary database, RDS automatically switches to the standby instance in another AZ. This ensures continuous operation with minimal downtime.
- **High Availability** - Multi AZ deployments are designed for high availability and disaster recovery. They are not meant for scaling read operations. However, you can combine multi AZ setup with read replicas for additional disaster recovery capabilities.

You can refer the official user [guide](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Welcome.html) to learn more about AWS RDS.