Posted on

Threads vs Processes

You do “ps xa | grep mysqld” and see lots of lines… looks like heaps of processes.
Newsflash: they aren’t. On closer investigation, you will find that each of the processes appears to be using the exact same amount of RAM, too… what a coincidence 😉

What are in fact looking at a Linux 2.4 kernel which generally makes threads look like processes. If you run MySQL on a 2.6 kernel (which has NTPL, the new threading), you will properly see one mysqld process and a number of threads. Of course, if you use a backported NTPL on a 2.4 kernel, all those “processes” also magically disappear.

But, but… isn’t it possible that…?
No! Really. If you wish to take a peek at the MySQL server source code, you will note that there is not a single fork() call in there. MySQL is single process, multi-threaded. You will always see a minimum of three threads running, more if you use InnoDB or for instance a replication slave.

Processes have their own heap (memory space), whereas threads share this space. The advantage of threads is that no inter-process communication (IPC) is required, you just need to take care during access of shared areas by using mutexes. Provided the coding is done right, it is much more efficient. And MySQL was specifically designed to work this way. It also uses less RAM.

Most daemons and apps tend to fork rather than thread, and of course most apps use less memory than a database server, so you tend to not spot it. Apache 2, of course, can use threading (in addition to forking).

Another side-effect of this “lack of exposure” of threads is that some threading libraries have bugs. If you build a MySQL server from source, you need to make sure that your thread library is okidoki. If it isn’t, the server will pass the test suite, but will get into trouble under load. A typical symptom is 99% CPU usage. See the Linux platform notes in the MySQL manual for details. But it also applies to some other platforms like FreeBSD. Generally, I’d recommend using a binary from mysql.com rather than compiling your own, if one is available.

Posted on