What is a read replica?
Read replicas are the secondary copies of your primary writer instance used to enhance performance and durability. They can be used in the DR scenario(by promoting it to primary), but in most cases, they are used for scaling purposes to offload the read requests from the Primary writer instance. We can create multiple read replicas from the Primary instance and, for most of the database, the number of 5(e.g., MySQL, MariaDB, Postgres, etc.). Some exceptions exist, e.g., the SQL server requires you to enable Multi-AZ with always-on-availability groups, and support is only available in the enterprise edition. Same with Oracle, you need to Oracle enterprise edition to enable read replicas.
RDS supports cross-region read replicas, which is helpful in disaster recovery. You can create 5 cross-region read replicas. For MariaDB and MySQL, and for specific versions of PostgreSQL, you can create a read replica from an existing read replica.
One common problem that most people face is replication lag. Replication lag refers to the amount of time it takes for changes made on the primary instance to be processed on the read replica. Simply, it defines how far the replica instance is from the primary writer instance.
💡 How replication works with different DB engine types
⓵ MySQL and MariaDB both use logical replication
② Oracle, Postgres, and SQL Server both use physical replication
✅ How does it work?
When you create a read replica, you specify the DB instance as your source. RDS takes a snapshot of this source instance and creates a read replica. It then performs asynchronous replication to update the read replica whenever there are changes on the primary. Some additional steps AWS needs to be performed if you are creating read-replica in different regions. Once the snapshot creation is complete, AWS starts cross-region snapshot copy for the initial data transfer. AWS RDS then uses the copied snapshot for the initial data load on the read replica. Once the data load is completed, the replica status is changed to available, and the DB snapshot copy is deleted. AWS has started replicating the changes to this read replica since the start of the create read replication operation. During this phase, you will see the replication lag > 0. Reference: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_ReadRepl.html
Difference between multi-AZ and read-replica
⓵ In the case of read-replica, the replication happens asynchronously, whereas multi-AZ replication is done synchronously, i.e., all changes made to the primary will also be made on the standby instance before the primary considers the operation complete.
② Read replicas are used for scaling purposes to offload the read requests from the Primary writer instance. The multi-AZ instance can provide us with availability and durability but not scalability, i.e., you can’t use it as a standby instance to offload the workload.
⓷ RDS allows read-replica creation in different regions, which is a good use case for disaster recovery(DR) scenarios. Multi-AZ support is a regional service; you need both Primary and replica regions.
④ You will get a separate endpoint when creating a read replica. In the case of multi-AZ, it’s the same endpoint, and AWS will update Route53 when failover occurs.
🚨🚨🚨 Please reread this, as this will bite you if you don’t pay special attention. When you perform a major version upgrade of your primary DB instance, all the read replicas in the same region are automatically upgraded. If your read replica is in a different region, you need to upgrade it manually. https://aws.amazon.com/premiumsupport/knowledge-center/rds-postgresql-version-upgrade-issues/
Creating a cross-region read replica
1️⃣ Go to AWS Management Console and open the Amazon RDS console at https://console.aws.amazon.com/rds/.
Choose the database and under the action drop-down, click on Create read replica.
2️⃣ Keep all the other settings as default. Under Destination, region, choose the region where you want to create a read replica.
3️⃣ Under the DB instance identifier, enter a name for the read replica. Click on Create read replica.
📚 Please check out my new book, Cracking the DevOps Interview
https://pratimuniyal.gumroad.com/l/cracking-the-devops-interview