Thread Pool

Updated at:

To achieve optimal performance for PolarDB for MySQL, PolarDB provides the thread pool feature, which separates threads from sessions. Many sessions can be open at the same time while only a small number of threads handle the tasks of active sessions.

Benefits

By default, MySQL uses session exclusive mode, in which each session creates a dedicated thread. When many sessions exist, heavy resource competition degrades performance. Extensive system thread scheduling and cache misses also cause a sharp performance drop.

PolarDB's thread pool implements priority and concurrency control mechanisms for different types of SQL operations. It keeps the number of connections near the optimal level, which enables PolarDB databases to maintain high performance under high connection counts and high concurrency. The thread pool provides the following benefits:

  • When many threads run concurrently, the thread pool automatically keeps the number of concurrent threads within a reasonable range, which prevents excessive thread scheduling and cache misses.

  • When many transactions run concurrently, the thread pool assigns statements and transactions to different priorities and controls the concurrency of statements and transactions separately, which reduces resource competition.

  • The thread pool gives management SQL statements a higher priority so that these statements run first. New connections, management operations, and monitoring therefore remain stable even when the system load is high.

  • The thread pool gives complex query SQL statements a relatively lower priority and limits their maximum concurrency. This prevents too many complex SQL statements from exhausting system resources and making the entire database service unavailable.

Use the thread pool

The thread pool feature includes the following parameters, which you can modify in the console. For more information, see Set cluster parameters and node parameters.

Note

Cluster parameters in the PolarDB console carry the MySQL configuration file compatibility prefix loose_. To modify parameters in the PolarDB console, modify the parameters that have the loose_ prefix.

Parameter

Description

loose_thread_pool_enabled

Specifies whether to enable the thread pool. Valid values:

  • ON: enables the thread pool.

  • OFF: disables the thread pool.

Note
  • The default value of this parameter varies by version.

    • MySQL 5.6: The default value is OFF.

    • MySQL 5.7, MySQL 8.0.1, and MySQL 8.0.2: The default value is ON.

  • Enabling or disabling the thread pool does not require a cluster restart.

loose_thread_pool_size

The number of thread groups in the thread pool. The value range depends on the number of CPU cores in the primary node specifications of the cluster.

Value range: DBNodeClassCPU~DBNodeClassCPU*10. Default value: DBNodeClassCPU*2.

Note
  • DBNodeClassCPU is an integer that represents the number of CPU cores in the primary node specifications.

  • MySQL 5.7 has a default value of DBNodeClassCPU.

Examples:

  • With Database engine set to MySQL 8.0.1 in the Cluster Edition series, if the primary node specifications are 4 cores and 8 GB, the value range and default value are 4~40 and 8.

  • With Database engine set to MySQL 8.0.1 in the Multi-master Cluster (Limitless) Edition series, if both primary nodes have specifications of 4 cores and 8 GB, the value range and default value are 8~80 and 16.

  • With Database engine set to MySQL 5.7 in the Cluster Edition series, if the primary node specifications are 4 cores and 8 GB, the value range and default value are 4~40 and 4.

loose_thread_pool_high_prio_mode

The high-priority queue mode of the thread pool. Valid values:

  • transactions (default): SQL statements in an open transaction are added to the high-priority queue and receive thread_pool_high_prio_tickets tickets. All SQL statements that run next are placed in the high-priority queue until the tickets are exhausted.

  • statements: All SQL statements are added to the high-priority queue.

  • none: No SQL statements are added to the high-priority queue.

Note

Applies to:

  • MySQL 5.6

  • MySQL 5.7

loose_thread_pool_high_prio_tickets

The number of tickets granted at a time for the high-priority queue.

Value range: 0~4294967295. Default value: 4294967295.

Note

Applies to:

  • MySQL 5.6

  • MySQL 5.7

loose_thread_pool_idle_timeout

The time threshold for releasing idle threads in the thread pool. An idle thread that has not served any request beyond this threshold is released.

Value range: 0~31536000. Default value: 60. Unit: seconds.

Note

Applies to:

  • MySQL 5.6

  • MySQL 5.7

loose_thread_pool_oversubscribe

The number of active threads allowed in each thread group. An active thread is a thread that is executing an SQL statement.

Value range: 1~1000. Default value: 20.

loose_thread_pool_stall_limit

The time threshold used to determine whether the thread pool has entered a stalled state.

When the thread pool enters a stalled state, the system creates new threads to serve SQL statements.

Value range: 1~18446744073709551615. Default value: 10. Unit: milliseconds.

Note

MySQL 5.6 has a default value of 30 milliseconds.

loose_bypass_thread_pool_ips

Configures the client IP addresses that are not restricted by the thread pool. Even when the thread pool is full, SQL statements can be run to perform management operations.

Configuration example:

10.69.96.16,10.69.96.17
Note

Applies to: MySQL 8.0.1 8.0.1 with revision 8.0.1.1.19 or later.

loose_bypass_thread_pool_check_ignore_proxy

Specifies whether to ignore the IP addresses of clients that connect through Proxy when client IP addresses are detected by using loose_bypass_thread_pool_ips. Valid values:

  • ON (default): For the IP addresses of clients that connect through Proxy, even if loose_bypass_thread_pool_ips includes the client IP address, the client remains subject to thread pool restrictions.

  • OFF: For the IP addresses of clients that connect through Proxy, if loose_bypass_thread_pool_ips includes the client IP address, the client is not subject to thread pool restrictions.

