Memcached and Apc

Apc:

Background
Installation
Config
What to check

Memcached:

Background
Installation
Config
What to check
Further reading

Apc:

Background

APC (Alternate PHP Cache) basically caches PHP scripts so it won't have to parse/execute the script next time. To use apc you have to modify your code or for CMS systems like WordPress use plugins. On installation edit /etc/php.ini to include extension=apc.so to enable apc. Apc stats can be viewed in a web page.

When should you Use Apc?  when your site has php processes taking up more than expected cpu or a site uses cms i.e wordpress/drupal

APC is discontinued (it will never support PHP 5.5), and will not be integrated into the PHP core.  Zend Optimizer+ is the preferred alternative.

Installation

to install apc http://www.tecmint.com/install-apc-alternative-php-cache-in-rhel-centos-fedora/ and it does work out the box

Config

To increase the size edit cat /etc/php.d/apc.ini and alter apc.shm_size= to the required size.

What to check

Copy the file apc.php (do a locate apc.php to find it) to your websites document root then browse to it.

The 3 keys things to check are:

1) hits%
2) misses%
3) cache full count - this number tell how many time the cache has been completely filled, if this number goes up too quickly you probably have to raise the size of the cache

Memcached:

Background

Memcached is a service that allows entire database tables to be stored in memory, drastically speeding up queries to those tables and alleviating database load.

Memcached allows you to point other servers at it but that in turn could lead to a single point of failure.
Php can be used with memcached but Mysql cannot be used with apc.

mysql 5.6 has a innodb plugin https://dev.mysql.com/doc/refman/5.6/en/innodb-memcached.html

Installation

installing http://www.cyberciti.biz/faq/howto-install-memcached-under-rhel-fedora-centos/ and it works out the box with new versions of mysql.

Config

to add to php /etc/php.ini extension=memcache.so and then load a phpinfo page to check its active, wordpress and other cms have plugings.
memchaced settings are in /etc/sysconfig/memcached

What to check

memcached-tool 127.0.0.1:11211 stats -- gives you a lot of output

a few rules to check if the parameters are setup correctly:
1) memcached-tool 127.0.0.1:11211 stats | grep "bytes" | head -1| awk '{print $2}' divided by 1024 divided by 1024 to get in bytes if that is near to memcached-tool 127.0.0.1:11211 stats | grep "limit" | awk '{print $2}' look at increasing memory
2) memcached-tool 127.0.0.1:11211 stats | grep "listen" if more than 0 look at increasing maxconn
3) memcached-tool 127.0.0.1:11211 stats | grep "get_[h,m]" if there is more misses than hits items are not cached or there is not enough memory so it has to remove some items to get some space for a new ones

Further reading

a fun story about memcached http://code.google.com/p/memcached/wiki/TutorialCachingStory

www.cyberciti.biz/faq/howto-install-memcached-under-rhel-fedora-centos/

https://www.digitalocean.com/community/articles/how-to-install-alternative-php-cache-apc-on-a-cloud-server-running-ubuntu-12-04
http://www.tecmint.com/install-apc-alternative-php-cache-in-rhel-centos-fedora/