This topic describes how to enable the read/write splitting function for an RDS for MySQL instance. When this function is enabled, your application connects to a read/write splitting module through a read/write splitting address. The read/write splitting module distributes write requests to the master instance, and distributes read requests to the read-only instances based on the preset read weights.

Prerequisites

  • The RDS instance is a master instance.
  • The MySQL version and RDS edition are as follows:
    • MySQL 8.0 High-availability Edition (with local SSDs)
      Note In the MySQL 8.0 High-availability Edition (with local SSDs), you must activate the dedicated database proxy service before you can use the read/write splitting function.
    • MySQL 5.7 High-availability Edition (with local SSDs)
    • MySQL 5.6
  • The RDS instance belongs to one of the following regions: China (Hangzhou), China (Shanghai), China (Qingdao), China (Beijing), China (Zhangjiakou), China (Hohhot), China (Shenzhen), China (Hong Kong), Singapore, Australia (Sydney), US (Virginia), US (Silicon Valley), Germany (Frankfurt), and UK (London).
  • The RDS instance has at least one read-only instance. For more information about how to create a read-only instance, see Create an RDS for MySQL read-only instance.
  • No replication delays are set for the read-only instances that are attached with the RDS instance. If replication delays are set, the read/write splitting function does not take effect on the read-only instances.读写分离无效

Billing

The read/write splitting function is free of charge. You pay only for the read-only instances that you use.

Precautions

  • If it is the first time that you enable the read/write splitting function, the system automatically upgrades the backend administration systems of the master and read-only instances to the latest version to guarantee service availability. When the read/write splitting function is being enabled, the master instance is disconnected for 30 seconds or less. Additionally, the read-only instances is inaccessible during the whole restart process. We recommend that you enable the read/write splitting function during off-peak hours and make sure that your application can automatically reconnect to the RDS instance.
  • If you have restarted or changed the specifications at least once for the master instance and its associated read-only instances after March 8, 2017, the backend administration systems of these instances have been automatically upgraded to the latest version. In such case, when you enable the read/write splitting function, the system does not restart the RDS instance or generate a transient disconnection.
  • The generated read/write splitting address is fixed. It does not change even when you enable and disable the read/write splitting function for multiple times. Therefore, you do not need to change the configuration data on your application frequently. This reduces maintenance costs.
    Note The read/write splitting address cannot be changed manually.
    读写分离基本信息

Procedure

  1. Log on to the RDS console.
  2. Select the target region.选择地域
  3. Find the target RDS instance and click the instance ID.
  4. In the left-side navigation pane, click Database Connection or Database Proxy.
  5. On the Read/Write Splitting tab, click Enable now.
    Note
    • If theEnable now button is not displayed, you must enable the database proxy and create at least one read-only instance.
    • If the master and read-only instances were created before March 8, 2017 and you have not restarted them or changed their specifications, enabling the read/write splitting function incurs a restart of the instances. In such case, you must click OK in the displayed dialog box to enable the read/write splitting function.
  6. Set the following parameters.设置读写分离
    Parameter Description
    Network Type The type of the read/write splitting address.
    • Intranet address
      • If the network type of the master instance is VPC, then the network type of the read/write splitting address is also VPC.
      • If the network type of the master instance is Classic Network, then the network type of the read/write splitting address is also Classic Network.
    • Internet Address: used to access the RDS instance from the Internet. The Internet is prone to fluctuations. Therefore, we recommend that you use an internal endpoint to connect to the RDS instance.
    Latency Threshold The maximum latency allowed for data synchronization between the master and read-only instances. When the data synchronization latency of a read-only instance exceeds this threshold, the system no longer distributes read requests to the read-only instance no matter how high the read weight of the read-only instance is.

    The latency threshold ranges from 0 seconds to 7,200 seconds. Due to limitations in the execution of SQL statements, a read-only instance may synchronize data from the master instance at a certain latency. We recommend that you set this parameter to a value greater than or equal to 30 seconds.

    Read Weight Distribution A higher read weight indicates more read requests to process. For example, the master instance has three read-only instances, and their read weights are 0, 100, 200, and 200, respectively. Then the master instance does not process read requests (write requests are still automatically sent to the master instance), and the three read-only instances process read requests at the 1:2:2 ratio.
    • Automatic Distribution: The system automatically assigns a read weight to each instance based on the instance specifications. After a read-only instance is created, it is automatically added to the read/write splitting link according to the read weight assigned by the system. For more information, see Rules of weight allocation by the system.
    • Customized Distribution: You can manually set the read weight of each instance. The value ranges from 0 to 10000. After a read-only instance is created, its read weight is 0 by default. You must manually specify a read weight for the read-only instance.
    Note You cannot specify a read weight for a read-only instance for which a replication latency is specified.
  7. Click OK.
    Note The instance status changes to Creating Network Connection. Wait until the instance status changes back to Running.

What to do next

You can change the configuration data on your application so that your application uses the read/write splitting address to connect to the RDS instance.
Note You can obtain the read/write splitting address on the Basic Information page or on the Read/Write Splitting tab of the Database Connection page.

FAQ