MariaDB thread_cache_size Explained: Thread Reuse, Performance, and Best Practices

Published: 2026-07-15

Introduction

The thread_cache_size system variable specifies how many idle worker threads MariaDB keeps in memory for reuse.

Whenever a client connects to MariaDB, the server needs a worker thread to process requests. Creating and destroying threads for every new connection consumes CPU resources. By keeping previously used threads in a cache, MariaDB can reuse them for future connections, reducing overhead and improving performance.

Although thread creation is relatively fast on modern operating systems, thread caching becomes increasingly beneficial on busy servers that handle frequent short-lived connections.

This guide explains how thread_cache_size works, how to configure it, recommended values, and common configuration mistakes.

Test Environment

Operating System : AlmaLinux 10
PHP Version      : PHP 8.3
Web Server       : Nginx
PHP-FPM          : Enabled
Architecture     : x86_64
Last Tested      : July 2026

What Is thread_cache_size?

The thread_cache_size variable controls how many completed worker threads MariaDB keeps available for reuse.

View the current value:

SHOW VARIABLES LIKE 'thread_cache_size';

Example output:

+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| thread_cache_size | 100   |
+-------------------+-------+
[sty]
Instead of destroying a thread after a client disconnects, MariaDB can place it into a cache for future use.
## How It Works
Without thread caching:
[sty]
Client
   │
   ▼
Create Thread
   │
   ▼
Execute Query
   │
   ▼
Destroy Thread

With thread caching:

Client
   │
   ▼
Reuse Cached Thread
   │
   ▼
Execute Query
   │
   ▼
Return Thread to Cache

Reusing existing threads reduces CPU overhead, especially when many clients connect and disconnect frequently.

Check the Current Value

View the current setting:

SHOW VARIABLES LIKE 'thread_cache_size';

To monitor thread activity:

SHOW STATUS LIKE 'Threads_created';

Example:

+-----------------+------+
| Variable_name   | Value|
+-----------------+------+
| Threads_created | 256  |
+-----------------+------+

If Threads_created increases rapidly over time, MariaDB is frequently creating new threads rather than reusing cached ones.

Change thread_cache_size

Edit the MariaDB configuration file.

sudo nano /etc/my.cnf

Example:

[mysqld]

thread_cache_size = 100

Save the file.

Restart MariaDB

Apply the changes.

sudo systemctl restart mariadb

Verify the service:

sudo systemctl status mariadb

Expected output:

Active: active (running)

Verify the New Value

SHOW VARIABLES LIKE 'thread_cache_size';

Recommended Values

| Server Type       | Suggested Value |
| ----------------- | --------------- |
| Development       |              16 |
| Small Website     |              32 |
| WordPress         |              64 |
| Medium Production |             100 |
| High Traffic      |         100–256 |

The optimal value depends on how often clients connect and disconnect.

Relationship with max_connections

These two variables are often tuned together.

Variable Purpose

max_connections Maximum simultaneous client connections

thread_cache_size Number of reusable worker threads

For example:

max_connections = 300

thread_cache_size = 100

MariaDB can accept up to 300 concurrent connections, while keeping up to 100 idle worker threads ready for reuse.

Common Issues

Threads_created Increases Quickly

Check:

SHOW STATUS LIKE 'Threads_created';

A rapidly increasing value may indicate that the thread cache is too small for the workload.

Setting thread_cache_size Too High

Example:

thread_cache_size = 1000

Keeping a very large number of idle threads consumes memory without improving performance if the workload does not require them.

Assuming It Improves Query Speed

thread_cache_size does not make SQL statements execute faster.

It only reduces the overhead of creating worker threads for new client connections.

Query performance is more closely related to:

Index design

SQL optimization

innodb_buffer_pool_size

Storage performance

Best Practices

Monitor Threads_created before changing the setting.

Increase the cache gradually rather than making large jumps.

Tune thread_cache_size together with max_connections.

Avoid configuring an unnecessarily large cache.

Review connection patterns periodically as workloads change.

Related Articles

MariaDB Explained

MariaDB max_connections Explained

MariaDB wait_timeout Explained

MariaDB connect_timeout Explained

MariaDB innodb_buffer_pool_size Explained

Conclusion

The thread_cache_size variable allows MariaDB to reuse worker threads instead of creating new ones for every client connection. On servers with frequent connection activity, an appropriately sized thread cache can reduce CPU overhead and improve connection handling efficiency.

Rather than simply choosing a large value, monitor thread creation statistics and adjust the cache according to your workload. Combined with properly configured connection limits and timeout settings, thread_cache_size contributes to a more efficient and responsive MariaDB server.

Explore More

Technology Guides →

MariaDB Explained: Essential Configuration Variables and Best Practices

MariaDB max_connections Explained: Configuration, Limits, and Best Practices

MariaDB innodb_buffer_pool_size Explained: Memory, Performance, and Best Practices

MariaDB max_allowed_packet Explained: Configuration, Packet Size, and Best Practices

Southeast Asia Insights →

Best Areas to Live in Bangkok in 2026: A Complete Guide for Expats and Digital Nomads

Best Coworking Spaces in Bangkok 2026: A Guide for Digital Nomads and Remote Workers

Why Do People in Southeast Asia Love Iced Drinks? The Climate, Culture, and Science Explained

Best eSIM for Southeast Asia in 2026: Compare the Top Travel eSIMs