MySQL runs on threads, not processes. Threads fall into 2 groups by role: foreground threads, which correspond to user sessions, and background threads, which handle internal processing.
The number of background threads can vary with the MySQL Server version and settings, and when work runs in parallel, 2 or more threads with the same name may appear.
📢 MySQL uses the traditional thread model, but MySQL Enterprise Edition and Percona Server for MySQL include a Thread Pool plugin. In the community edition, the **thread model** uses one thread per client connection to execute statements. So the more clients connect to the server and run statements, the more overall performance degrades. The **Thread Pool plugin**, on the other hand, provides an alternative thread-handling model designed to reduce overhead and improve performance. It improves server performance by efficiently managing statement-execution threads for large numbers of client connections, especially on modern multi-CPU/core systems.
Foreground Thread(Client Thread)
There are at least as many foreground threads as there are clients connected to MySQL Server, and each one processes the queries requested by its client user.
When a user finishes work and the session ends, the thread is returned to the Thread Cache. If the Thread Cache already holds more than a certain number of waiting threads, the thread is terminated so that only that number of threads stays in the Thread Cache.
📢 The maximum number of threads kept in the Thread Cache is set with the system variable thread_cache_size
A foreground thread normally takes a user request (query) and fetches the data it needs, but its role and scope can differ depending on the storage engine in use.
For example, for MyISAM tables the foreground thread handles all disk I/O work, while for InnoDB tables the foreground thread handles only reads and writes in memory. (Background threads handle the rest.)
Background Thread
Among MySQL's storage engines, InnoDB has background threads perform much of the work.
Data reads are mostly handled by foreground threads, so there is no need to configure many read threads. Writes, however, are mostly handled by background threads, so it is a good idea to configure enough write threads.
📢 In older versions, the Master Thread performed many roles, so concurrency was inevitably low. Now each job is separated by role and handled by a different thread. For example, flushing dirty pages in the buffer pool is done by the Page Cleaner Thread, and physically removing rows deleted by a DELETE query is done by the Purge Thread.
Main Background Threads of the InnoDB Engine
Thread Type | Name | Description |
Master Thread | thread/innodb/srv_master_trhead | • Manages background threads and schedules various tasks.
• Flushes data in the buffer pool to disk asynchronously to guarantee data consistency. |
Insert Buffer Thread | thread/innodb/io_ibuf_thread | Merges the Insert Buffer (Change Buffer). |
Log Thread | thread/innodb/io_log_thread | Writes the transaction log (redo log). |
Read Thread | thread/innodb/io_read_thread | Handles various types of read requests. |
Write Thread | thread/innodb/io_write_thread | Handles various types of write requests. |
Page Cleaner Thread | thread/innodb/page_cleaner_thread | Flushes dirty pages in the buffer pool to disk. |
Purge Thread | thread/innodb/srv_purge_thread | Removes rows that are no longer needed for rollback. |
How to Check InnoDB Thread Status
Main InnoDB System Variables (Background Threads)
System Variable | Default Value | Range | Description |
innodb_read_io_threads | 4 | 1 - 64 | Number of I/O threads for reads. |
innodb_write_io_threads | 4 | 1 - 64 | Number of I/O threads for writes. |
innodb_page_cleaners | 4 | 1 - 64 | Number of Page Cleaner threads that flush dirty pages from the buffer pool. |
innodb_purge_threads | 4 | 1 - 32 | Number of purge threads assigned to purge work. |
Related Articles
