Skip to content

Setting up read replicas on managed MySQL

A read replica on managed MySQL is a 10-minute provision and a multi-week behavior change in the application. Here is the order we do it in.

Provisioning a read replica on a managed MySQL is the easy part. Convincing an application that has only ever talked to one database to use two — without writing to the replica by accident, without reading stale data on a path the user thinks is fresh, and without leaving the replica idle — is the work. Here is the order we do it in on a Cloud PaaS environment, and the verification steps that catch the common mistakes before they reach production.

What’s happening

Managed MySQL exposes a writer endpoint (always the current primary) and a reader endpoint (load-balanced across replicas). Replication is asynchronous by default — writes commit on the primary and replay on replicas with a small lag, usually under a second on a healthy cluster. The application has to opt into the split: most ORMs and database libraries default to a single connection that goes to the writer.

How to verify

Before promoting the replica to a real traffic role, sanity-check it. On the primary:

SHOW MASTER STATUS;
SHOW REPLICAS;

On the replica, confirm it is in sync:

SHOW REPLICA STATUS\G

The fields that matter: Replica_IO_Running and Replica_SQL_Running both Yes, and Seconds_Behind_Source consistently in single digits. Spikes above 30 seconds mean either the primary is overloaded or the replica’s instance class is undersized.

To see whether the application is actually sending reads to the replica, run on the replica:

SELECT user, host, db, command, count(*)
FROM information_schema.processlist
GROUP BY user, host, db, command;

If you see only Binlog Dump and replication threads, the replica is receiving writes from the primary but no application traffic.

The fix

  1. Add a reader connection to the application’s database config. Frameworks with native read/write splitting (Laravel, Rails, Django) need a few lines of config. Frameworks without it need a small wrapper that picks a connection per query intent.
  2. Mark every code path that issues a write, sits inside a transaction, or reads back its own write as “primary only.” Lists, search results, dashboards, reports, public read APIs go to the replica.
  3. Set an explicit lag tolerance. If Seconds_Behind_Source exceeds, say, 5 seconds, the application should either fall back to the primary or surface a “delayed data” banner. Silent stale reads are the bug we see most often after a naive split.
  4. Move expensive analytical queries off the primary entirely. A 30-second report query on the primary contends with OLTP traffic and stretches the backup window; the same query on the replica is invisible to users.

Once the split holds for two weeks without an incident, the replica earns the next CPU upgrade instead of the primary.

If you provisioned a managed MySQL replica and it is not earning its keep, our Cloud PaaS team can take the application through the routing change — start a scoping call.