Here comes the next open source mysqlnd plugin prototype: PECL/mysqlnd_mux. The PHP mysqlnd connection multiplexing plugin shares a connection to MySQL between multiple user connection handles. Connection multiplexing aims to reduce connect time and lowers the total number of concurrenly opened connections. The first is primarily a benefit for the client, the latter is great for the server. Multiplexing may be most desired in environments with many short-lived connections and weak client-side pooling - such as a PHP web application.
The mysqlnd library is part of PHP as of 5.3. Since PHP 5.4 it is the default C client library for all three PHP MySQL APIs (mysqli, PDO_MySQL, mysql).
|Wordpress, Drupal, Symfony, Zend Framework, Oxid, …|
As a backend C library for the existing APIs, mysqlnd and its plugins can be transparent from a user perspective. They operate inside PHP at a layer beneath the users applications.Thus, the mysqlnd multiplexing plugin can be used with all existing PHP MySQL applications. However, there is a glory detail. The plugin needs the latest version of mysqlnd contained in PHP 5.5.
How the multiplexing is implemented
Whenever a user attempts to open a MySQL connection using any of the PHP MySQL extensions (mysql, mysqli, PDO_MySQL), the corresponding functions of the mysqlnd library are called inside PHP. The plugin hooks those calls. Whenever the user opens a connection, the plugin checks for a cached connection to the requested MySQL server. If none is found, a new connection is opened, cached and returned to the caller.
$handle_1 = mysqli_connect("host", ...)
Have cached connection to
When doing another connect to the same MySQL server as before, no matter what API used, the plugin checks its cache and links the new user handle to the already opened connection. No second connection needs to be established.
$handle_2 = new PDO("mysql:host=host;...", ...)
Have cached connection to
Compared to a traditional connect there is now only one connection opened to MySQL. Two user connection handles share the same underlying connection. Less work for the server.
An optimization for some
Multiplexing is a performance optimization with side-effects. Collisions and wait situations can arise. Concurrent access to the shared connection must be synchronized. Imagine two clients attempt to use the shared connection at the same time. One of the clients will have to wait until the other stopped using the connection before it can execute its query.
||Query 1||Query 2||->||MySQL|
Sharing a connection means also sharing its state. A connections state consists of assorted settings, transaction status, SQL session variables, temporary tables and many more. Please, see the replication plugin manual for an in-depth discussion. Shared state is one of the biggest criticism of persistent connections. This does not mean shared state is malicious per-se. It needs to be understood and mastered, that’s it.
Connection multiplexing is at an optimization detail level that requires understanding its properties.
A demo of the plugin API
We are making an initial public release: 1.0.0-prototype. We are
not announcing a production-ready stable release. The initial
implementation supports buffered queries only. API calls that use
buffered queries include
mysql_query() and all of PDO if
PDO::ATTR_EMULATE_PREPARES is set. Good enough for
an evaluation of the idea.
The initial release is also great to learn about the mysqlnd C plugin API. There is not too much code. This allows expert users to talk to us at any level they want. If anybody feels for C coding and want to speak to us the international developer language, commit a patch. If you feel the next step should be looking at multiplexing from a users perspective, ask for a feature addition. Andrey understands both languages. Andrey Hristov? Yes, he developed the plugin.