SlideShare a Scribd company logo
1 of 29
Download to read offline
Apc & Memcached
                                  the High-
                           Performance Duo
                                        ConFoo 2011
                                    Ilia Alshanetsky
                                               @iliaa



Wednesday, March 9, 2011
Wednesday, March 9, 2011
What is APC?

                   • Primarily designed to accelerate script
                           performance via opcode caching

                   • Extends opcode caching to facilitate user-data
                           caching

                   • Actively maintained
                   • It even works on Windows!!


Wednesday, March 9, 2011
Opcode Caching
                 Default Mode                                                           With APC
                                                                                                PHP Script
                             PHP Script




                                                              APC
                                                                                                Opcode Cache


                             Zend Compile




                                                                Zend Compile           No       Data Cached?




                             Zend Execute
                                                                                                    Yes
                                                               Cache Opcode




           Function/Method
                                            Require/Include
                 Call                                                                           Zend Execute




                                                                              Function/Method
                                                                                                               Require/Include
                                                                                    Call




Wednesday, March 9, 2011
APC User-Cache
                   • Allows you to apply the same caching logic to
                           your data as applied to PHP scripts.




                    Slide Motto:
                           Not everything has to be real-time!

Wednesday, March 9, 2011
APC in Practice
  // store an array of values for 1 day, referenced by "identifier"
  if (!apc_add("identifier", array(1,2,3), 86400)) {
      // already exists? let's update it instead
      if (!apc_store("identifier", array(1,2,3), 86400)) {
          // uh, oh, b0rkage
      }
  }

  $ok = null;
  // fetch value associated with "identified" and
  // put success state into $ok variable
  $my_array = apc_fetch("identifier", $ok);
  if ($ok) {
      // changed my mind, let's delete it
      apc_delete("identifier");
  }



Wednesday, March 9, 2011
Let’s be lazy

      // create or update an array of values for 1 day
      if (!apc_store("identifier", array(1,2,3), 86400)) {
        // uh, oh, b0rkage
        mail("gopal, brian, kalle", "you broke my code", "fix it!");
      }




           If you don’t care whether your are adding or updating
           values you can just use apc_store() and keep your
           code simpler.



Wednesday, March 9, 2011
Don’t Delete
               • Deleting from cache is expensive as it may
                    need to re-structure internal hash tables.

                           • Rely on auto-expiry functionality instead
                           • Or an off-stream cron job to clean up stale
                            cache entries

               • In many cases it is simpler just to start from
                    scratch.

                            apc_clear_cache(“user”)

Wednesday, March 9, 2011
Advantages of APC

                   • If you (or your ISP) uses opcode caching,
                           chances are it is already there.

                   • Really efficient at storing simple types (scalars
                           & arrays)

                   • Really simple to use, can’t get any easier…
                   • Fairly stable


Wednesday, March 9, 2011
APC Limitations

                   • PHP only, can’t talk to other “stuff ”




                   • Not distributed, local only
                   • Opcode + User cache == all eggs in one basket


Wednesday, March 9, 2011
Memcached

                   • Interface to Memcached - a distributed caching
                           system

                   • Provides Object Oriented interface to caching
                           system

                   • Offers a built-in session handler
                   • Can only be used for “user” caching
                   • Purpose built, so lots of nifty features

Wednesday, March 9, 2011
Memcache vs Memcached

           • Memcached Advantages
                  • Faster
                           • Igbinary serializer
                           • fastlz compression
                  • Multi-Server Interface
                  • Fail-over callback support

Wednesday, March 9, 2011
Basics in Practice
             $mc = new MemCached();

             // connect to memcached on local machine, on default port
             $mc->addServer('localhost', '11211');

             // try to add an array with a retrieval key for 1 day
             if (!$mc->add('key', array(1,2,3), 86400)) {
                 // if already exists, let's replace it
                 if (!$mc->replace('key', array(1,2,3), 86400)) {
                     die("Critical Error");
                 }
             }

             // let's fetch our data
             if (($data = $mc->get('key')) !== FALSE) {
                 // let's delete it now
                 $mc->delete('key'); // RIGHT NOW!
             }


Wednesday, March 9, 2011
Data Retrieval Gotcha(s)
             $mc = new MemCached();
             $mc->addServer('localhost', '11211');

             $mc->add('key', FALSE);

             if (($data = $mc->get('key')) !== FALSE) {
               die("Not Found?"); // not true
               // The value could be FALSE, you
               // need to check the response code
             }

             // The “right” way!
             if (
                    (($data = $mc->get('key')) === FALSE)
                    &&
                    ($mc->getResultCode() != MemCached::RES_SUCCESS)
             ) {
               die("Not Found");
             }

Wednesday, March 9, 2011
Interface Basics Continued...
 $mc = new MemCached();
 // on local machine we can connect via Unix Sockets for better speed
 $mc->addServer('/var/run/memcached/11211.sock', 0);

 // add/or replace, don't care just get it in there
 // without expiration parameter, will remain in cache “forever”
 $mc->set('key1', array(1,2,3));

 $key_set = array('key1' => “foo”, 'key1' => array(1,2,3));

 // store multiple keys at once for 1 hour
 $mc->setMulti($key_set, 3600);

 // get multiple keys at once
 $data = $mc->getMulti(array_keys($key_set));
 /*
 array(                                 For multi-(get|set), all   ops
     'key1' => ‘foo’
     'key2' => array(1,2,3)                  must succeed for
 )
 */
                                             successful return.
