TCMalloc and MySQL

Over the last month or so, we noticed steady week-over-week rises in overall MySQL query time. A poorly-performing database makes for a slow site and a slow site makes for…

Ted Nyman
3 min readintermediate
--
View Original

Overview

The article discusses the performance issues encountered with MySQL and the subsequent resolution through the implementation of TCMalloc as a memory allocator. It highlights the investigation process, the identification of kernel mutex contention, and the significant performance improvements achieved post-implementation.

What You'll Learn

1

How to diagnose MySQL performance issues using open source tools

2

Why switching memory allocators can improve database performance

3

How to implement TCMalloc in a MySQL environment

Prerequisites & Requirements

  • Understanding of MySQL performance metrics and common issues
  • Familiarity with Percona tools such as pt-stalk(optional)

Key Questions Answered

What were the performance issues observed in MySQL?
The performance issues included steady week-over-week rises in overall MySQL query time, primarily due to kernel mutex contention in MySQL versions 5.1 and 5.5. The investigation revealed that the running thread count was excessively high, leading to increased wait times for transactions.
How did switching to TCMalloc affect MySQL performance?
After switching to TCMalloc, the frequency of pt-stalk triggers declined to about 1/7th of their original rate, resulting in a 30 percent improvement in overall query performance. This change significantly reduced contention and improved the database's responsiveness.
What tools were used to diagnose MySQL issues?
The investigation utilized open source tools from Percona, specifically pt-stalk, to monitor MySQL's running thread count and trigger alerts for diagnostic data collection. This helped identify the underlying performance issues effectively.
What specific mutex contention issue was identified?
The article identified kernel mutex contention around transactions in MySQL, particularly related to the kernel_mutex, which was causing threads to wait excessively, thus degrading performance.

Key Statistics & Figures

Reduction in pt-stalk triggers
1/7th of their original frequency
This reduction occurred after implementing TCMalloc, indicating a significant decrease in contention.
Improvement in query performance
30 percent
This improvement was observed across the board after switching to TCMalloc.

Technologies & Tools

Some links below are affiliate links. We may earn a commission if you make a purchase.

Database
Mysql
Used as the primary database system experiencing performance issues.
Memory Allocator
Tcmalloc
Implemented to reduce lock contention and improve performance in MySQL.
Tools
Percona Toolkit
Used for monitoring and diagnosing MySQL performance issues.

Key Actionable Insights

1
Regularly monitor MySQL performance metrics to identify potential issues early.
Using tools like pt-stalk can help catch performance degradation before it impacts user experience, allowing for timely interventions.
2
Consider alternative memory allocators like TCMalloc for multi-threaded applications.
If your application experiences high contention and performance bottlenecks, switching allocators can lead to significant improvements, as demonstrated in this case.
3
Document your findings and solutions for future reference.
Keeping a record of performance issues and their resolutions can aid in troubleshooting similar problems in the future and help onboard new team members.

Common Pitfalls

1
Failing to investigate underlying causes of performance issues can lead to ineffective solutions.
It's crucial to thoroughly analyze performance metrics and system behavior before implementing changes to ensure that the root cause is addressed.
2
Overlooking the impact of memory allocation on multi-threaded applications.
Many developers may not realize how traditional memory allocators can introduce contention, leading to performance bottlenecks in applications with high concurrency.

Related Concepts

Mysql Performance Tuning
Memory Allocation Strategies
Thread Contention In Databases