Skip to content

Commit 04d67e3

Browse files
committed
PS-9449 Update the Thread pool topic in 8.0
modified: docs/threadpool.md
1 parent 81edcbd commit 04d67e3

File tree

1 file changed

+37
-31
lines changed

1 file changed

+37
-31
lines changed

docs/threadpool.md

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,40 @@
11
# Thread pool
22

3-
You can use thread pooling for MySQL databases to improve performance and scalability. Thread pooling is a technique that reuses a fixed number of threads to handle multiple client connections and execute statements. Thread pooling reduces the overhead of creating and destroying threads, and avoids the contention and context switching that can occur when there are too many threads.
3+
Thread pooling can improve performance and scalability for MySQL databases. This technique reuses a fixed number of threads to handle multiple client connections and execute statements. It reduces the overhead of creating and destroying threads and avoids the contention and context switching that can occur when there are too many threads.
44

5-
MySQL supports thread pooling through the thread pool plugin, which replaces the default one-thread-per-connection model. When a statement arrives, the thread group either begins executing it immediately or queues it for later execution in a round-robin fashion. The high-priority queue consists of a number of thread groups, each of which manages a set of client connections. Each thread group has a listener thread that listens for incoming statements from the connections assigned to the group. The thread pool exposes several system variables that can be used to configure its operation, such as thread_pool_size, thread_pool_algorithm, thread_pool_stall_limit and others.
5+
MySQL supports thread pooling through the thread pool plugin, which replaces the default one-thread-per-connection model. When a statement arrives, the thread group either begins executing it immediately or queues it for later execution in a round-robin fashion. The high-priority queue consists of several thread groups, each managing client connections. Each thread group has a listener thread that listens for incoming statements from the connections assigned to the group. The thread pool exposes several system variables that can be used to configure its operation, such as thread_pool_size, thread_pool_algorithm, thread_pool_stall_limit, and others.
66

7-
The thread pool plugin consists of a number of thread groups, each of which manages a set of client connections. As connections are established, the thread pool assigns them to thread groups in round-robin fashion. For more details, see .
7+
The thread pool plugin consists of several thread groups, each of which manages a set of client connections. As connections are established, the thread pool assigns them to thread groups using the round-robin method. This method assigns threads fairly and efficiently. Here's how it works:
8+
9+
1. The thread pool starts with a set number of thread groups.
10+
11+
2. When a new task arrives, the pool needs to assign it to a group.
12+
13+
3. It does this by going through the groups in order, one by one.
14+
15+
4. Let's say you have four thread groups. The assignment would work like this:
16+
- Task 1 goes to Group 1
17+
- Task 2 goes to Group 2
18+
- Task 3 goes to Group 3
19+
- Task 4 goes to Group 4
20+
- Task 5 goes back to Group 1
21+
22+
5. This pattern continues, always moving to the next group and starting over when it reaches the end.
23+
24+
6. Each group handles its assigned tasks using its available threads.
25+
26+
This round-robin approach spreads work evenly across all groups. It prevents any single group from getting overloaded while others sit idle. This method helps maintain balanced performance across the system.
827

928
MySQL executes statements using one thread per client connection. When the number of connections increases past a specific point, performance degrades.
10-
This feature introduces a dynamic thread pool, which enables the server to keep the top performance even with a large number of client connections. The server decreases the number of threads using the thread pool and reduces the context switching and hot locks contentions. The thread pool is most effective with `OLTP` workloads (relatively short CPU-bound queries).
29+
This feature introduces a dynamic thread pool, which enables the server to maintain top performance even with a large number of client connections. The server decreases the number of threads using the thread pool and reduces the context switching and hot lock contentions. The thread pool is most effective with `OLTP` workloads (relatively short CPU-bound queries).
1130