Wednesday, March 9, 2011
Multi-Server Environment

      $mc = new MemCached();

      // add multiple servers to the list
      // as many servers as you like can be added
      $mc->addServers(array(
           array('localhost', 11211, 80), // high-priority 80%
           array('192.168.1.90', 11211, 20)// low-priority 20%
      ));

      // You can also do it one at a time, but this is not recommended
      $mc->addServer('localhost', 11211, 80);
      $mc->addServer('192.168.1.90', 11211, 20);

      // Get a list of servers in the pool
      $mc->	
  getServerList();
      // array(array(‘host’ => … , ‘port’ => … ‘weight’ => …))



Wednesday, March 9, 2011
Data Segmentation
                   • Memcached interface allows you to store
                           certain types of data on specific servers
         $mc = new MemCached();
         $mc->addServers( … );

         // Add data_key with a value of “value” for 10 mins to server
         // identified by “server_key”
         $mc->addByKey('server_key', 'data_key', 'value', 600);

         // Fetch key from specific server
         $mc->getByKey('server_key', 'data_key');

         // Add/update key on specific server
         $mc->setByKey('server_key', 'data_key', 'value', 600);

         // Remove key from specific server
         $mc->deleteByKey('server_key', 'data_key');

Wednesday, March 9, 2011
And there is more ...
      • The specific-server interface also supports multi-(get|set)

       $mc = new MemCached();
       $mc->addServers( … );

       $key_set = array('key2' => “foo”, 'key1' => array(1,2,3));

       // store multiple keys at once for 1 hour
       $mc->setMultiByKey('server_key', $key_set, 3600);

       // get multiple keys at once
       $data = $mc->getMultiByKey('server_key', array_keys($key_set));




Wednesday, March 9, 2011
Delayed Data Retrieval

                   • One of the really neat features of Memcached
                           extension is the ability to execute the “fetch”
                           command, but defer the actual data retrieval
                           until later.



                   • Particularly handy when retrieving many keys
                           that won’t be needed until later.



Wednesday, March 9, 2011
Delayed Data Retrieval

       $mc = new MemCached();
       $mc->addServer('localhost', '11211');

       $mc->getDelayed(array('key')); // parameter is an array of keys

       /* some PHP code that does “stuff” */

       // Fetch data one record at a time
       while ($data = $mc->fetch()) { ... }

       // Fetch all data in one go
       $data = $mc->fetchAll();




Wednesday, March 9, 2011
Data Compression
       • In many cases performance can be gained by
            compressing large blocks of data. Since in most cases
            network IO is more expensive then CPU speed + RAM.
                      $mc = new MemCached();
                      $mc->addServer('localhost', 11211);
                      // enable compression
                      $mc->setOption(Memcached::OPT_COMPRESSION, TRUE);

                             Related INI settings (INI_ALL)
                             Other possible value is zlib
                             memcached.compression_type=fastlz

                             minimum compression rate
                             memcached.compression_factor=1.3

                             minimum data size to compress
                             memcached.compression_threshold=2000

Wednesday, March 9, 2011
Counter Trick
            $mc = new MemCached();
            $mc->addServer('localhost', 11211);

            // add key position if does not already exist
            if (!$mc->add('key_pos', 1)) {
                // otherwise increment it
                $position = $mc->increment('key_pos');
            } else {
                $position = 1;
            }

            // add real value at the new position
            $mc->add('key_value_' . $position, array(1,2,3));



                           • Simplifies cache invalidation
                           • Reduces lock contention (or eliminates it)
Wednesday, March 9, 2011
PHP Serialization

        If you are using memcached to store complex data type
        (arrays & objects), they will need to be converted to
        strings for the purposes of storage, via serialization.



        Memcached can make use of igbinary serializer that
        works faster (~30%) and produces more compact data set
        (up-to 45% smaller) than native PHP serializer.

                           https://github.com/igbinary

Wednesday, March 9, 2011
Enabling Igbinary
                           Install Memcached extension with
                           --enable-memcached-igbinary

                           $mc = new MemCached();
                           $mc->addServer('localhost', 11211);

                           // use Igbinary serializer
                           $mc->setOption(
                              Memcached::OPT_SERIALIZER,
                              Memcached::SERIALIZER_IGBINARY
                           );




Wednesday, March 9, 2011
Utility Methods
                                      Array
                                      (
                                      	
  	
  	
  	
  [server:port]	
  =>	
  Array
                                      	
  	
  	
  	
  	
  	
  	
  	
  (
                                      	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  [pid]	
  =>	
  4933
      $mc = new MemCached();          	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  [uptime]	
  =>	
  786123
      $mc->addServer('localhost', 11211);
                                      	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  [threads]	
  =>	
  1
                                      	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  [time]	
  =>	
  1233868010
                                      	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  [pointer_size]	
  =>	
  32
      // memcached statistics gathering
                                      	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  [rusage_user_seconds]	
  =>	
  0
      $mc->getStats();                	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  [rusage_user_microseconds]	
  =>	
  140000
                                      	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  [rusage_system_seconds]	
  =>	
  23
                                      	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  [rusage_system_microseconds]	
  =>	
  210000
                                      	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  [curr_items]	
  =>	
  145
                                      	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  [total_items]	
  =>	
  2374
                                      	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  [limit_maxbytes]	
  =>	
  67108864
                                      	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  [curr_connections]	
  =>	
  2
      // clear all cache entries      	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  [total_connections]	
  =>	
  151
      $mc->flush();                   	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  [bytes]	
  =>	
  20345
                                      	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  [cmd_get]	
  =>	
  213343
                                      	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  [cmd_set]	
  =>	
  2381
      // clear all cache entries      	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  [get_hits]	
  =>	
  204223
                                      	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  [get_misses]	
  =>	
  9120
      // in 10 minutes                	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  [evictions]	
  =>	
  0
      $mc->flush(600);                	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  [bytes_read]	
  =>	
  9092476
                                      	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  [bytes_written]	
  =>	
  15420512
                                      	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  [version]	
  =>	
  1.2.6
                                      	
  	
  	
  	
  	
  	
  	
  	
  )
                                      )

