PHP Performance w/APC + thaicyberpoint.com thaithinkpad.com thaihi5.com
Cache Theory ?
Req Output Cache Compute Miss Hit Store Check Miss - [Store] - Hit
Type ? FastSlow
APC ? Alternative PHP Cache
Code not changes request Opcode Cache and Variables Cache Automatic update w/code changes
Free & Open Source
Simply Installation
PHP Flow Scanning and Lexing (Plain Text to Tokens) Parsing (Tokens to Expressions) Compilation (Translated Expressions to Opcode) Execution (Opcode stacks are processed) Req Output Scanning Lexing Parsing Compilation Scanning Lexing Parsing Compilation Execution
Normal PHP vs APC
Req Output Scanning Lexing Parsing Compilation Scanning Lexing Parsing Compilation Execution
Normal PHP vs APC Req Output Opcode cache Opcode cache Scanning Lexing Parsing Compilation Scanning Lexing Parsing Compilation APC hook Execution Miss Hit Store
Installation Basic PECL ( –pecl install apc Ubuntu –apt-get install php5-apc Windows ( –extension=php_apc.dll Conf extension=apc.so apc.shm_segments=1 apc.shm_size=256 apc.optimization=0 apc.num_files_hint=2048 apc.ttl=3600 apc.user_ttl=3600 apc.enable_cli=1 apc.max_file_size=1M extension=apc.so apc.shm_segments=1 apc.shm_size=256 apc.optimization=0 apc.num_files_hint=2048 apc.ttl=3600 apc.user_ttl=3600 apc.enable_cli=1 apc.max_file_size=1M
Benchmark PHP NativePHP w/APC Concurrency Level10 Time taken for tests60 seconds Complete requests Total transferred 643,149 bytes1,962,675 bytes HTML transferred 516,971 bytes1,582,035 bytes Requests per second 4.91 [#/sec] (mean)15.21 [#/sec] (mean) Time per request [ms] (mean) [ms] (mean) Time per request (mean, across all concurrent requests) [ms] [ms] Transfer rate [KB/s] received31.88 [KB/s] received
Memcached
History “Developed by Danga Interactive for LiveJournal.com”
Memcached ? a distributed memory object caching system with hash table
Memcached ? a distributed memory object caching system with hash table
Memcached ? a distributed memory object caching system with hash table a generic two stage hashing system (clients and servers). Clients are written for specific application languages. Server is written in C
Memcached ? a distributed memory object caching system with hash table a generic two stage hashing system (clients and servers). Clients are written for specific application languages. Server is written in C
Memcached ? a distributed memory object caching system with hash table a generic two stage hashing system (clients and servers). Clients are written for specific application languages. Server is written in C 85% of top 20 sites use memcached; 50%+ of top 5k sites uses Memcached
Memcached ? a distributed memory object caching system with hash table a generic two stage hashing system (clients and servers). Clients are written for specific application languages. Server is written in C 85% of top 20 sites use memcached; 50%+ of top 5k sites uses Memcached
Why Memcached ? Scale-out –Machine –Process (Use different ports) Memory faster than Disk Allows for efficient use of your database Can utilize existing network –Uses non-blocking network I/O (TCP/IP) Very flexible Client libraries in all major languages
Limits (c/Hash Table) Key : max 250 chars. Values: max 1MB
“We use more than 800 servers supplying over 28 terabytes of memory to our users.” “total throughput achieved is 300,000 UDP req/s” by Paul Saab Saturday, December 13, 2008 at 2:43am 8-core machines 20-30% CPU usage. scale memcached to 8 threads on an 8-core system moved to UDP for get operations to reduce network traffic
Architecture practical ?
Architecture practical ? “designing & implementing scalable applications with memcached and mysql”
Architecture practical ?
Coding ? (in PHP) /* OO API */ $memcacheObj = new Memcache; $memcacheObj->connect('memcache_host', 11211); /* set */ $memcacheObj->set('key', 'data', MEMCACHE_COMPRESSED, 50); / * get w/single key */ $var = $memcacheObj->get('key'); / * get w/multiple key */ $var = $memcacheObj->get(array('key1', 'key2')); $memcacheObj->close();
CMS ? Drupal has Memcache API and Integration Wordpress has object cache wordpress-object-cache.html
code optimization is best
Discussion Q&A
Ref. Improving PHP Application Performance with APC performance-with-apc-presentation PHP Accelerators : APC vs Zend vs XCache with Zend Framework xcache-with-zend-framework/ Scaling with memcached Gear6 and Scaling Website Performance: Caching Session and Profile Data with Memcached performance-caching-session-and-profile-data-with-memcached
Ref. (2) PHP: Memcache - Manual
Thank