Use read/write splitting

Updated at:

When your MySQL database handles far more reads than writes, the primary instance can become a bottleneck as traffic grows. Read/write splitting offloads read requests to one or more read-only instances, freeing the primary instance to focus on writes.

This tutorial walks you through setting up read/write splitting on ApsaraDB RDS for MySQL using the database proxy. You'll create a read-only instance, enable the proxy, test the setup with sample queries, and optionally tune read weights across multiple read-only instances.

When to use read/write splitting

Read/write splitting works well when:

  • Read requests significantly outnumber write requests (for example, product catalogs, news feeds, or reporting dashboards).

  • Business intelligence or analytics queries run against live data and would slow down the primary instance.

  • You need to scale read capacity without upgrading the primary instance.

Read/write splitting does not reduce write bottlenecks. If writes are the constraint, consider scaling up the primary instance instead.

How it works

The database proxy sits between your application and the RDS instances. All connections go through the proxy's internal endpoint. The proxy inspects each statement and routes it automatically:

  • Write statements (CREATE, INSERT) — primary instance

  • Read statements (SELECT) — read-only instance(s)

Billing

Resource Billing basis
Primary and read-only RDS instances Instance type and storage capacity
Elastic Compute Service (ECS) instance Instance type and Elastic Block Storage (EBS)
Database proxy (general-purpose) Free of charge
SQL Explorer and Audit Log traffic and log indexing

For RDS billing details, see Billable items. For ECS billing details, see Billing overview. For SQL Explorer and Audit billing details, see Billing details.

Prerequisites

Before you begin, make sure that you have:

  • An ApsaraDB RDS for MySQL instance (this tutorial uses MySQL 8.0, High-availability Edition, Standard product type, PL1 ESSD or Premium ESSD storage, 20 GB storage capacity)

  • An Elastic Compute Service (ECS) instance in the same region and virtual private cloud (VPC) as the RDS instance (this tutorial uses Pay-as-you-go, China (Hangzhou) region, Alibaba Cloud Linux 3.2104 LTS 64-bit image, root logon)

  • A database named dbtest (UTF-8 character set) on the RDS instance

  • A privileged account named dbuser with a custom password on the RDS instance

  • MySQL client installed on the ECS instance

To set up the ECS instance, see Getting started with ECS.

Install MySQL on the ECS instance

Log on to the ECS instance as root and run:

yum install -y mysql-server
systemctl start mysqld
systemctl enable mysqld

Verify the installation by running mysql. If the MySQL prompt appears, the client is ready. Run exit to return to the shell.

Important

The ECS instance and the RDS instance must be in the same region and the same VPC. Instances in different VPCs cannot communicate over the internal network.

Step 1: Enable the database proxy on the primary instance

The general-purpose database proxy is free of charge and handles read/write routing automatically. For an overview, see What are database proxies?

  1. Log on to the ApsaraDB RDS console and click the ID of your primary RDS instance.

  2. In the left-side navigation pane, click Database Proxy.

  3. In the General-purpose card, click Enable Now.

  4. In the dialog box that appears, click OK. The proxy takes approximately 1–2 minutes to become active.

Step 2: Create a read-only instance