Wednesday, March 9, 2011
Memcached Session Handler

   # Session settings
   session.save_handler
   # set to “memcached”
   session.save_path
   # set to memcache host server:port
   memcached.sess_prefix
   # Defaults to memc.sess.key
Wednesday, March 9, 2011
Advantages of Memcached

                   • Allows other languages to talk to it
                   • One instance can be shared by multiple servers
                   • Failover & Redundancy
                   • Nifty Features
                   • Fairly stable


Wednesday, March 9, 2011
Perfection Attained?

                                  • Slower then APC, especially
                                   for array storage

                                  • Requires external daemon
                                  • You can forget about it on
                                   shared hosting, due to lack of
                                   authentication (D’oh!).




Wednesday, March 9, 2011
Any Questions?

                       Feedback:
                           http://joind.in/2806
                       Slides:
                           http://ilia.ws


Wednesday, March 9, 2011

More Related Content

What's hot

Solving_the_C20K_problem_PHP_Performance_and_Scalability-phpquebec_2009
Solving_the_C20K_problem_PHP_Performance_and_Scalability-phpquebec_2009Solving_the_C20K_problem_PHP_Performance_and_Scalability-phpquebec_2009
Solving_the_C20K_problem_PHP_Performance_and_Scalability-phpquebec_2009Hiroshi Ono
 
[Strukelj] Why will Java 7.0 be so cool
[Strukelj] Why will Java 7.0 be so cool[Strukelj] Why will Java 7.0 be so cool
[Strukelj] Why will Java 7.0 be so cooljavablend
 
Cake Php 1.2 (Ocphp)
Cake Php 1.2 (Ocphp)Cake Php 1.2 (Ocphp)
Cake Php 1.2 (Ocphp)guest193fe1
 
Puppet Conf 2012 - Managing Network Devices with Puppet
Puppet Conf 2012 - Managing Network Devices with PuppetPuppet Conf 2012 - Managing Network Devices with Puppet
Puppet Conf 2012 - Managing Network Devices with PuppetNan Liu
 
Uptime Database Appliance - Technology Preview
Uptime Database Appliance - Technology PreviewUptime Database Appliance - Technology Preview
Uptime Database Appliance - Technology PreviewUptime Technologies LLC
 
Puppet modules: An Holistic Approach
Puppet modules: An Holistic ApproachPuppet modules: An Holistic Approach
Puppet modules: An Holistic ApproachAlessandro Franceschi
 
PuppetCamp SEA 1 - Puppet Deployment at OnApp
PuppetCamp SEA 1 - Puppet Deployment  at OnAppPuppetCamp SEA 1 - Puppet Deployment  at OnApp
PuppetCamp SEA 1 - Puppet Deployment at OnAppWalter Heck
 
Puppet modules: A Holistic Approach - Geneva
Puppet modules: A Holistic Approach - GenevaPuppet modules: A Holistic Approach - Geneva
Puppet modules: A Holistic Approach - GenevaAlessandro Franceschi
 
Managing themes and server environments with extensible configuration arrays
Managing themes and server environments with extensible configuration arraysManaging themes and server environments with extensible configuration arrays
Managing themes and server environments with extensible configuration arraysChris Olbekson
 
Puppet for Sys Admins
Puppet for Sys AdminsPuppet for Sys Admins
Puppet for Sys AdminsPuppet
 
Spl to the Rescue - Zendcon 09
Spl to the Rescue - Zendcon 09Spl to the Rescue - Zendcon 09
Spl to the Rescue - Zendcon 09Elizabeth Smith
 
A first Draft to Java Configuration
A first Draft to Java ConfigurationA first Draft to Java Configuration
A first Draft to Java ConfigurationAnatole Tresch
 
Configure Your Projects with Apache Tamaya
Configure Your Projects with Apache TamayaConfigure Your Projects with Apache Tamaya
Configure Your Projects with Apache TamayaAnatole Tresch
 

What's hot (20)

Solving_the_C20K_problem_PHP_Performance_and_Scalability-phpquebec_2009
Solving_the_C20K_problem_PHP_Performance_and_Scalability-phpquebec_2009Solving_the_C20K_problem_PHP_Performance_and_Scalability-phpquebec_2009
Solving_the_C20K_problem_PHP_Performance_and_Scalability-phpquebec_2009
 
Download It
Download ItDownload It
Download It
 
Anatomy of a reusable module
Anatomy of a reusable moduleAnatomy of a reusable module
Anatomy of a reusable module
 
[Strukelj] Why will Java 7.0 be so cool
[Strukelj] Why will Java 7.0 be so cool[Strukelj] Why will Java 7.0 be so cool
[Strukelj] Why will Java 7.0 be so cool
 
Cake Php 1.2 (Ocphp)
Cake Php 1.2 (Ocphp)Cake Php 1.2 (Ocphp)
Cake Php 1.2 (Ocphp)
 
