3. The speaker says... Fun is a function of performance. Performance gains, performance wins are one of the primary purposes of a cache. Before we look at the mysqlnd cache plugin, let's see some performance figures. Maybe, you'll smile about any benchmark you read in the future.
4. Performance: open 24h „ Your ultimate OpenSource Shopsoftware E-Commerce solution for growth and profitability.“ http://www.oxid-esales.com/
5.
6. The speaker says... We are benchmarking Oxid on a dated two machines setup. One machine runs Apache 2.2.15 + PHP, the other machine runs MySQL 5.1.25-rc. Our hardware consists of two machines with using dual-core x86_64 CPUs. We run on Linux, have 4GB RAM and use RAID-0. The machines are connected via a 1GBit ethernet. The webserver is CPU saturated. The database server is bored... - no CPU, no I/O wait. Network latency <0.2ms. Benchmark: ab -n600 -c8 http://127.0.0.1/oxid
7.
8. The speaker says... Add 14 charachters, get 61% performance gain! Use byte-code caches, simple as that! Your http-workers don't increase in size and the memory required by the byte-code cache is irrelevant. Do you smell the blood? Let's use fresh meat!
9.
10. The speaker says... 73 % performance gain, if you step up to PHP 5.3 ! The fresh and tasty meat is waiting for you. Honestly, try PHP 5.3. Make the life of the php.net developers easier. Be brave, forget about „never touch a runnning systems“. Security and performance are good reasons to eat fresh meat. Anyway, what can MySQL do for Oxid, any MySQL contributions to PHP 5.3? Sure!
11. The MySQL native driver for PHP Server API (SAPI) CGI CLI Embed ISAPI NSAPI phttpd thttpd ... Zend Engine PHP Runtime PHP Extensions bcmath mysql mysqli mysqlnd pdo pdo_mysql xml ...
12. The speaker says... The MySQL native driver for PHP (mysqlnd) is a C library which implements the MySQL Client Server Protocol. It serves as a drop-in replacement for the MySQL Client Library (AKA libmysqlclient AKA Connector/C). mysqlnd is part of the PHP source code repository as of PHP 5.3. Every PHP source code distribution contains it. mysqlnd is is a special kind of PHP extension. Like ext/PDO it does not export any userland functions. It serves as a C library for other extensions , like ext/PDO.
13. Replacement for libmysql $mysqli = new mysqli(...); $mysql = mysql_connect(...); $pdo = new PDO(...);
14. The speaker says... All PHP-MySQL APIs can either make use of the MySQL Client Library or mysqlnd to connect to MySQL. The decision to use one or the other library is made at compile time. Within a PHP binary you can mix mysqlnd and the MySQL Client Library as you like: one PHP MySQL API may use mysqlnd and another PHP MySQL extension may use the MySQL Client Library. To use the MySQL Client Library, you must have it installed on your system (at least when building PHP), whereas mysqlnd ships with PHP and thus has no pre-requisites.
15.
16. The speaker says... Strike - 80 % performance gain! That is 7% more than before. Unexpected, though. You can expect mysqlnd to be about as fast as libmysql. It really depends on the benchmark. If, for example, I use -c4 instead of -c8 and the webserver is not CPU bound, the results flip around: libmysql is a little faster than mysqlnd. That's a bit like with MySQL 5.5. I'm using MySQL 5.1 here because MySQL 5.5 turned out to be slower in my setup. MySQL 5.5 is optimized for high concurrent loads, but MySQL is not the bottle-neck in my setup...
18. The speaker says... The core feature set of mysqlnd is defined by the MySQL Client Libary feature set. It has taken about 15k lines of C (without comments) to implement the core functionality. Further growth will complicate maintenance. Further growth will hinder an understanding of the code base. mysqlnd growth must be stopped without preventing the addition of new features. Some new features may be far beyond mainstream user requirements. Good reasons to introduce mysqlnd "plugins". Plugins can hook and replace all mysqlnd C API calls.
28. The speaker says... Uuups... that mysqlnd_uh plugin is slow! Thank you David Sorria Para from Mayflower for making PHP slower! The performance benefit of mysqlnd over libmysql is lost.
29.
30. The speaker says... Uuups... that mysqlnd_qc plugin is slow! Thank you Andrey Hristov and Ulf Wendel from Oracle for developing a cache that makes PHP even slower!
31.
32. The speaker says... Wait, have traded-in some 5% performance loss compared to libmysql for a new feature. The new feature is user space query monitoring. The benchmark shows Oxid running with a simplictic query monitor. The query monitor logs all queries run by Oxid and prints outs the total number of queries and distinct queries at the end of the web page.
33.
34. The speaker says... The client-side query cache is now turned on. What we see is about the maximum performance gain can get from client side query caching in this scenario: 88% performance win. mysqlnd_qc.cache_by_default does cache all queries for mysqlnd_qc.ttl seconds. This setting is not recommended for production environments. Its is for development to estimate the maximum performance gain.
35.
36. The speaker says... End of the PHP 5.3 game: 94% performance win. mysqlnd_qc.cache_no_table will cache even those queries which query no table, such as SELECT NOW(). In most cases this setting is out of question: don't use it.
37.
38. The speaker says... The future is bright: 149% performance win! Recall how we got here. We did nothing but use the latest software. We have not modified Oxid, we have not made future updates of Oxid a hassle because of potential add-ons and custom additions. We have compiled a custom version of PHP which powers PECL/mysqlnd_qc. We have set two php.ini configuration settings. That's it.
39.
40. The speaker says... Just kidding. However, the previous PHP 5.3.99-dev figure is no fake. Well done, Zend guys!
42. The speaker says... 93 % - 60 % = 33 % - disappointing and expected! The web server is CPU bound. The impact of the database queries on the total CPU consumption is rather small. We save very little, although we save some query time. The database server CPU is 90% idle. Queries are served mostly from main memory buffers. The 1GBit network is idle. It has a latency of <0.2ms.
44. The speaker says... Almost 200% performance win! Same hardware, same software, same query cache configuration. Still a two machine setup, still PHP 5.3.4-dev using APC. A minor variation on using Apache bench: ab -n80 -c8 http://127.0.0.1/oxid
45.
46. The speaker says... Forget about the benchmarks: what if, for example, we use ab -c32 ? The CPU is idle with all the network wait times. Run your own tests! Too many variables! The benchmark demostrates the difference between a client and a server-side cache. The MySQL Server Query Cache will not help you much, if the client-server connection is slow. The mysqlnd_qc plugin gives you a significant boost: move the cache to the client! In 2003 Rasmus said something like „they are *** stupid not to have the cache in the driver.“ - we listened...
47. PECL/mysqlnd_qc at a glance Supports Oxid eShop, supports any PHP MySQL application!
49. The speaker says... PECL/mysqlnd_qc is a basic client side query cache. The default invalidation method is TTL. The limitation can be lifted with user defined storage handler. Build-in storage handler support local memory, APC, Memcache and SQLite. Multiple query cache prototypes had been developed before. None got published because all of them had been too complicated. PECL/mysqlnd_qc is way more elegant and simpler: it caches raw wire protocol data. On a cache hit it replays the raw data. This is much easier than serializing and deserializing result sets of PHP variables (zvals).
55. The speaker says... PECL/mysqlnd_qc shares the basic design with the much older PEAR_Cache – a generic cache, written in PHP, to store arbitrary data. Both offer flexible storage and default to TTL invalidation strategy but can easily be expanded.
56.
57. The speaker says... There is one important change over previous talks and presentations: we do support prepared statements! As of two days after the initial release and the publication of the first presentations.
58. Basic Usage $mysqli = new mysqli($host, $user, $pw, $db, $port, $sock); /* Cached for mysqlnd_qc.ttl seconds */ $sql = sprintf("/*%s*/SELECT id, label FROM test", MYSQLND_QC_ENABLE_SWITCH); $res = $mysqli->query($sql); var_dump($res->fetch_all(MYSQLI_ASSOC)); $res->free(); /* Not cached */ $sql = "SELECT id, label FROM test"; $res = $mysqli->query($sql); var_dump($res->fetch_all(MYSQLI_ASSOC)); $res->free();
59. The speaker says... Somehow we need to tell the query cache which query to cache. SQL hints are a very simple solution. If you do not want to change any code in your application but turn on caching only for selected queries, read below. Check the examples at the end of the presentation.
70. The speaker says... PECL/mysqlnd_qc can use Memcache as a storage medium. An application based cache, such as PEAR/Cache, can do the same. None has the edge. PECL/mysqlnd_qc will store raw wire data in Memcache. With PECL/mysqlnd_qc you get three data transformations, three serializations: MySQL intern -> Wire data -> PHP variable. The application based requires additional serializsations: MySQL intern -> Wire data -> PHP variable -> String (-> PHP variable).
77. The speaker says... This presentation was given at the 10 th aniversary of the PHP Conference. At some point, during the first edition, Björn Schotte asked to close the doors of the main presentation room. People in the back started to joke: Ferengi is talking – sales will start now, he does not want anybody to be able to leave the room. So, if Commericals have a tradition. Let's see some commercials. Note the comment in the lower left. There are some 40-50 PHP slots in total at the 10 th conference...
78. thePHP.cc In times of tight budgets, quality is a key success factor to reduce risks in IT projects. We help businesses and enterprises to reduce IT costs in the long term with Open Source technologies and PHP in particular. [email_address] 11 slots + 1 commercial
79. A closer look at the query cache Storage handler, slam defense and statistics.
84. The speaker says... Caches can be shared to a different degree between clients. Some caches are available to the current process, resulting in a low re-use figure of cache entries, others are shared among multiple processes or even machines. Those shared among machines usually add latency. The is_select() method of a storage handler gets asked for every query if it shall be cached or not. User-defined storage handler use is_select() to implement whatever invalidation strategy they want. See below.
85. No rush Concurrency # queries # distinct queries % DB runtime 1 330 297 46% 2 330 297 64% 3 330 297 85% 4 330 297 91% 5 330 297 91% 6 330 297 83% Total Many Many Significant 1 An application developed by the presenter in 2004 has run several thousand queries per page load 2 A HTML forms library developed by the presenter in 2001 has used sessions of a size >1MB
86. The speaker says... Almost 200% performance win! Same hardware, same software, same query cache configuration, still a two machine setup, still PHP 5.3.4-dev using APC. A minor variation on using Apache bench: ab -n80 -c8 http://127.0.0.1/oxid
88. The speaker says... Plan your cache strategy carefully! If not properly planned, caching can be counter productive. For example, test what happens if a very popular cache entry used by many clients expires. For the time it takes to refresh the cache entry all clients formerly using the invalidated cache entry will contact the database. The load of the MySQL server will increase suddenly – MySQL will be slammed. Due to the high load it takes longer and longer to refresh the cache entry. MySQL gets overloaded: a spiral to death.
90. The speaker says... To avoid slamming the MySQL Server the query cache plugin has a special TTL based slam protection operation mode. If a client hits an expired cache entry (cache miss) and slam protection is turned on the client gets a cache miss but the cache entry lifetime will be extended by a configurable time. All other clients also using the expired cache entry will get a cache hit because of the extended lifetime. Only the first client, which got a cache miss, contacts the MySQL Server to refresh the expired cache entry. Ideally, it will refresh the expired cache entry before the extended lifetime ends and MySQL does not get slammed because of a sudden massive load.
107. To new shores with MAYFLOWER Mayflower is Germany's largest service provider in terms of PHP programming. No matter whether you need corporation-wide Intranets, communities, e-business and e-commerce systems or customised solutions - we are the right partner to cooperate with. Find out more about our services in the field of software development. http://www.mayflower.de/en/contact 5 slots + 1 commercial