Home > Enterprise >  AWS RDS MySQL Read Replica - do I need to update my API to point to the replica for any URIs that ar
AWS RDS MySQL Read Replica - do I need to update my API to point to the replica for any URIs that ar

Time:10-01

This is the first time I have worked with a read replica so please bare with me. I tried multiple searches on here and google to no avail.

I know how a primary database instance and a read replica on RDS. Replication is working great, I can connect to both instances with no problem. My question is two fold.

  1. Do I need to update my API models so that read only operations are directed to the replica?
  2. Are there any tips to get the best performance from this setup? Can I adjust indexes on the master so that write commands have less indexes than tables I need for read? Lastly, some of my models read a dataset, return the value, then log the activity in an audit log table. Would I need to split this functionality out so the logging activity occurs on the main write database?

Appreciate any guidance here.

CodePudding user response:

It depends. The replica will always be slightly behind its source instance, so if you have queries that have a requirement to read current data always, they need to read from the source instance.

Different queries even in your single app will have different needs in that respect. So you really need to decide on a case-by-case basis which query can read from the read-replica instance.

I wrote a presentation about this: Read Write Splitting in MySQL or the video.

Regarding performance, you may have different secondary indexes on the same tables on each instance. Indexes depend on which queries you will run, so you have to decide first which queries you will run on the source versus the replica.

But consider that if the read-replica goes offline for some reason, or falls too far behind or something, you might want to be able to run the same queries on the source instance until the replica is ready again. In that case, you'd want the same indexes on both, or else your query will run very slowly.

Another possibility is that the source instance dies for some reason. In the cloud, you must always have a plan for resources disappearing on short notice, including databases! It happens. So if the source instance dies, the read-replica could take over as your new source. But if it has different indexes, different tables, different instance size, or whatever, then it may not be prepared to be that substitute.

It's simpler to just make the source and replica the same in as many ways as you can. Less details to track, and you're prepared for failover.

  • Related