Puppet modules for Fun and Profit
Puppet modules for Fun and ProfitPuppet modules for Fun and Profit
Puppet modules for Fun and Profit
 
Puppet Conf 2012 - Managing Network Devices with Puppet
Puppet Conf 2012 - Managing Network Devices with PuppetPuppet Conf 2012 - Managing Network Devices with Puppet
Puppet Conf 2012 - Managing Network Devices with Puppet
 
Jsp and jstl
Jsp and jstlJsp and jstl
Jsp and jstl
 
Uptime Database Appliance - Technology Preview
Uptime Database Appliance - Technology PreviewUptime Database Appliance - Technology Preview
Uptime Database Appliance - Technology Preview
 
Puppet modules: An Holistic Approach
Puppet modules: An Holistic ApproachPuppet modules: An Holistic Approach
Puppet modules: An Holistic Approach
 
PuppetCamp SEA 1 - Puppet Deployment at OnApp
PuppetCamp SEA 1 - Puppet Deployment  at OnAppPuppetCamp SEA 1 - Puppet Deployment  at OnApp
PuppetCamp SEA 1 - Puppet Deployment at OnApp
 
Puppi. Puppet strings to the shell
Puppi. Puppet strings to the shellPuppi. Puppet strings to the shell
Puppi. Puppet strings to the shell
 
BeJUG JavaFx In Practice
BeJUG JavaFx In PracticeBeJUG JavaFx In Practice
BeJUG JavaFx In Practice
 
Puppet modules: A Holistic Approach - Geneva
Puppet modules: A Holistic Approach - GenevaPuppet modules: A Holistic Approach - Geneva
Puppet modules: A Holistic Approach - Geneva
 
Managing themes and server environments with extensible configuration arrays
Managing themes and server environments with extensible configuration arraysManaging themes and server environments with extensible configuration arrays
Managing themes and server environments with extensible configuration arrays
 
Puppet for Sys Admins
Puppet for Sys AdminsPuppet for Sys Admins
Puppet for Sys Admins
 
Spl to the Rescue - Zendcon 09
Spl to the Rescue - Zendcon 09Spl to the Rescue - Zendcon 09
Spl to the Rescue - Zendcon 09
 
A first Draft to Java Configuration
A first Draft to Java ConfigurationA first Draft to Java Configuration
A first Draft to Java Configuration
 
Ansible
AnsibleAnsible
Ansible
 
Configure Your Projects with Apache Tamaya
Configure Your Projects with Apache TamayaConfigure Your Projects with Apache Tamaya
Configure Your Projects with Apache Tamaya
 

Viewers also liked

Memcached B box presentation
Memcached B box presentationMemcached B box presentation
Memcached B box presentationNagesh Chinkeri
 
Memcached And MySQL
Memcached And MySQLMemcached And MySQL
Memcached And MySQLChris Barber
 
Plugin Memcached%20 Study
Plugin Memcached%20 StudyPlugin Memcached%20 Study
Plugin Memcached%20 StudyLiu Lizhi
 
High Performance Drupal
High Performance DrupalHigh Performance Drupal
High Performance DrupalChapter Three
 
Drupal High Availability and High Performance
Drupal High Availability and High PerformanceDrupal High Availability and High Performance
Drupal High Availability and High PerformanceAmazee Labs
 
Implementing High Performance Drupal Sites
Implementing High Performance Drupal SitesImplementing High Performance Drupal Sites
Implementing High Performance Drupal SitesShri Kumar
 
Build a responsive website with drupal
Build a responsive website with drupalBuild a responsive website with drupal
Build a responsive website with drupalShyamala Rajaram
 

Viewers also liked (7)

Memcached B box presentation
Memcached B box presentationMemcached B box presentation
Memcached B box presentation
 
Memcached And MySQL
Memcached And MySQLMemcached And MySQL
Memcached And MySQL
 
Plugin Memcached%20 Study
Plugin Memcached%20 StudyPlugin Memcached%20 Study
Plugin Memcached%20 Study
 
High Performance Drupal
High Performance DrupalHigh Performance Drupal
High Performance Drupal
 
Drupal High Availability and High Performance
Drupal High Availability and High PerformanceDrupal High Availability and High Performance
Drupal High Availability and High Performance
 
Implementing High Performance Drupal Sites
Implementing High Performance Drupal SitesImplementing High Performance Drupal Sites
Implementing High Performance Drupal Sites
 
Build a responsive website with drupal
Build a responsive website with drupalBuild a responsive website with drupal
Build a responsive website with drupal
 

Similar to Apc Memcached Confoo 2011

APC & Memcache the High Performance Duo
APC & Memcache the High Performance DuoAPC & Memcache the High Performance Duo
APC & Memcache the High Performance DuoAnis Berejeb
 
Barcelona apc mem2010
Barcelona apc mem2010Barcelona apc mem2010
Barcelona apc mem2010isnull
 
Caching with Memcached and APC
Caching with Memcached and APCCaching with Memcached and APC
Caching with Memcached and APCBen Ramsey
 
Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES
Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICESSpring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES
Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICESMichael Plöd
 
Caching and tuning fun for high scalability @ phpBenelux 2011
Caching and tuning fun for high scalability @ phpBenelux 2011Caching and tuning fun for high scalability @ phpBenelux 2011
Caching and tuning fun for high scalability @ phpBenelux 2011Wim Godden
 
Caching and tuning fun for high scalability @ FrOSCon 2011
Caching and tuning fun for high scalability @ FrOSCon 2011Caching and tuning fun for high scalability @ FrOSCon 2011
Caching and tuning fun for high scalability @ FrOSCon 2011Wim Godden
 
