PolarDB for MySQL provides a resource control feature that lets you create rules to limit the CPU usage of specific users, databases, or connection IDs. This feature helps reduce the business impact of traffic spikes and lets you quickly impose resource limits on large queries.
Version limitations
Only Enterprise Edition clusters are supported.
For Cluster Edition clusters:
PolarDB for MySQL 8.0.1 with revision version 8.0.1.1.48 or later.
PolarDB for MySQL 8.0.2 with revision version 8.0.2.2.27 or later.
For Multi-master Cluster (Limitless) clusters:
PolarDB for MySQL 8.0.1 with revision version 8.0.1.0.33 or later.
Notes
The PolarDB Resource Control feature is different from the official MySQL Resource Group feature. They use two different mechanisms to control resource usage in a cluster:
The PolarDB Resource Control feature limits resources by database, database user, and database connection. This feature is well-suited for cloud-native databases.
The official MySQL Resource Group feature lets you set the number of CPU cores and thread priorities for different resource groups. For more information, see the official MySQL document Resource Groups. This feature is not currently supported in PolarDB for MySQL.
To use the Resource Control feature, you must enable the Thread Pool feature.
Read-only (RO) nodes asynchronously synchronize resource control information from read-write (RW) nodes. Therefore, delays may occur.
Usage
After you connect to the PolarDB database cluster, log on with a privileged account.
Enable the Resource Control feature
The method for modifying PolarDB cluster parameters differs between the console and a database session. The differences are as follows:
In the PolarDB console
Compatibility: Some cluster parameters in the PolarDB console have the loose_ prefix for compatibility with MySQL configuration files.
Procedure: Find and modify the parameters that have the
loose_prefix.
In a database session (using the command line or a client)
Procedure: When you connect to the database and use the
SETcommand to modify a parameter, remove theloose_prefix and use the original parameter name.
Parameter name | Level | Description |
| Global | Enables or disables the Resource Control feature for the cluster.
|
Create a resource control
CREATE polar_resource_control <rc_name> max_cpu <max_cpu_value>;Parameters:
Parameter | Description |
rc_name | The name of the resource control to create. The maximum length is 64 characters. |
max_cpu_value | The maximum CPU for the resource control. The value is a percentage of the total cluster CPU. The value ranges from 1 to 100. |
After a resource control is created, you can run the following SQL query from a high-privilege account to view all resource controls.
SELECT * FROM mysql.polar_resource_control;You can run the following SQL query from a high-privilege account to retrieve the Thread Groups under a resource control.
SELECT id, resource_control_name FROM information_schema.thread_pool_status;Update a resource control
ALTER polar_resource_control <rc_name> max_cpu <max_cpu_value>;Description:
Parameter | Description |
rc_name | The name of the resource control. The name can be up to 64 characters in length. |
max_cpu_value | The new maximum CPU usage allowed by the resource control. Specify the value as a percentage of the total CPU capacity of the cluster. Valid values: 1 to 100. |
After you update the maximum CPU usage allowed by a resource control, the number of thread groups associated with the resource control may change.
Delete a Resource Control
DROP polar_resource_control <rc_name>;Description:
Parameter | Description |
rc_name | The name of the Resource Control to delete. |
After a resource control is deleted, its associated thread groups are released. The users, databases, or connections that were previously limited by the resource control are no longer restricted.
Set a resource control for a user or database
Attach
SET polar_resource_control <rc_name> FOR [database|USER] <db_name/user_name>;The SQL statement is as follows:
Parameter | Description |
rc_name | The name of the resource control to apply. |
db_name/user_name | The database or user to which you want to apply the resource control. |
After this statement is executed, the specified database or user is subject to the CPU resource limits set by the resource control. This change does not affect currently running statements. It applies only to statements that are executed after the resource control is applied.
Detach
RELEASE polar_resource_control <rc_name> FOR [database|USER] db_name/user_name>;After this statement is executed, the specified database or user is no longer restricted by the CPU resource limits of the resource control. Statements that are already running remain subject to the limits. Statements that start after the resource control is removed are not subject to the limits.
Configure a resource control for a query or connection
Attach
SET polar_resource_control <rc_name> FOR [query|connection] <connection_id>;Parameters:
Parameter | Description |
rc_name | The name of the Resource Control to attach. |
connection_id | The ID of the connection to limit. |
When you attach a Resource Control to a query, you can run
SHOW PROCESSLISTto view the corresponding connection ID. After the query completes, it is no longer limited by the Resource Control.When you attach a Resource Control to a connection, the resource limits remain active after the current query completes. The limits apply until the connection is closed.
Release
RELEASE polar_resource_control <rc_name> FOR [query|connection] <connection_id>;Parameters:
Parameter | Description |
rc_name | The name of the resource control to attach. |
connection_id | The ID of the connection to restrict. |
When you release a Resource Control from a query, the query is no longer limited by that control. After the query completes, the connection is not limited if no other Resource Control applies.
After you release a Resource Control from a connection, the connection is then limited by any applicable Resource Control set for the connection, user, or database. If no other Resource Control applies, the connection is not limited.
Priority of resource controls
Resource controls are applied to a query statement in the following order of priority. The first match found is used.
A control explicitly attached to the query by its connection ID. This has the highest priority.
Determines whether the current connection is attached to resource control and applies resource limits accordingly.
The system checks if a connected user is attached to resource control. If so, resource limits are applied.
If the currently connected database is attached to resource control, the system applies resource limits.
If none of the preceding conditions are met, resource limits do not apply.
Using a multi-master cluster
A Multi-master Cluster works the same as a single-master cluster, and the syntax is fully compatible. When you execute a CREATE POLAR_RESOURCE_CONTROL, ALTER POLAR_RESOURCE_CONTROL, or DROP POLAR_RESOURCE_CONTROL statement, it is randomly forwarded to a read/write node in the cluster. The statement is then synchronized across all read/write nodes, which may cause some latency. To limit resources for a query or connection, you can use the HINT syntax to specify a node.
Best practices
Scenarios
Prevent malicious users from consuming excessive cluster resources.
Handle sudden slow queries.
Prevent malicious users from triggering automatic scaling in a Serverless cluster.
Limit CPU usage in multi-tenant environments or when different services share a cluster.
Usage examples
Prevent malicious users from consuming excessive cluster resources
To handle malicious users, you can set CPU resource limits for them. You can apply these limits proactively or in response to malicious activity.
CREATE polar_resource_control rc_for_trouble_user max_cpu 10; SET polar_resource_control rc_for_trouble_user FOR USER trouble_user;After you set a resource control for the user
trouble_user, the user's threads can use a maximum of 10% of the total cluster CPU. This effectively prevents the malicious user from consuming excessive CPU resources and affecting other services.Handle sudden slow queries
Unexpected slow queries can sometimes occur in a production environment. They might be caused by incorrect SQL parsing or optimization, which can increase the CPU load. This causes the query to run for a long time and consume significant CPU resources. Although you can terminate the query with the
killcommand, you must wait for the query to reach a specific point in its execution, which is time-consuming. In addition, rolling back the transaction during thekillprocess also consumes CPU resources and takes time. To quickly reduce the CPU load from a slow query or prevent the CPU from being fully utilized, you can apply a CPU usage limit to that query.CREATE polar_resource_control rc_for_slow_query max_cpu 10; SET polar_resource_control rc_for_slow_query FOR query query_id;NoteWhen you set a resource control for a slow query, you can find its
query_idby runningSHOW PROCESSLIST.After you set a resource control for the slow query with the ID
query_id, the query's maximum CPU usage is limited to 10% of the total cluster CPU. This quickly reduces the cluster's CPU load and prevents the query from impacting other production services.A PolarDB cluster can have multiple compute nodes, and different nodes might have queries with the same connection ID. Therefore, when you set a resource control for a query, use the HINT syntax to apply the setting to a specific node.
/*force_node='pi-bpxxxxxxxx'*/ SET polar_resource_control rc_for_slow_query FOR query query_id;Prevent malicious users from triggering cluster scaling in a serverless cluster
When a cluster is in Serverless mode, it automatically scales based on the workload to handle sudden high-load bursts. However, you may want to prevent the cluster from scaling in response to requests from malicious users. In this case, you can apply resource limits to these malicious users on the serverless cluster.
CREATE polar_resource_control rc_for_trouble_user max_cpu 10; SET polar_resource_control rc_for_trouble_user FOR USER trouble_user;Limit CPU usage in multi-tenant or multi-service clusters
In Software as a Service (SaaS) scenarios, multiple tenants often run on the same cluster. It is also common for different business services to share a cluster. In these situations, it is important to limit the resources that a single tenant or service can use. This prevents a user or service from consuming too much CPU and affecting the overall cluster performance. To do this, you can set separate CPU resource limits for each service. For example, you can apply limits per user or per database to achieve this.
CREATE polar_resource_control rc_for_business_1 max_cpu 20; CREATE polar_resource_control rc_for_business_2 max_cpu 20; SET polar_resource_control rc_for_business_1 FOR USER user_1; SET polar_resource_control rc_for_business_2 FOR USER user_2;Based on these settings, the services used by
user_1anduser_2are each limited to a maximum of 20% of the total cluster CPU. Therefore, the two users can use a combined maximum of 40% of the CPU resources. If the workload foruser_1suddenly increases, its CPU usage remains capped at 20%, ensuring that other services on the cluster are not affected.If a single service uses multiple users, you can assign the same resource control to all of them.
CREATE polar_resource_control rc_for_business_1 max_cpu 20; SET polar_resource_control rc_for_business_1 FOR USER user_1; SET polar_resource_control rc_for_business_1 FOR USER user_2;Based on these settings, the service used by
user_1anduser_2is limited to a maximum of 20% of the total cluster CPU. Therefore, the two users can use a combined maximum of 20% of the CPU resources. This prevents the service from exceeding its CPU limit when the workload of one user increases. This ensures fair resource allocation.