php mysqlnd connection multiplexing plugin
TRANSCRIPT
PowerPoint-Prsentation
Ulf Wendel, Oracle
PECL/mysqlnd_mux 1.0.0-prototype
PHP mysqlnd
Connection Multiplexing
The speaker says...
When fast is not fast enough, you pull all triggers. This includes attempts to optimize an already reasonable fast operation: connect time to MySQL.
The PHP mysqlnd connection multiplexing plugin, shares a physical connection among multiple user handles. Thus, connection overhead is saved on the client side and the number of opened connection is reduced on the server side.PECL/mysqlnd_mux is an Oracle open source development available from pecl.php.net. Developed by Andrey Hristov.
Plugins are mostly transparent
WordPress, Drupal, Symfony, Oxid, ZendFramework, ...mysql, mysqli, PDO_MYSQLmysqlndConnection MultiplexingPECL/mysqlnd_mux plugin
MySQL Server
The speaker says...
PECL/mysqlnd_mux is a plugin for the mysqlnd library. The mysqlnd library is the default C client library used by the PHP MySQL extensions mysql, mysqli and PDO_MySQL internally. Mysqlnd ships with PHP since 5.3.
Plugins operate at a layer beneath the user APIs, thus improvements are available to all PHP MySQL APIs.
Other free plugins are PECL/mysqlnd_ms (replication and load balancing), PECL/mysqlnd_qc (client-side query cache) and many more.
Multiplexing: share connection among handles
Reduce client connect overhead, reduce server load
Prototype, requires PHP 5.5.0+
PECL/mysqlnd_mux
connect()MySQLPECL/mysqlnd_muxconnect()connect()
1 connectionn connection handles
The speaker says...
The PHP mysqlnd connection multiplexing proxies MySQL
connections. Whenever a client attempts to open a connection to a
host, the plugin checks whether there is already a cached network
connection to the host in question. If not, a new connection is
established and associated with the users' connection handle.
Otherwise, the users' connection handle is linked with an already
opened network connection.
This way, multiple user handles can point to the same network
connection and share it. Connection overhead is saved and fewer
connections are opened.
Multiplexing means serializing tasks
Possibility of wait situations
Prototype: no upgrade to dedicated connection
Prototype: no collision counter
The price you pay
MySQLMUXQuery 1Query 2
Query 1Query 2
Time
The speaker says...
Sharing a resource often requires serializing access to it. This is also the case with a shared connection of the PHP mysqlnd connection multiplexing plugin. Serializing actions bares the risks of collisions and wait situations. In the example, a clients query has to wait for completion of another clients query before it can be executed. Query 2 waits for query 1 to finish.
The prototype is using a mutex to synchronize access to a shared connection.
No new API calls, it just works!
Supports popular buffered queries
(mysql_query(), mysqli_query(),
PDO if using PDO::ATTR_EMULATE_PREPARES)
Prototype does not handle unbuffered queries
(mysqli_real_query())
Prepared statements not in 1.0.0-prototype
(mysqli_stmt_*())
MUX as a demo of the plugin API
The speaker says...
PECL/mysqlnd_mux 1.0.0-prototype is an example of the strenghts of the mysqlnd C plugin API. The initial public release is not a production-ready stable solution. Features have been skipped for the prototype to keep the demo of the plugin API short and comprehensive.
The astonishing finding of the plugin is that multiplexing can be added to add PHP MySQL APIs without changing the APIs. It just works for some cases already today. Other cases could be covered in future versions, depending on user feedback.
THAT'S IT FOLKS?
Connection remains open after close()
Connection establishment overhead reduced
Number of concurrent connections not reduced
Usually, connection state reset upon reuse
Compared to pooling
ClientMySQLConnection poolClient
ConnectionConnectionConnection
The speaker says...
A connection pool is a cache for connections. If a client openes a connection, the pool is checked for an already estiablished connection to the requested host. In case of a hit, the pooled connection is reset, then taken from the pool of unused connections and returned to the client. If no connection is found a new one gets opened and returned to the caller. Then, the connection is used until the client calls close(). Upon close(), the connection is not closed but put back into the pool for reuse.
Pooling saves time on reuse but does not reduce the total number of concurrent connections.
Connection remains open after close()
Connection overhead and reuse costs reduced
Number of concurrent connections not reduced
Connection state not reset before reuse
Persistent Connection
MySQLClient 1PoolConnection 1
SET @myvar = 1
MySQLClient 2PoolConnection 1
SELECT @myvar
The speaker says...
Persistent connections can be described as a special kind of pooled connections. As the name says, the state of a persistent connection is not reset between use. PHP persistent database connections have often been criticised for persisting the state of a connection... - for their very purpose!
Thus, when persistent connections have been added to the mysqli extension, mysqli actually got pooled connections. By default, the connection state is reset before reuse. Performance fanatics can disable this during compile time.
Speed-up tricks compared
MUXPooled Conn.Pers. Conn
Reduce connection overheadYesYesYes
Reduce # concurrently open connectionsYesNoNo
Connection state shared among clientsYesNoYes
(mysqli: No)
Serialization required, collisions possibleYesNoNo
Every optimization is a trade
You gain something: fewer connections opened
You give something: collision possible
You gain something: lower connection costs
You give something: no isolation of connection state
The speaker says...
Optimizations, such as multiplexing, pooling or persistent
connections come at a price. There is no one-fits all
trick.
Please, try to understand the properties of each option. Then, decide on a case-by-case basis which technology to use.
Pools are bound to a PHP process
Depending on deployment model,
a PHP process handles one or multiple requests
Remember: scope/life-span
HTTP ServerPHP processPHP processConnection poolConnection poolConn 1Conn 2Conn 3Conn 4
The speaker says...
Operating systems associate file descriptors, including network connections, with processes. At the end of the process, the network connections are closed. Thus, the life-span of every client-side cache/pool of a PHP process is that of the PHP process. No matter whether we are discussing connections cached for multiplexing or persistent connections as found in any of the PHP MySQL APIs (mysql, mysqli, PDO_MySQL).
Depending on the web server deployment model, a PHP process handles one or multiple requests (script runs).
THE END
http://www.slideshare.net/nixnutz/Contact: [email protected], @Ulf_Wendel