Databases 3 min read

Boost MySQL Connection Performance with Thread Cache: How It Works & Tuning Tips

MySQL uses a Thread Cache pool to store idle connection threads, dramatically speeding up client connection creation; understanding parameters like thread_cache_size and thread_stack, monitoring cache hit rates, and proper tuning can keep the hit rate around 90% for optimal performance.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Boost MySQL Connection Performance with Thread Cache: How It Works & Tuning Tips

MySQL improves the performance of the “client request to create a connection” process by using a Thread Cache pool that stores idle connection threads instead of destroying them after each request.

When a new connection request arrives, MySQL first checks the Thread Cache pool; if an idle thread is available it is reused, otherwise a new thread is created.

Related parameters

thread_cache_size : the maximum number of idle connection threads that the Thread Cache can hold. Threads are added to the cache as they are created and used, and once the cache reaches this size MySQL stops adding more idle threads.

thread_stack : the amount of memory allocated for each connection thread’s stack when it is created. The default value (192 KB) is suitable for most environments, and the size should not be changed unless you are very familiar with MySQL’s thread handling.

To monitor the cache, view the related status variables (see the image below).

Thread Cache status
Thread Cache status

Thread Cache hit rate is the ratio of connections obtained from the cache to the total number of connections received by the system:

Threads_Cache_Hit = (Connections - Threads_created) / Connections * 100%

After the system has been running stably for a while, the Thread Cache hit rate should generally stay around 90 %.

MySQLThread Cachehit rateThread Stackconnection performancethread_cache_size
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.