Note

Applies to: MySQL 8.0.1 with revision 8.0.1.1.19 or later.

loose_thread_pool_high_priority_users

Configures high-priority database accounts. After configuration, requests from these accounts are placed in the high-priority queue of the thread pool and processed first.

Configuration example:

user1, user2
Note
  • Applies to:

    • MySQL 8.0.1 with revision 8.0.1.1.19 or later.

    • MySQL 8.0.2 with revision 8.0.2.2.12 or later.

  • After you configure this parameter, it takes effect only for new database connections.

  • Do not configure too many high-priority accounts.

loose_thread_pool_mark_ddl_thread_timeout_sec

Configures the thread pool timeout threshold for DDL statements. After this threshold is exceeded, DDL is marked as timed out and the system creates new threads to process requests.

Value range: 0~864000. Default value: 600. Unit: seconds.

Note

Applies to: MySQL 8.0.1 with revision 8.0.1.1.19 or later.

loose_thread_pool_mark_ddl_thread_timeout_immediately

Specifies whether to immediately mark DDL as timed out when the thread pool is under high load and the low-priority queue is backlogged. The system then creates new threads to process requests. This parameter is suitable for workloads that require frequent batch DDL operations. Valid values:

  • ON: enables this feature.

  • OFF (default): disables this feature.

Note

Applies to: MySQL 8.0.1 with revision 8.0.1.1.19 or later.

Query the thread pool status

You can run the following command to query the thread pool status:

select * from information_schema.THREAD_POOL_STATUS;

The following sample output is returned:

mysql> select * from information_schema.THREAD_POOL_STATUS;
+----+--------------+---------------------+----------------------+-------------------+---------------------------+------------------+-----------------+------------------+
| ID | THREAD_COUNT | ACTIVE_THREAD_COUNT | WAITING_THREAD_COUNT | DUMP_THREAD_COUNT | SLOW_THREAD_TIMEOUT_COUNT | CONNECTION_COUNT | LOW_QUEUE_COUNT | HIGH_QUEUE_COUNT |
+----+--------------+---------------------+----------------------+-------------------+---------------------------+------------------+-----------------+------------------+
|  0 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
|  1 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
|  2 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
|  3 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
|  4 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
|  5 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
|  6 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
|  7 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
|  8 |            1 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
|  9 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 10 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 11 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 12 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 13 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 14 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 15 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 16 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 17 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 18 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 19 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 20 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 21 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 22 |            2 |                   1 |                    0 |                 0 |                         0 |                1 |               0 |                0 |
| 23 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 24 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 25 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 26 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 27 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 28 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 29 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 30 |            2 |                   0 |                    0 |                 0 |                         0 |                1 |               0 |                0 |
| 31 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 32 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 33 |            2 |                   0 |                    0 |                 0 |                         0 |                1 |               0 |                0 |
| 34 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 35 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 36 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 37 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 38 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 39 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 40 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 41 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 42 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 43 |            1 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 44 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 45 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 46 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 47 |            3 |                   1 |                    0 |                 0 |                         0 |                1 |               0 |                0 |
| 48 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 49 |            3 |                   1 |                    0 |                 0 |                         0 |                1 |               0 |                0 |
| 50 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 51 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 52 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 53 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 54 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 55 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 56 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 57 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 58 |            1 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 59 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 60 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 61 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 62 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
| 63 |            2 |                   0 |                    0 |                 0 |                         0 |                0 |               0 |                0 |
+----+--------------+---------------------+----------------------+-------------------+---------------------------+------------------+-----------------+------------------+
64 rows in set (0.00 sec)

The following table describes the parameters.

Parameter

Description

ID

The thread pool ID.

THREAD_COUNT

The number of threads in the thread pool.

ACTIVE_THREAD_COUNT

The number of active threads in the thread pool.

WAITING_THREAD_COUNT

The number of threads in the thread pool that are waiting for disk I/O or a transaction commit.

DUMP_THREAD_COUNT

The number of DUMP-type persistent connections in the thread pool.

SLOW_THREAD_TIMEOUT_COUNT

The number of threads in the thread pool that are marked as timed out.

CONNECTION_COUNT

The number of established user connections in the thread pool.

LOW_QUEUE_COUNT

The number of requests waiting in the low-priority queue of the thread pool.

HIGH_QUEUE_COUNT

The number of requests waiting in the high-priority queue of the thread pool.

Sysbench tests

The following figures compare performance with the thread pool enabled and disabled. The test results show that the thread pool provides clear performance benefits under high concurrency.

Figure 1. OLTP non-indexed update testOLTP non-indexed update

Figure 2. OLTP write-only testOLTP write-only

Figure 3. OLTP read-only testOLTP read-only

Figure 4. OLTP read/write testOLTP read/write test

Troubleshoot a full thread pool

If the database experiences connection timeouts or queued requests, check whether the thread pool is full.

  1. Log on to the PolarDB console and go to the details page of the target cluster.

  2. Choose Performance Monitoring > Advanced Monitoring > Database Metrics > Thread Pool.

  3. Compare thread_pool_running_threads (the current number of running threads) with tp_thread_count (the total thread capacity of the thread pool). If thread_pool_running_threads remains close to tp_thread_count, the thread pool is full.

  4. Check the tp_low_queue_count and tp_high_queue_count metrics. A backlog in either queue indicates that requests are queued and the thread pool is full.