Maggie Nelson

To persist or not to persist?

by maggie on Aug.21, 2008, under entry

Recently, in a new PHP + MySQL application I’m building, I was faced with a choice of using persistent connections. The benefit of persistent connections is that you don’t have to keep opening new connections every time you need to hit the database for something. There’s a connection already waiting for you. Yay, right? Well, with MySQL, connecting is actually really really cheap, and frankly, if you are using persistent connections, you might encounter some issues with Apache going zombie on processes that use a connection, effectively taking that connection out of use. Grrr.
“To Google!”, I said. “The Interwebs must have a solution for this!” I’ve searched and searched trying to find a definite answer about whether to use or not to use persistent connections in a LAMP application. There seem to be lots of differing opinions, namely this famous presentation by Rasmus which basically “proves” that persistent connections are awesome. However, the PHP community seemed to be pretty against them, despite that evidence.
At this point, it was just annoying. Do I use persistent connections or not? Having this conversation internally with Ben Ramsey and Robert Swarthout, it seemed a good idea to have some hard numbers to back up our claims, so we wouldn’t have to say “well, guy x said so” as part of the argument.
Here are the results of Robert’s tests: Benchmarking of MySQL Persistent Connections vs Non-Persistent Connections. As Robert says:

Basically what the numbers above shows us is that in an isolated environment it makes no difference which connection type you are going to use.

Jay Pipes says that “if you use Apache, Apache can zombie a PHP process and cause the mysql connection to be held until the mysql server restarts…” Given the risk, the possible management overhead and the results of the test, we’re not going to use persistent connections on this upcoming project.

:
4 comments for this entry:
  1. Richard Harrison

    My experience with persistent connections is from years ago (circa 2001) but the problem we found was that on a very busy server the Apache processes would get so numerous that they would suck up all of the available MySQL connections and then the next Apache process was screwed because it’s connection would then get refused.
    This was mostly because Apache was used to serve all static content, so a process serving a static image would still have it’s own persistent connection.

  2. Carsten Pedersen

    Good run-down of the issues and documentation available. I’ve been teaching “don’t bother with persistent connections” for years, but haven’t had a good source. Now I do. Thanks!

  3. Daniel Smith

    Maggie,
    Don’t forget that with persistent connections, MySQL won’t know to drop temporary tables if you use them in the connection. It’s bit me in the past. Good luck making your decision!
    Daniel

  4. M. van der Klip

    Actually, I’d say it depends on the use case. We’re running some ad delivery software which does about 2k inserts per second at peak times. We have been running this without persistent connections for a long time under Apache. We had long learned that enabling persistent connections under Apache is a no-go.
    However when we made the switch from Apache to lighttpd several years ago, we got loads more control of the number of running PHP processes. Previously we were running hundreds of Apache processes on one single webserver, which got replaced by one single lighttpd process and a fixed amount of 20 PHP FastCGI processes.
    Recently we decided to evaluate persistent connections again because we realized that the math had became easy: servers * 20 = max_connections. The number of connections to our MySQL server is now constant and the system load has gone down by about 20%.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Tags

RSS Planet PHP

  • Continuous Integration und Cruise Control im Projekteinsatz -> Vortrag@Mayflower-München
    Am kommenden Donnerstag, den 11.02.2010 findet wieder ein öffentlicher Vortrag im Mayflower Büro in München statt (Mannhardtstraße 6, S-Bahn Isartor). Beginn ist um 18:00 Uhr, Thema des Vortrags ist "Continuous Integration und Cruise Control im Projekteinsatz" Sebastian Springer zeigt wie man Continuous Integration in PHP-basierten Entwicklungspr […]
    ThinkPHP /dev/blog - PHP
  • Coding Is The Easy Part
    Writing code is easy. Instantiating objects, calling methods, memorising functions (or using the documentation instead); these are all trivial tasks that we have all taken time to study and practise. But engineering software is so much more than coding. As a software engineer you take on several roles throughout the software development life cycle. Let us ta […]
    TechPortal
  • Troubleshoot – My PHP script is timing out
    I hear a lot about the problem where web server (IIS) throws an error saying that PHP script has timed out in our forums. Let’s try to understand the reason behind this. I will be using a Windows 7 Enterprise machine (having IIS 7.5) to explain this but this should be applicable to Windows 2008/Windows Vista too. The same can be told true for Windows 2003/Wi […]
    Venkat Raman Don
  • Resources for a PHP and Hudson CI Integration
    Yesterday I finally had the time to setup my first continuous integration environment. Possible solutions for CI are phpUnderControl, Hudson or Arbit. Although phpUnderControl is the most wide-spread, but from I heard complex to setup/maintain, solution supposedly a hack and Arbit just in an early Alpha I decided to give Hudson a shoot. Another reason for th […]
    Benjamin Eberlei
  • HipHop for PHP: First look
    Just this tuesday Facebook announced a ambitious project called “HipHop for PHP”, if you missed it general opinion says you have been coding PHP in a cave. As I write this review no code has been posted yet, but Facebook has made a great move to open source the project so we can all get our hands on it, use it and contribute to it. So since the code is not o […]
    Rafael Dohms