O&M operations like switchovers on your ApsaraDB RDS for MySQL instance can cause connection drops that affect your applications. The persistent connection feature prevents these interruptions by keeping connections active during such operations, improving availability and reducing O&M costs.
Overview
The persistent connection feature of the database proxy for ApsaraDB RDS for MySQL ensures that connections between your application and the proxy remain active during events such as instance switchovers. This prevents applications that connect through a database proxy endpoint from receiving connection error messages, as the following figures illustrate.


How it works
A database proxy uses two types of connections: frontend connections between the proxy and your client, and backend connections between the proxy and the database. During an instance switchover, the proxy maintains the frontend connection even when the backend connection is interrupted. This mechanism allows the proxy to provide persistent connections.
For backend connections to an ApsaraDB RDS for MySQL instance, the key to persistence is restoring the connection state after an interruption.
The connection state for ApsaraDB RDS for MySQL typically includes system variables, user-defined variables, temporary tables, character set encoding, transaction status, and prepared statement information. This topic uses set names utf8mb4 as an example to explain how the persistent connection feature for ApsaraDB RDS for MySQL works.
Switchover
During a switchover, the database proxy for ApsaraDB RDS for MySQL maintains a persistent connection in three steps:
O&M operations that involve a switchover include:
Primary/secondary switchover
Minor engine version update
Parameter change that requires an instance restart
Primary instance configuration change
Start switchover: Block new connections and requests
The proxy cannot preserve transactions. Therefore, it handles sessions differently based on their state:
For sessions with active transactions during the blocking period, the proxy forwards requests to the backend primary instance for execution.
For sessions that start new transactions during the blocking period, the proxy blocks the requests. The client then waits for a response from the server.
For sessions with transactions still active after the blocking period ends, the proxy terminates the connection. The backend database then rolls back the uncommitted transactions.

During switchover: Switch existing connections
During the switchover, the state of existing connections is modified:
For connections that cannot be maintained, the proxy terminates the entire connection.
For connections that can be maintained, the proxy switches them to the new database instance.
The proxy clears connections to the original primary instance from the connection pool.

After switchover: Restore connections
For connections that are successfully maintained, the proxy establishes connections to the new primary instance and restores the connection state.

