Leveraging AWS Read Replica for minimal downtime

02 / Nov / 2015 by Mohit Dayal Gupta 0 comments

It is really a very big problem to apply indexing or executing any query on a table containing millions of records. There is a large probability of query taking locks on the table. A few people just wait for the query to execute while some just end up killing important background queries. Both the above-mentioned scenarios are not fruitful as the query taking lock might slow or turn down the entire database. It is not either a better choice to kill background queries as it may affect the running database. The same situation occurred to me and rather than choosing one of the above ways, I decided to use a significant feature of Amazon Web Services – AWS Promoting Read Replica. By executing queries or indexing on AWS Read Replica and promoting it to the database, you will be on a much safer side.

AWS Read Replica is wrongly perceived by many people that it is just used for executing read queries on a database, instead we can also apply indexing directly to read replica. One major disadvantage of such indexing is that it will not be applied directly to the main DB instance. This is where the AWS Read Replica Promotion comes into action. We can promote the Read Replica to the main DB instance.

The same feature can also be used for altering the schema or modifying large tables in a  production environment. There will be minimal downtime. The downtime will be mainly of 10-20 seconds when we have to apply read locks to the main database instance. The read lock is applied so that no write query is able to execute on a database instance. Thus, Read Replica will be in sync to main DB instance.

The following are the steps to apply indexing on AWS RDS having millions of records:

  • Create a Read Replica of existing RDS.

Selection_086

Click on Instance Actions and then click on Create Read Replica. The following window will appear:

Selection_088

Always check Yes on Enable Automatic Backups. If you choose No, then you will not be able to create Read Replica of the promoted RDS.

  • Create read locks on main RDS.
The read locks will prevent any write query to execute in the main RDS thereby making RDS read replica in sync to the main Database.
  • Execute indexing or alter tables on the Read Replica.

The indexing can be applied to the AWS Read Replica. The indexed data will not be reflected in the original database instance. The indexed data will only be served through Read Replica.

  • Promote Read Replica to the Database instance.

Selection_089

Promoted read replica will now behave as main RDS and not as read replica. If required, new read replicas can be created. After clicking on Promote Read Replica, a new RDS will be created with indexed data.

The new RDS can now be replaced with the original database instance.

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *