Persistent DB Connections – Yea or Nay?

You could use this as a rough “ruleset”:

YES, use persistent connections, if:

  • There are only few applications/users accessing the database, i.e. you will not result in 200 open (but probably idle) connections, because there are 200 different users shared on the same host.
  • The database is running on another server that you are accessing over the network
  • An (one) application accesses the database very often

NO, don’t use persistent connections, if:

  • Your application only needs to access the database 100 times an hour.
  • You have many webservers accessing one database server
  • You’re using Apache in prefork mode. It uses one connection for each child process, which can ramp up fairly quickly. (via @Powerlord in the comments)

Using persistent connections is considerable faster, especially if you are accessing the database over a network. It doesn’t make so much difference if the database is running on the same machine, but it is still a little bit faster. However – as the name says – the connection is persistent, i.e. it stays open, even if it is not used.

The problem with that is, that in “default configuration”, MySQL only allows 1000 parallel “open channels”. After that, new connections are refused (You can tweak this setting). So if you have – say – 20 Webservers with each 100 Clients on them, and every one of them has just one page access per hour, simple math will show you that you’ll need 2000 parallel connections to the database. That won’t work.

Ergo: Only use it for applications with lots of requests.

Leave a Comment

tech