Failover
A failover is an unplanned event where a system failure causes an ApsaraDB RDS instance to automatically promote a secondary instance to become the new primary.
The proxy caches the SQL statements that are being executed on the database or will be forwarded. When a database failure occurs, the backend connection between the proxy and the database is interrupted. After the proxy detects the failover, it does not immediately disconnect from the client. Instead, the proxy forwards failed read requests to an available database instance and restores the connection state.
For failed write requests, the proxy cannot determine whether the writes to the database are successful. Therefore, session persistence during a failover does not support write requests.
Enable persistent connections
As of January 9, 2024, the persistent connection feature is enabled by default for qualifying ApsaraDB RDS for MySQL instances when you enable the database proxy. You can disable this feature at any time.
Prerequisites
Persistent connections for switchovers
The ApsaraDB RDS instance meets the following requirements:
Engine version: MySQL 5.6, 5.7, 8.0, or 8.4
Edition: RDS High-availability Edition or RDS Cluster Edition
Storage type: cloud disk or Premium Local SSDs
Proxy type: general-purpose or dedicated
The database proxy is enabled, and the proxy version is 1.14.5_20231207 or later.
Persistent connections for failovers
The ApsaraDB RDS instance meets the following requirements:
Engine version: MySQL 5.6, 5.7, or 8.0
Edition: RDS High-availability Edition or RDS Cluster Edition
Storage type: cloud disk or Premium Local SSDs
Proxy type: dedicated
NoteThe general-purpose database proxy supports persistent connections only for switchovers. The dedicated database proxy supports persistent connections for both switchovers and failovers.
The database proxy is enabled, and the proxy version is 2.9.1 or later.
Procedure
Go to the ApsaraDB RDS Instances page. In the top navigation bar, select the region where the instance is located. Then, find the instance and click its ID.
In the left-side navigation pane, click Database Proxy.
In the Basic Information section, click Enable next to Persistent Connection.
NoteIf the 'persistent connection' text is not displayed, it means your instance does not meet the requirements to enable persistent connections.
Use persistent connections
Prerequisites
The database proxy is enabled.
The persistent connection feature is enabled for the database proxy.
Procedure
Go to the ApsaraDB RDS Instances page. In the top navigation bar, select the region where the instance is located. Then, find the instance and click its ID.
In the left-side navigation pane, click Database Proxy.
Configure an access policy for the target database proxy endpoint based on your business requirements. For more information, see Configure an access policy for a database proxy endpoint. Set Read/Write Attributes to Read/Write (Read/Write Splitting).
Apply for an internal or public endpoint for the database proxy based on your business requirements. For more information, see Set a database proxy endpoint.
In your application, use the internal or public endpoint and port number of the database proxy to connect to the database.
When an O&M operation involving a switchover occurs on the database instance, the database proxy automatically maintains the connection. Your application's connection to the proxy is not interrupted when you use the database proxy endpoint.
Limitations
During a switchover, the persistent connection feature cannot maintain connections in the following scenarios:
Connections with an incomplete result set, where the MySQL server has not finished sending all data.
Connections with uncommitted transactions.
Connections that have used a
change userstatement.Connections that have used a
LOAD DATAstatement.Connections with temporary tables.
Connections used to subscribe to binary logs through the database proxy endpoint.
Calls to the
FOUND_ROWS(),ROW_COUNT(), andLAST_INSERT_ID()functions are not supported. Although these functions can be called, the accuracy of the returned results cannot be guaranteed. MySQL no longer recommends the use ofSELECT FOUND_ROWS(). We recommend that you replaceSELECT FOUND_ROWS()withSELECT COUNT(*) FROM tb1for queries. For more information, see FOUND_ROWS().
Usage notes
Because connections can reconnect, the thread ID of the current connection returned by the
select connection_id()query may change.Because connections are re-established, the IP address and port displayed in
show processlistor SQL Explorer may not match the client's actual IP address and port.If a connection has user-defined variables, it can be maintained, but the variables become invalid.
Disable persistent connections
Go to the ApsaraDB RDS Instances page. In the top navigation bar, select the region where the instance is located. Then, find the instance and click its ID.
In the left-side navigation pane, click Database Proxy.
In the Basic Information section, click Disable next to Persistent Connection.
Feature testing
This topic provides test results only for switchover scenarios.
Test environment
Test ApsaraDB RDS for MySQL instance:
MySQL 8.0, RDS High-availability Edition
Instance type: mysql.x2.xlarge.2c (dedicated, 8 vCPUs, 16 GB of memory)
Test tool: Sysbench
Test data:
100 tables, with 40,000 rows in each table.
Number of concurrent threads: 128.
Test method
Test the connection retention of an ApsaraDB RDS for MySQL instance in different O&M scenarios. Connection retention is the percentage of connections that remain active after an O&M operation.
Run the following test command:
sysbench --db-driver=mysql --mysql-host=127.X.X.1 --mysql-port=3306 --mysql-user=username --mysql-password='' --tables=100 --table-size=40000 --threads=128 --mysql-db=sbtest --report-interval=5 --time=600 oltp_read_write runThe following table describes the key parameters in the test command.
db-driver: Specifies the database engine.
mysql-host: Specifies the database proxy endpoint.
tables: Specifies the number of tables in the database.
table-size: Specifies the number of records in each table.
threads: Specifies the number of concurrent threads.
time: Specifies the test duration in seconds.
Test results
In the O&M scenarios tested, the ApsaraDB RDS for MySQL instance achieved 100% connection retention.
Switchover scenario | Retention |
Minor engine version update | 100% |
Primary/secondary switchover | 100% |
Primary instance configuration change | 100% |
Parameter change that requires a restart | 100% |
API reference
API | Description |
Modifies the database proxy feature for an ApsaraDB RDS instance. | |
Queries the details of the database proxy for an ApsaraDB RDS instance. |