On High-availability Edition, the secondary instance is not directly accessible. You must create a dedicated read-only instance.

  1. On the Basic Information page of the primary instance, find the Instance Distribution section and click Add to the right of Read-only Instance.

  2. On the buy page, select the following:

    • Billing method: Pay-as-you-go

    • Edition: High-availability Edition

    • Product type: Standard

    • Storage capacity: 20 GB (must be greater than or equal to the primary instance's storage capacity)

    • Other specs: lowest available to reduce cost

  3. Click Next: Instance Configuration, then Next: Confirm Order. Set Quantity to 1 and complete the payment.

  4. After the read-only instance is created, go to the Database Proxy page of the primary instance. The proxy automatically connects to the read-only instance and assigns default read weights. The connection topology shows the routing configuration.image

By default, the primary instance handles all write requests and the read-only instance handles all read requests.

For more information about read-only instances, see Overview of read-only ApsaraDB RDS for MySQL instances.

Important

To verify that read/write splitting is working correctly in Step 3, enable the SQL Explorer and Audit feature on both the primary and read-only instances before sending any requests. See Use the SQL Explorer and Audit feature. If you enable this feature, charges apply for log traffic and log indexing. Disable it after verification to avoid ongoing costs.

Step 3: Connect and send requests

Connect to the database

Important

You must connect through the internal endpoint of the database proxy, not the primary instance endpoint. Connecting directly to the primary instance endpoint bypasses the proxy entirely — all requests go to the primary instance and read/write splitting does not take effect.

To get the proxy's internal endpoint, in the left-side navigation pane click Database Proxy. In the Network Connection area, find the Internal Network (VPC) row and click Copy to copy the proxy connection address.

Log on to the ECS instance and connect to the database through the proxy:

# Template
mysql -h<proxy-internal-endpoint> -P3306 -udbuser -p

# Example
mysql -hmr-bm**************.rwlb.rds.aliyuncs.com -P3306 -udbuser -p

Replace <proxy-internal-endpoint> with the internal endpoint from the connection topology. Use port 3306 and the dbuser privileged account.

Send write requests

Switch to the dbtest database, create a table, and insert test data:

-- Switch to the test database
USE dbtest;

-- Create a test table
CREATE TABLE table_test (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    age INT
);

-- Insert test data
INSERT INTO table_test (id, name, age) VALUES (1, 'user1', 25);
INSERT INTO table_test (id, name, age) VALUES (2, 'user2', 30);
INSERT INTO table_test (id, name, age) VALUES (3, 'user3', 28);
INSERT INTO table_test (id, name, age) VALUES (4, 'user4', 18);

The proxy routes CREATE TABLE and INSERT statements to the primary instance.

Send read requests

Run SELECT queries to generate read traffic:

SELECT * FROM table_test;
SELECT * FROM table_test WHERE id = 1;
SELECT * FROM table_test WHERE id = 2;
SELECT * FROM table_test WHERE id = 3;
SELECT * FROM table_test WHERE id = 4;

The proxy routes all SELECT statements to the read-only instance.

Step 4: (Optional) Verify read/write splitting

This step requires SQL Explorer and Audit to be enabled on both instances before the requests in Step 3 were sent.

After the requests complete, check the SQL logs on each instance to confirm the routing.

Verify write requests on the primary instance

  1. On the Instances page, click the ID of the primary instance. In the left-side navigation pane, click SQL Explorer and Audit, then click the Audit tab.

  2. Set the User filter to dbuser, the Database filter to dbtest, specify the time range, and click Query.

The Logs section should show one CREATE TABLE statement and four INSERT statements — no SELECT statements. This confirms that only write requests reached the primary instance.

Verify read requests on the read-only instance

  1. On the Instances page, click the ID of the read-only instance. In the left-side navigation pane, click SQL Explorer and Audit, then click the Audit tab.

  2. Apply the same filters and click Query.

The Logs section should show five SELECT statements — no write statements. This confirms that only read requests reached the read-only instance.

Step 5: (Optional) Add read-only instances and configure read weights

A single read-only instance can become a bottleneck as read traffic grows. Add more read-only instances and set custom weights to distribute load proportionally.

The read weight ranges from 1 to 10,000. A higher weight means the instance receives a larger share of read requests. Weights are proportional — for example, weights of 100 and 300 result in a 1:3 read allocation ratio.

Add a second read-only instance and set weights

  1. Create a second read-only instance following the same steps as Step 2. Enable SQL Explorer and Audit on the new instance if you want to verify weight allocation. See Use the SQL Explorer and Audit feature.

  2. On the Database Proxy page of the primary instance, find the connection topology and click Modify Configuration.

  3. In the dialog box, set Read Weight Allocation to Custom. Set the weight for the new read-only instance to 300 and keep the original instance at 100. Click OK.

For more details on weight configuration, see Configure the read and write attributes and the read weight.

Verify weight allocation

  1. On the ECS instance, run the following SELECT statement eight times:

    SELECT * FROM table_test;

    With weights of 100 and 300 (a 1:3 ratio), the proxy should distribute 2 requests to the first read-only instance and 6 requests to the second.

  2. Check SQL Explorer and Audit on the read-only instance with weight 100. The logs should show 2 SELECT statements.

  3. Check SQL Explorer and Audit on the read-only instance with weight 300. The logs should show 6 SELECT statements.

Step 6: Release resources

Release resources after completing this tutorial to avoid ongoing charges.

  1. Release the ECS instance: Log on to the ECS console, find the ECS instance, click the More icon in the Actions column, and select Release.

  2. Release the RDS instances: Log on to the ApsaraDB RDS console, find the primary instance, click More in the Actions column, and select Release Instance. Releasing the primary instance automatically releases all associated read-only instances and disables all enabled features.

Important

If you plan to keep the RDS instances but no longer need SQL Explorer and Audit, disable the feature to stop log traffic and indexing charges. See Use the SQL Explorer and Audit feature.

What's next