1

I have some doubts about persistent database connections in PHP.

I understand that using a persistent connection can be efficient because every PHP process that needs a connection with the same host/user can share that connection, without having to re-establish this.

I undestand that the newest implementation introduces features that overcome the problem of the unknown connection state.

But! If I have 10 thousand processes sharing the same connection, this will push down the performance of my application because they can only use the connection one at a time! Is this true or am I wrong?

Brad
  • 159,648
  • 54
  • 349
  • 530
MaCi
  • 163
  • 2
  • 11
  • And what will be your performance if it create 10 thousands non-persistent connection !! – Rikesh Jul 19 '12 at 15:57
  • 2
    for 10k connections I would use load balancing mysql cluster – MilMike Jul 19 '12 at 15:58
  • you mean 10k unique queries, queries that have been submitted before might be cached if the result hasn't changed, so it would return the result faster and proceed to next query – Gntem Jul 19 '12 at 16:01

2 Answers2

1

Using persistent connections will be faster in about any case as you save the overhead of establishing a connection.

Using persistent connections doesn't limit you to a single connection, you can have a connection pool of 100 or 1000 persistent connections.

Handling 10000 parallel queries is oing to take some loadbalancing and a database cluster too however, a single database will be overloaded with both, non-persistent and persistent connections if you try to push 10k queries through it at the same time.

bardiir
  • 14,556
  • 9
  • 41
  • 66
1

The efficiency benefit provided by the persistent connections, specifically in establishing the connection, will be enormous if you actually have 10k processes querying the database.

Assuming you're running PHP under Apache, the first time a SQL connection is made the Apache "child process" will keep it open (i.e. - the persistent connection). All PHP requests that belong to that specific "Apache child process" will reuse that connection, if the connection information is the same of course. Apache's configuration defines how many requests a single child-process can handle before it is restarted - which should clear the persistent connection.

Now, there is also the case that you are running queries in a loop or even - dare I say it, an infinite loop. In that case, even a non-persistent connection would be deadly.

It is doubtful that the number of processes using your persistent connections will be a factor in the server's efficiency (unless, as I mentioned, you have some in-efficient code using queries). The thing that should be focused on is how many persistent connections should you allow and how many can your database/server handle?

The MySQL manual for the persistent connections, http://php.net/manual/en/features.persistent-connections.php, also covers most of (and more) of what I just summarized.

newfurniturey
  • 37,556
  • 9
  • 94
  • 102