1231
Set the thread pool variable [thread_handling](#thread_handling) to `pool-of-threads` by adding the following line to `my.cnf`:
1332

1433
```text
1534
thread_handling=pool-of-threads
1635
```
1736

18-
Although the default values for the thread pool should provide good performance, perform additional tuning with the dynamic system variables. The goal is to minimize the number of open transactions on the server. Short-running transactions commit faster and deallocate server resources and locks.
37+
Although the default values for the thread pool should provide good performance, additional tuning should be performed with the dynamic system variables. The goal is to minimize the number of open transactions on the server. Short-running transactions commit faster and deallocate server resources and locks.
1938

2039
Due to the following differences, this implementation is not compatible with upstream:
2140

@@ -25,7 +44,7 @@ Due to the following differences, this implementation is not compatible with ups
2544

2645
Priority Queue:
2746

28-
This is a queue that assigns a priority to each data element and processes them according to their priority. The data element with the highest priority is served first, regardless of its order in the queue. A priority queue can be implemented using an array, a linked list, a heap or a binary search tree. A priority queue can also be ascending or descending, meaning that the highest priority is either the smallest or the largest value.
47+
A queue that assigns a priority to each data element and processes them according to their priority. The data element with the highest priority is served first, regardless of its order in the queue. A priority queue can be implemented using an array, a linked list, a heap, or a binary search tree. It can also be ascending or descending, meaning that the highest priority is either the smallest or the largest value.
2948

3049
## Version specific information
3150

@@ -45,27 +64,24 @@ The thread pool adds the connection to the high-priority queue and decrements th
4564

4665
Otherwise, the variable adds the connection to the low-priority queue with the initial value.
4766

48-
The thread pool checks the high-priority queue for the next connection each time. The thread pool picks connections from the low-priority queue when the high-priority queue is empty. The default behavior is to put events from already started transactions into the high-priority queue.
67+
Each time, the thread pool checks the high-priority queue for the next connection. When the high-priority queue is empty, the thread pool picks connections from the low-priority queue. The default behavior is to put events from already started transactions into the high-priority queue.
4968

50-
If the value equals `0`, all connections are put into the low-priority queue. Each connection could be put into a high-priority queue if the value exceeds zero.
69+
If the value equals `0`, all connections are put into the low-priority queue. If the value exceeds zero, each connection could be put into a high-priority queue.
5170

5271
The [thread_pool_high_prio_mode](#thread_pool_high_prio_mode) variable prioritizes all statements for a connection or assigns connections to the low-priority queue. To implement this new [thread_pool_high_prio_mode](#thread_pool_high_prio_mode) variable
5372

54-
5573
## Low-priority queue throttling
5674

57-
One case that can limit thread pool performance and even lead to deadlocks under high concurrency is when thread groups are oversubscribed due to active threads reaching the oversubscribe limit. Still, all/most worker threads are waiting on locks currently held by a transaction from another connection not currently in the thread pool.
75+
One case that can limit thread pool performance and even lead to deadlocks under high concurrency is when thread groups are oversubscribed due to active threads reaching the oversubscribe limit. Still, all/most worker threads are waiting on locks currently held by a transaction from another connection that is not currently in the thread pool.
5876

5977
In this case, the oversubscribe limit does not account for those threads in the pool that marked themselves inactive. As a result, the number of threads (both active and waiting) in the pool grows until it hits the [`thread_pool_max_threads`](#thread_pool_max_threads) value. If the connection executing the transaction holding the lock has managed to enter the thread pool by then, we get a large (depending on the [`thread_pool_max_threads`](#thread_pool_max_threads) value) number of concurrently running threads and, thus, suboptimal performance. Otherwise, we get a deadlock as no more threads can be created to process those transaction(s) and release the lock(s).
6078

61-
Such situations are prevented by throttling the low-priority queue when the total number of worker threads (both active and waiting ones) reaches the oversubscribe limit. If there are too many worker threads, do not start new transactions and create new threads until queued events from the already started transactions are processed.
79+
Such situations are prevented by throttling the low-priority queue when the total number of worker threads (both active and waiting ones) reaches the oversubscribe limit. If there are too many worker threads, do not start new transactions; create new threads until queued events from the already-started transactions are processed.
6280

6381
## Handling long network waits
6482

6583
Specific workloads (large result sets, BLOBs, slow clients) can wait longer on network I/O (socket reads and writes). Whenever the server waits, this should be communicated to the thread pool so it can start a new query by either waking a waiting thread or sometimes creating a new one. This implementation has been ported from *MariaDB* patch MDEV-156.
6684

67-
68-
6985
## System variables
7086

7187
### `thread_handling`
@@ -111,7 +127,7 @@ The following values are allowed:
111127

112128
* `statements`. In this mode, all individual statements go into the high-priority queue, regardless of the transactional state and the number of available high-priority tickets. Use this value to prioritize `AUTOCOMMIT` transactions or other statements, such as administrative ones. Setting this value globally essentially disables high-priority scheduling. All connections use the high-priority queue.
113129

114-
* `none`. This mode disables the priority queue for a connection. Certain types of connections, for example, monitoring, are insensitive to execution latency and do not allocate the server resources that would impact the performance of other connections. These types of connections do not require high-priority scheduling. Setting this value globally essentially disables high-priority scheduling. All connections use the low-priority queue.
130+
* `none`. This mode disables the priority queue for a connection. Certain types of connections, such as monitoring, are insensitive to execution latency and do not allocate the server resources that would impact the performance of other connections. These types of connections do not require high-priority scheduling. Setting this value globally essentially disables high-priority scheduling. All connections use the low-priority queue.
115131

116132
### `thread_pool_high_prio_tickets`
117133

@@ -137,7 +153,7 @@ This variable controls the high-priority queue policy. Assigns the selected numb
137153
| Data type: | Numeric |
138154
| Default value: | 100000 |
139155

140-
This variable can limit the maximum number of threads in the pool. The server does not create new threads when the limit is reached.
156+
This variable can limit the maximum number of threads in the pool. When the limit is reached, the server does not create new threads.
141157

142158
### `thread_pool_oversubscribe`
143159

@@ -176,19 +192,14 @@ Defines the number of threads that can use the CPU simultaneously.
176192
| Data type: | Numeric |
177193
| Default value: | 500 (ms) |
178194

179-
Defines the number of milliseconds before a running thread is considered stalled. The thread pool will wake up or create another thread when this limit is reached. This variable prevents a long-running query from monopolizing the pool.
180-
181-
### Upgrading from a version before 8.0.14 to 8.0.14 or higher
182-
183-
195+
Defines the number of milliseconds before a running thread is considered stalled. When this limit is reached, the thread pool will wake up or create another thread. This variable prevents a long-running query from monopolizing the pool.
184196

185197
### `extra_port`
186198

187199
The variable was removed in [Percona Server for MySQL 8.0.14](release-notes/Percona-Server-8.0.14.md).
188200

189-
Specifies an additional port that Percona Server for MySQL listens on. This can be used in case no new connections can be established
190-
due to all worker threads being busy or being locked when `pool-of-threads`
191-
feature is enabled.
201+
It specifies an additional port that Percona Server for MySQL listens to. This port can be used in case no new connections can be established
202+
due to all worker threads being busy or being locked when the `pool-of-threads` feature is enabled.
192203

193204
The following command connects to the extra port:
194205

@@ -207,11 +218,11 @@ mysql --port='extra-port-number' --protocol=tcp
207218
| Data type: | Numeric |
208219
| Default value: | 1 |
209220

210-
The varible was removed in [Percona Server for MySQL 8.0.14](release-notes/Percona-Server-8.0.14.md). This variable can be used to specify the maximum allowed number of connections
211-
plus one extra `SUPER` users connection on the extra_port. This
221+
The variable was removed in [Percona Server for MySQL 8.0.14](release-notes/Percona-Server-8.0.14.md). This variable can be used to specify the maximum allowed number of connections
222+
plus one extra `SUPER` user connection on the extra_port. This
212223
can be used with the extra_port variable to access the server in
213224
case no new connections can be established due to all worker threads being busy
214-
or being locked when `pool-of-threads` feature is enabled.
225+
or being locked when the `pool-of-threads` feature is enabled.
215226

216227
## Status variables
217228

@@ -233,8 +244,3 @@ This status variable shows the number of idle threads in the pool.
233244

234245
This status variable shows the number of threads in the pool.
235246

236-
## Other reading
237-
238-
* [Thread pool in MariaDB 5.5](https://kb.askmonty.org/en/threadpool-in-55/)
239-
240-
* [Thread pool implementation in Oracle MySQL](https://mikaelronstrom.blogspot.com/2011_10_01_archive.html)

0 commit comments

Comments
 (0)