Cache all the things - A guide to caching Drupal
Cache all the things - A guide to caching DrupalCache all the things - A guide to caching Drupal
Cache all the things - A guide to caching Drupaldigital006
 
Zend Con 2008 Slides
Zend Con 2008 SlidesZend Con 2008 Slides
Zend Con 2008 Slidesmkherlakian
 
Writing Plugged-in Java EE Apps: Jason Lee
Writing Plugged-in Java EE Apps: Jason LeeWriting Plugged-in Java EE Apps: Jason Lee
Writing Plugged-in Java EE Apps: Jason Leejaxconf
 
Caching Data For Performance
Caching Data For PerformanceCaching Data For Performance
Caching Data For PerformanceDave Ross
 
Zend_Cache: how to improve the performance of PHP applications
Zend_Cache: how to improve the performance of PHP applicationsZend_Cache: how to improve the performance of PHP applications
Zend_Cache: how to improve the performance of PHP applicationsEnrico Zimuel
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalabilityWim Godden
 
Designing enterprise drupal
Designing enterprise drupalDesigning enterprise drupal
Designing enterprise drupalJason Burnett
 
Jug Lugano - Scale over the limits
Jug Lugano - Scale over the limitsJug Lugano - Scale over the limits
Jug Lugano - Scale over the limitsDavide Carnevali
 
잘 알려지지 않은 Php 코드 활용하기
잘 알려지지 않은 Php 코드 활용하기잘 알려지지 않은 Php 코드 활용하기
잘 알려지지 않은 Php 코드 활용하기형우 안
 
Heapoff memory wtf
Heapoff memory wtfHeapoff memory wtf
Heapoff memory wtfOlivier Lamy
 
Caching for Cash: Caching
Caching for Cash: CachingCaching for Cash: Caching
Caching for Cash: CachingScott MacVicar
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalabilityWim Godden
 
HTML5: Building the Next Generation of Web Applications
HTML5: Building the Next Generation of Web ApplicationsHTML5: Building the Next Generation of Web Applications
HTML5: Building the Next Generation of Web ApplicationsChrome Developer Relations
 

Similar to Apc Memcached Confoo 2011 (20)

APC & Memcache the High Performance Duo
APC & Memcache the High Performance DuoAPC & Memcache the High Performance Duo
APC & Memcache the High Performance Duo
 
Barcelona apc mem2010
Barcelona apc mem2010Barcelona apc mem2010
Barcelona apc mem2010
 
Caching with Memcached and APC
Caching with Memcached and APCCaching with Memcached and APC
Caching with Memcached and APC
 
Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES
Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICESSpring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES
Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES
 
Caching and tuning fun for high scalability @ phpBenelux 2011
Caching and tuning fun for high scalability @ phpBenelux 2011Caching and tuning fun for high scalability @ phpBenelux 2011
Caching and tuning fun for high scalability @ phpBenelux 2011
 
Caching and tuning fun for high scalability @ FrOSCon 2011
Caching and tuning fun for high scalability @ FrOSCon 2011Caching and tuning fun for high scalability @ FrOSCon 2011
Caching and tuning fun for high scalability @ FrOSCon 2011
 
PostgreSQL Query Cache - "pqc"
PostgreSQL Query Cache - "pqc"PostgreSQL Query Cache - "pqc"
PostgreSQL Query Cache - "pqc"
 
Cache all the things - A guide to caching Drupal
Cache all the things - A guide to caching DrupalCache all the things - A guide to caching Drupal
Cache all the things - A guide to caching Drupal
 
Zend Con 2008 Slides
Zend Con 2008 SlidesZend Con 2008 Slides
Zend Con 2008 Slides
 
Writing Plugged-in Java EE Apps: Jason Lee
Writing Plugged-in Java EE Apps: Jason LeeWriting Plugged-in Java EE Apps: Jason Lee
Writing Plugged-in Java EE Apps: Jason Lee
 
Caching Data For Performance
Caching Data For PerformanceCaching Data For Performance
Caching Data For Performance
 
Zend_Cache: how to improve the performance of PHP applications
Zend_Cache: how to improve the performance of PHP applicationsZend_Cache: how to improve the performance of PHP applications
Zend_Cache: how to improve the performance of PHP applications
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalability
 
Designing enterprise drupal
Designing enterprise drupalDesigning enterprise drupal
Designing enterprise drupal
 
Jug Lugano - Scale over the limits
Jug Lugano - Scale over the limitsJug Lugano - Scale over the limits
Jug Lugano - Scale over the limits
 
잘 알려지지 않은 Php 코드 활용하기
잘 알려지지 않은 Php 코드 활용하기잘 알려지지 않은 Php 코드 활용하기
잘 알려지지 않은 Php 코드 활용하기
 
Heapoff memory wtf
Heapoff memory wtfHeapoff memory wtf
Heapoff memory wtf
 
Caching for Cash: Caching
Caching for Cash: CachingCaching for Cash: Caching
Caching for Cash: Caching
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalability
 
HTML5: Building the Next Generation of Web Applications
HTML5: Building the Next Generation of Web ApplicationsHTML5: Building the Next Generation of Web Applications
HTML5: Building the Next Generation of Web Applications
 

More from Bachkoutou Toutou

Making php see, confoo 2011
Making php see, confoo 2011 Making php see, confoo 2011
Making php see, confoo 2011 Bachkoutou Toutou
 
