Doing failover in Amazon Aurora Read Replica with the highest priority
Tags: database, rds, aws
Published: 2023-02-05 23:22:27

In this article, I am going to show you how to create a high-available Aurora Cluster via the Amazon RDS Management Console, add 2 Aurora Replicas with different priorities, and in different availability zones, test a fail-over scenario, and then terminate the whole infra-structure.
 
- By default, an Amazon Aurora cluster has only one primary compute instance performing read/write operations. 

- By adding one or more Aurora Replicas to the cluster, read scalability and high availability increases.

- If the primary instance in the cluster fails, Aurora automatically promotes an existing Replica with the highest priority to be the new primary instance.

- Generally, you’ll want to create Aurora replicas in a different Availability Zone (AZ) than the primary instance. 

- This way, your database can quickly fail over to the Replica in the other AZ in the event of infrastructure detects problems in the primary AZ.

- However, if you did not add Aurora Replicas to the cluster, you will have to wait for Aurora to create a new replacement primary instance for you when a failure is detected, which may take longer time.

- Amazon Aurora uses MySQL compatibility, but you can follow a similar process for the PostgreSQL edition. 

Please visit my **[GitHub Repository for Aurora articles](https://github.com/awsmine/Aurora_projects)** on various topics being updated on constant basis.


_**Let’s get started!**_


# Objectives:

**1.** Create an Amazon Aurora cluster

**2.** Create an Aurora Replica for High Availability

**3.** Set up the database cluster for failover

**4.** Test database failover

**5.** Terminate Resources


# Pre-requisites:
- AWS user account with admin access, not a root account.
- AWS CLI.


# Resources Used:

**[Creating a RDS MySQL DB instance](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_GettingStarted.CreatingConnecting.MySQL.html)**

# Steps for implementation to this project:

## 1. Create an Amazon Aurora cluster

- On the Select engine page, select Standard Create. 

- Create an an Amazon RDS database with the following parameters -
**`Standard create Amazon Aurora engine, Dev/Test, database-1, <username> admin, <password>, db.r5.large, default vpc, DB subnet group - default, public access - no, Under Multi-AZ deployment - select No, VPC security group - create new - aurora-sg, us-east-1a, under additional configuration - myrds`**
    
- Create database.
    
- Wait for 3-4 minutes till the status changes to **`available`**

- Note: **`availability zone - us-east-1a`**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/89jijsfc7szn1yo3ddut.png)

## 2. Create 2 Aurora Replicas for High Availability

- You can increase compute availability by adding Read Replicas in different Availability Zones (AZs) within an AWS region. 

- Up to 15 Aurora Replicas can be added across the AZs within a region. 

**Create a 1st reader**

- Select the radio button for the cluster and create an Aurora Replica by selecting Actions > Add reader.

- **`reader-1, us-east-1c, Under Additional configuration - Failover priority - 0`**

- Click Add reader.

**Create a 2nd reader**

- Select the radio button for the cluster and create an Aurora Replica by selecting Actions > Add reader.

- **`reader-2, us-east-1d, Under Additional configuration - Failover priority - 1`**

- Click Add reader.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s9nlsrg6tp29b6z7qrot.png)


## 3. Set up the database cluster for failover

> Use this Step only, if you do not use priority order while creating Read replicas shown in Step 2.

- Select the radio button next to your Reader DB instance - **`reader-1`** and select Modify.

- Set the highest priority. Under Failover, select Tier-0.

 - During a failover, Amazon RDS will promote the replica with the highest priority (starting with tier 0) to become the new primary instance. 

- Select Continue, then Modify DB instance.

## 4. Test database failover

- Select the radio button on the target writer instance. 

- Then select Actions > Failover. 

- During a failover, Amazon RDS will promote the replica with the highest priority (starting with tier 0), to become the new primary instance.

- This will cause the replica - **`reader-1 with tier-0`** to be promoted as the new primary (or writer) instance and old primary (or writer) instance becomes a new read replica.

- **`reader-2`** remains a read replica, as we have set the priority order as - **`tier-1`**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0e0tdcbb5cr3m8fkmvkm.png)

- The time it takes for the failover to complete depends on the amount of database activity at the time of failover but is typically under 60 seconds. 

- You can monitor failover process under Log & events > Recent events.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mattaa39p23ckqbze0bn.png)

- The failover is transparent to the application by using endpoints. 

- While the cluster and reader endpoints are used as DNS for the database, the instance connection will remain the same and automatically use the new db instance.

## 5. Terminate Resources

- Delete **`reader-1 and reader-2`**.

- Delete Amazon Aurora cluster 

- Do not create a final snapshot and select the acknowledgement. Enter delete me and select Delete. This step will delete the Aurora cluster, including storage and all automated db backups.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bsrel5ekmcfd8dkadoyc.png)

- After some time you will this that database-1 as deleting

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/swynrcq3xz9gts329xur.png)


# What we have done so far

- We have created a high-available Aurora Cluster via the Amazon RDS Management Console, added 2 Aurora Replicas with different priorities, and in different availability zones, tested a fail-over scenario, and then terminated the whole infra-structure.