Updated on 2024-10-28 GMT+08:00

Introduction to RDS for MySQL Database Proxy

Context

In read-intensive workloads, a single DB instance may be unable to handle the read pressure. If this happens, performance deteriorates and, under peak loads, workloads will suffer.

To scale the read capacity of your instance, RDS for MySQL allows you to create read replicas and enable read/write splitting. With read/write splitting enabled, write requests are routed to the primary instance and read requests to read replicas. The read pressure on the primary instance is reduced, and database performance improves.

Function Description

Using Database Proxy, read and write requests are automatically routed through a read/write splitting address.

To isolate workloads from one another, you can create multiple database proxies for one RDS for MySQL instance.

After you enable read/write splitting, if there is no read replica, read and write requests will be automatically routed to the primary instance through the read/write splitting address.

After you enable read/write splitting, if there are read replicas, write requests will be automatically routed to the primary instance and read requests to each read replica based on the weighted or load balancing routing policy through the read/write splitting address.

Basic Concepts

  • Proxy address

    After purchasing a database proxy, you can view the proxy address on the Database Proxy page. The database proxy sends write requests to the primary instance and read requests to read replicas through this address.

  • Transaction splitting

    Database proxies support transaction splitting. With this feature enabled, the read requests prior to write operations in a transaction are routed to read replicas, offloading read pressure from the primary instance.

    For more information about transaction splitting, see Configuring Transaction Splitting.

  • Connection pool

    Database proxies provide session-level connection pooling. It helps reduce the database load caused by frequently setting up short connections.

    For more information about connection pools, see Configuring Connection Pools.

  • Routing policy

    RDS for MySQL database proxies support weighted and load balancing routing policies.

    • Weighted: Read requests are routed based on the read weights you specify.
    • Load balancing: Read requests are routed to database nodes with fewer active connections. With this policy enabled, you do not need to configure the weights of nodes.

    For more information about routing policies, see Configuring the Delay Threshold and Routing Policy.

Billing

Database proxy can be enabled only for purchased DB instances. After it is enabled, it is separately billed on a pay-per-use basis.

The database proxy service is available for commercial use. It is billed by node. When you purchase a database proxy instance on the console, two nodes are created by default. The total fee is calculated as follows: Total fee = Number of nodes x Unit price. For details about the unit price, see the price of database proxy in RDS Pricing Details.

How Read/Write Splitting Works

Read/write splitting uses database proxies to split read and write requests. You can create one or more database proxies for your DB instance.

  • Single database proxy

    If your RDS for MySQL instance has only one database proxy, applications connect to the database proxy through the proxy address. Write requests are forwarded to the primary instance and read requests to the primary instance or read replicas based on the routing policy you specify.

    Figure 1 Read/write splitting with only one database proxy
  • Multiple database proxies

    To isolate workloads from one another, you can create up to four database proxies for an RDS for MySQL instance. Different applications can connect to different database proxies as required. The database proxies connect to specified read replicas and forward read requests from different applications to different read replicas for workload isolation.

    One RDS for MySQL read replica can connect to different proxies. However, you are advised to configure only one proxy that can allocate requests to the read replica.

    Figure 2 Read/write splitting with multiple database proxies

Scenario

  • Read/write splitting enables read and write requests to be automatically routed. If your application requires more proxies, you can request additional proxy nodes with just a few clicks.
  • Read requests are distributed to your read replicas based on weights to balance your database traffic and improve resource utilization.
  • A proxy routes read requests of your application only to the read replicas you specify for the proxy.

Request Routing Rules

  • The following requests will be routed only to the primary instance.
    • INSERT, UPDATE, DELETE, and SELECT FOR UPDATE
    • All DDL operations (such as table/database creation, table/database deletion, table structure change, and permission change)
    • All requests in transactions (But if transaction splitting is enabled, some read requests in transactions may be sent to read replicas. For details, see Configuring Transaction Splitting.)
    • User-defined functions
    • Stored procedures
    • Multi-statement requests
    • Requests that use temporary tables
    • SELECT last_insert_id()
    • All queries of and changes to user variables
  • The following requests will be routed either to the primary instance or a read replica.
    • SELECT not in a transaction
    • COM_STMT_EXECUTE
  • The following requests will always be routed to all database nodes.
    • Changes to all system variables
    • The USE command

Read/Write Role Processing Logic

Role

Routing Policy

Weight of Primary Instance

Normal Case

All Read Replicas Are Faulty

Read only

Weighted

Load balancing

Not configurable

Primary instance: does not process read-only requests.

Proxy address: readable but not writable

Primary instance: does not process read-only requests.

Proxy address: connection error

Read and write

Load balancing

Assigned by system

Primary instance: readable and writable

Proxy address: readable and writable

Primary instance: readable and writable

Proxy address: readable and writable

Weighted

> 0

Primary instance: readable and writable

Proxy address: readable and writable

Primary instance: readable and writable

Proxy address: readable and writable

= 0

Primary instance: not readable but writable

Proxy address: readable and writable

Primary instance: readable and writable

Proxy address: readable and writable