Connections and Threads

Connections, Threads, and the Thread Cache

Community MySQL 524 runs one OS thread per connection. max_connections (default 151) caps them, and one extra slot is kept for an account with CONNECTION_ADMIN. With the limit at 20, 30 mysqlslap clients connecting as a plain app user got 11 errors 1040 Too many connections, and Connection_errors_max_connections counted 111 refused attempts. Raising the limit is rarely the fix, since each connection has its own buffers; pool connections in the application instead (Connection Handling).

A disconnecting client's thread waits in the thread cache (up to thread_cache_size) for reuse. The default is 8 + max_connections / 100, so 9 here. Counters read around mysqlslap --concurrency=50 --iterations=20 --query="SELECT 1":

Thread cache reuse under 50 concurrent short connections
thread_cache_size Connections Threads_created Reused
0 1,003 1,003 0%
9 (default) 1,003 829 17%
64 1,003 42 96%

Run time stayed at 0.05 s per iteration, since Linux creates threads cheaply; the cache matters at hundreds of connections per second. If Threads_created climbs in step with Connections, raise it.