Sean coates fifty things and tricks, confoo 2011
Sean coates fifty things and tricks, confoo 2011Sean coates fifty things and tricks, confoo 2011
Sean coates fifty things and tricks, confoo 2011Bachkoutou Toutou
 
hacking your website with vega, confoo2011
hacking your website with vega, confoo2011hacking your website with vega, confoo2011
hacking your website with vega, confoo2011Bachkoutou Toutou
 
Premiers pas dans les extensions PHP, Pierrick Charron, Confoo 2011
Premiers pas dans les extensions PHP, Pierrick Charron, Confoo 2011Premiers pas dans les extensions PHP, Pierrick Charron, Confoo 2011
Premiers pas dans les extensions PHP, Pierrick Charron, Confoo 2011Bachkoutou Toutou
 
Kill bottlenecks with gearman, sphinx, and memcached, Confoo 2011
Kill bottlenecks with gearman, sphinx, and memcached, Confoo 2011Kill bottlenecks with gearman, sphinx, and memcached, Confoo 2011
Kill bottlenecks with gearman, sphinx, and memcached, Confoo 2011Bachkoutou Toutou
 
Zend Framework 2, What's new, Confoo 2011
Zend Framework 2, What's new, Confoo 2011Zend Framework 2, What's new, Confoo 2011
Zend Framework 2, What's new, Confoo 2011Bachkoutou Toutou
 
Connecting web Applications with Desktop, confoo 2011
Connecting web Applications with Desktop, confoo 2011Connecting web Applications with Desktop, confoo 2011
Connecting web Applications with Desktop, confoo 2011Bachkoutou Toutou
 
Connecting Web Application and Desktop, confoo 2011, qafoo
Connecting Web Application and Desktop, confoo 2011, qafooConnecting Web Application and Desktop, confoo 2011, qafoo
Connecting Web Application and Desktop, confoo 2011, qafooBachkoutou Toutou
 
99 problems but the search aint one, confoo 2011, andrei zmievski
99 problems but the search aint one, confoo 2011, andrei zmievski99 problems but the search aint one, confoo 2011, andrei zmievski
99 problems but the search aint one, confoo 2011, andrei zmievskiBachkoutou Toutou
 
Php Inside - confoo 2011 - Derick Rethans
Php Inside -  confoo 2011 - Derick RethansPhp Inside -  confoo 2011 - Derick Rethans
Php Inside - confoo 2011 - Derick RethansBachkoutou Toutou
 
WebShell - confoo 2011 - sean coates
WebShell - confoo 2011 - sean coatesWebShell - confoo 2011 - sean coates
WebShell - confoo 2011 - sean coatesBachkoutou Toutou
 
Stress Free Deployment - Confoo 2011
Stress Free Deployment  - Confoo 2011Stress Free Deployment  - Confoo 2011
Stress Free Deployment - Confoo 2011Bachkoutou Toutou
 
Confoo 2011 - Advanced OO Patterns
Confoo 2011 - Advanced OO PatternsConfoo 2011 - Advanced OO Patterns
Confoo 2011 - Advanced OO PatternsBachkoutou Toutou
 

More from Bachkoutou Toutou (14)

Making php see, confoo 2011
Making php see, confoo 2011 Making php see, confoo 2011
Making php see, confoo 2011
 
Sean coates fifty things and tricks, confoo 2011
Sean coates fifty things and tricks, confoo 2011Sean coates fifty things and tricks, confoo 2011
Sean coates fifty things and tricks, confoo 2011
 
hacking your website with vega, confoo2011
hacking your website with vega, confoo2011hacking your website with vega, confoo2011
hacking your website with vega, confoo2011
 
Premiers pas dans les extensions PHP, Pierrick Charron, Confoo 2011
Premiers pas dans les extensions PHP, Pierrick Charron, Confoo 2011Premiers pas dans les extensions PHP, Pierrick Charron, Confoo 2011
Premiers pas dans les extensions PHP, Pierrick Charron, Confoo 2011
 
Kill bottlenecks with gearman, sphinx, and memcached, Confoo 2011
Kill bottlenecks with gearman, sphinx, and memcached, Confoo 2011Kill bottlenecks with gearman, sphinx, and memcached, Confoo 2011
Kill bottlenecks with gearman, sphinx, and memcached, Confoo 2011
 
Zend Framework 2, What's new, Confoo 2011
Zend Framework 2, What's new, Confoo 2011Zend Framework 2, What's new, Confoo 2011
Zend Framework 2, What's new, Confoo 2011
 
Connecting web Applications with Desktop, confoo 2011
Connecting web Applications with Desktop, confoo 2011Connecting web Applications with Desktop, confoo 2011
Connecting web Applications with Desktop, confoo 2011
 
Connecting Web Application and Desktop, confoo 2011, qafoo
Connecting Web Application and Desktop, confoo 2011, qafooConnecting Web Application and Desktop, confoo 2011, qafoo
Connecting Web Application and Desktop, confoo 2011, qafoo
 
99 problems but the search aint one, confoo 2011, andrei zmievski
99 problems but the search aint one, confoo 2011, andrei zmievski99 problems but the search aint one, confoo 2011, andrei zmievski
99 problems but the search aint one, confoo 2011, andrei zmievski
 
Php Inside - confoo 2011 - Derick Rethans
Php Inside -  confoo 2011 - Derick RethansPhp Inside -  confoo 2011 - Derick Rethans
Php Inside - confoo 2011 - Derick Rethans
 
WebShell - confoo 2011 - sean coates
WebShell - confoo 2011 - sean coatesWebShell - confoo 2011 - sean coates
WebShell - confoo 2011 - sean coates
 
Stress Free Deployment - Confoo 2011
Stress Free Deployment  - Confoo 2011Stress Free Deployment  - Confoo 2011
Stress Free Deployment - Confoo 2011
 
Xdebug confoo11
Xdebug confoo11Xdebug confoo11
Xdebug confoo11
 
Confoo 2011 - Advanced OO Patterns
Confoo 2011 - Advanced OO PatternsConfoo 2011 - Advanced OO Patterns
Confoo 2011 - Advanced OO Patterns
 

Apc Memcached Confoo 2011

  • 1. Apc & Memcached the High- Performance Duo ConFoo 2011 Ilia Alshanetsky @iliaa Wednesday, March 9, 2011
  • 3. What is APC? • Primarily designed to accelerate script performance via opcode caching • Extends opcode caching to facilitate user-data caching • Actively maintained • It even works on Windows!! Wednesday, March 9, 2011
  • 4. Opcode Caching Default Mode With APC PHP Script PHP Script APC Opcode Cache Zend Compile Zend Compile No Data Cached? Zend Execute Yes Cache Opcode Function/Method Require/Include Call Zend Execute Function/Method Require/Include Call Wednesday, March 9, 2011
  • 5. APC User-Cache • Allows you to apply the same caching logic to your data as applied to PHP scripts. Slide Motto: Not everything has to be real-time! Wednesday, March 9, 2011
  • 6. APC in Practice // store an array of values for 1 day, referenced by "identifier" if (!apc_add("identifier", array(1,2,3), 86400)) {     // already exists? let's update it instead     if (!apc_store("identifier", array(1,2,3), 86400)) {         // uh, oh, b0rkage     } } $ok = null; // fetch value associated with "identified" and // put success state into $ok variable $my_array = apc_fetch("identifier", $ok); if ($ok) {     // changed my mind, let's delete it     apc_delete("identifier"); } Wednesday, March 9, 2011
  • 7. Let’s be lazy // create or update an array of values for 1 day if (!apc_store("identifier", array(1,2,3), 86400)) { // uh, oh, b0rkage mail("gopal, brian, kalle", "you broke my code", "fix it!"); } If you don’t care whether your are adding or updating values you can just use apc_store() and keep your code simpler. Wednesday, March 9, 2011
  • 8. Don’t Delete • Deleting from cache is expensive as it may need to re-structure internal hash tables. • Rely on auto-expiry functionality instead • Or an off-stream cron job to clean up stale cache entries • In many cases it is simpler just to start from scratch. apc_clear_cache(“user”) Wednesday, March 9, 2011
  • 9. Advantages of APC • If you (or your ISP) uses opcode caching, chances are it is already there. • Really efficient at storing simple types (scalars & arrays) • Really simple to use, can’t get any easier… • Fairly stable Wednesday, March 9, 2011
  • 10. APC Limitations • PHP only, can’t talk to other “stuff ” • Not distributed, local only • Opcode + User cache == all eggs in one basket Wednesday, March 9, 2011
  • 11. Memcached • Interface to Memcached - a distributed caching system • Provides Object Oriented interface to caching system • Offers a built-in session handler • Can only be used for “user” caching • Purpose built, so lots of nifty features Wednesday, March 9, 2011
  • 12. Memcache vs Memcached • Memcached Advantages • Faster • Igbinary serializer • fastlz compression • Multi-Server Interface • Fail-over callback support Wednesday, March 9, 2011
  • 13. Basics in Practice $mc = new MemCached(); // connect to memcached on local machine, on default port $mc->addServer('localhost', '11211'); // try to add an array with a retrieval key for 1 day if (!$mc->add('key', array(1,2,3), 86400)) {     // if already exists, let's replace it     if (!$mc->replace('key', array(1,2,3), 86400)) {         die("Critical Error");     } } // let's fetch our data if (($data = $mc->get('key')) !== FALSE) {     // let's delete it now     $mc->delete('key'); // RIGHT NOW! } Wednesday, March 9, 2011
  • 14. Data Retrieval Gotcha(s) $mc = new MemCached(); $mc->addServer('localhost', '11211'); $mc->add('key', FALSE); if (($data = $mc->get('key')) !== FALSE) {   die("Not Found?"); // not true // The value could be FALSE, you // need to check the response code } // The “right” way! if ( (($data = $mc->get('key')) === FALSE) && ($mc->getResultCode() != MemCached::RES_SUCCESS) ) {   die("Not Found"); } Wednesday, March 9, 2011
  • 15. Interface Basics Continued... $mc = new MemCached(); // on local machine we can connect via Unix Sockets for better speed $mc->addServer('/var/run/memcached/11211.sock', 0); // add/or replace, don't care just get it in there // without expiration parameter, will remain in cache “forever” $mc->set('key1', array(1,2,3)); $key_set = array('key1' => “foo”, 'key1' => array(1,2,3)); // store multiple keys at once for 1 hour $mc->setMulti($key_set, 3600); // get multiple keys at once $data = $mc->getMulti(array_keys($key_set)); /* array( For multi-(get|set), all ops     'key1' => ‘foo’     'key2' => array(1,2,3) must succeed for ) */ successful return. Wednesday, March 9, 2011
  • 16. Multi-Server Environment $mc = new MemCached(); // add multiple servers to the list // as many servers as you like can be added $mc->addServers(array( array('localhost', 11211, 80), // high-priority 80% array('192.168.1.90', 11211, 20)// low-priority 20% )); // You can also do it one at a time, but this is not recommended $mc->addServer('localhost', 11211, 80); $mc->addServer('192.168.1.90', 11211, 20); // Get a list of servers in the pool $mc->  getServerList(); // array(array(‘host’ => … , ‘port’ => … ‘weight’ => …)) Wednesday, March 9, 2011
  • 17. Data Segmentation • Memcached interface allows you to store certain types of data on specific servers $mc = new MemCached(); $mc->addServers( … ); // Add data_key with a value of “value” for 10 mins to server // identified by “server_key” $mc->addByKey('server_key', 'data_key', 'value', 600); // Fetch key from specific server $mc->getByKey('server_key', 'data_key'); // Add/update key on specific server $mc->setByKey('server_key', 'data_key', 'value', 600); // Remove key from specific server $mc->deleteByKey('server_key', 'data_key'); Wednesday, March 9, 2011
  • 18. And there is more ... • The specific-server interface also supports multi-(get|set) $mc = new MemCached(); $mc->addServers( … ); $key_set = array('key2' => “foo”, 'key1' => array(1,2,3)); // store multiple keys at once for 1 hour $mc->setMultiByKey('server_key', $key_set, 3600); // get multiple keys at once $data = $mc->getMultiByKey('server_key', array_keys($key_set)); Wednesday, March 9, 2011
  • 19. Delayed Data Retrieval • One of the really neat features of Memcached extension is the ability to execute the “fetch” command, but defer the actual data retrieval until later. • Particularly handy when retrieving many keys that won’t be needed until later. Wednesday, March 9, 2011
  • 20. Delayed Data Retrieval $mc = new MemCached(); $mc->addServer('localhost', '11211'); $mc->getDelayed(array('key')); // parameter is an array of keys /* some PHP code that does “stuff” */ // Fetch data one record at a time while ($data = $mc->fetch()) { ... } // Fetch all data in one go $data = $mc->fetchAll(); Wednesday, March 9, 2011
  • 21. Data Compression • In many cases performance can be gained by compressing large blocks of data. Since in most cases network IO is more expensive then CPU speed + RAM. $mc = new MemCached(); $mc->addServer('localhost', 11211); // enable compression $mc->setOption(Memcached::OPT_COMPRESSION, TRUE); Related INI settings (INI_ALL) Other possible value is zlib memcached.compression_type=fastlz minimum compression rate memcached.compression_factor=1.3 minimum data size to compress memcached.compression_threshold=2000 Wednesday, March 9, 2011
  • 22. Counter Trick $mc = new MemCached(); $mc->addServer('localhost', 11211); // add key position if does not already exist if (!$mc->add('key_pos', 1)) {     // otherwise increment it     $position = $mc->increment('key_pos'); } else {     $position = 1; } // add real value at the new position $mc->add('key_value_' . $position, array(1,2,3)); • Simplifies cache invalidation • Reduces lock contention (or eliminates it) Wednesday, March 9, 2011
  • 23. PHP Serialization If you are using memcached to store complex data type (arrays & objects), they will need to be converted to strings for the purposes of storage, via serialization. Memcached can make use of igbinary serializer that works faster (~30%) and produces more compact data set (up-to 45% smaller) than native PHP serializer. https://github.com/igbinary Wednesday, March 9, 2011
  • 24. Enabling Igbinary Install Memcached extension with --enable-memcached-igbinary $mc = new MemCached(); $mc->addServer('localhost', 11211); // use Igbinary serializer $mc->setOption( Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_IGBINARY ); Wednesday, March 9, 2011
  • 25. Utility Methods Array (        [server:port]  =>  Array                (                        [pid]  =>  4933 $mc = new MemCached();                        [uptime]  =>  786123 $mc->addServer('localhost', 11211);                        [threads]  =>  1                        [time]  =>  1233868010                        [pointer_size]  =>  32 // memcached statistics gathering                        [rusage_user_seconds]  =>  0 $mc->getStats();                        [rusage_user_microseconds]  =>  140000                        [rusage_system_seconds]  =>  23                        [rusage_system_microseconds]  =>  210000                        [curr_items]  =>  145                        [total_items]  =>  2374                        [limit_maxbytes]  =>  67108864                        [curr_connections]  =>  2 // clear all cache entries                        [total_connections]  =>  151 $mc->flush();                        [bytes]  =>  20345                        [cmd_get]  =>  213343                        [cmd_set]  =>  2381 // clear all cache entries                        [get_hits]  =>  204223                        [get_misses]  =>  9120 // in 10 minutes                        [evictions]  =>  0 $mc->flush(600);                        [bytes_read]  =>  9092476                        [bytes_written]  =>  15420512                        [version]  =>  1.2.6                ) ) Wednesday, March 9, 2011
  • 26. Memcached Session Handler # Session settings session.save_handler # set to “memcached” session.save_path # set to memcache host server:port memcached.sess_prefix # Defaults to memc.sess.key Wednesday, March 9, 2011
  • 27. Advantages of Memcached • Allows other languages to talk to it • One instance can be shared by multiple servers • Failover & Redundancy • Nifty Features • Fairly stable Wednesday, March 9, 2011
  • 28. Perfection Attained? • Slower then APC, especially for array storage • Requires external daemon • You can forget about it on shared hosting, due to lack of authentication (D’oh!). Wednesday, March 9, 2011
  • 29. Any Questions? Feedback: http://joind.in/2806 Slides: http://ilia.ws Wednesday, March 9, 2011