Linux – Install Memcached on CentOS
According to http://memcached.org/, Memcached is a “distributed memory object caching system”. What this means is that memcached will return stored values from memory rather than other storage mediums such as disk or database. How do you access these values? Well, depending upon the key construct you used, you just need to provide the memcached with the proper key and you will gain access to stored values (also known as a key-value pair).
Memcached provides a layer which sits between your application and your backend storage solution. What memcached does is allow you, the developer, to store commonly retrieved data into the cache layer so subsequent requests will use the cache version rather than querying your database or pinging your filesystem. As you can probably tell, this is a much faster solution and reduces the resources needed by your databases.
There is a lot of power that memcached provides, but as a developer you need to ensure your app can correctly harness this power. What I mean by this is; imagine you update a value in your database, then through your application you submit a form to confirm the value has indeed been changed, but for some reason you keep seeing the old value. Guess what? Your old value was cached and you optimized your application to use cache before hitting the database. As a developer you need to create a method of clearing cache when needed so you don’t bang your head on the desk trying to figure out why values you changed are not being respected by your application.
Now that you have a basic understanding of what memcached does, lets install it on your centos system.
Memcached is not available to your system by default (via YUM), you will have to gain access to rpmforge (a collection of rpms). If you don’t have rpmforge setup, follow this guide: http://melikedev.com/2010/03/10/centos-basic-usage-guide/ in the section RPMForge.
Now that you have rpmforge setup you should be able to get access to memcached. Try:
yum list memcached
If all looks good, then go ahead and install:
yum install memcached
It should also install about 10 more dependencies all from rpmforge, but shouldn’t have any negative impact on your system. When that’s finished you now have memcached installed on your system.
Now it’s time to configure. First we want to add a user to the system so we can run the memcached(aemon) as a non-root user:
Now we can edit memcached settings:
# /etc/sysconfig/memcached PORT="11211" USER="memcached" MAXCONN="1024" CACHESIZE="64" OPTIONS=""
Now you should be able to start your memcached server:
sudo -u memcached /etc/init.d/memcached start
If you get an permission related error regarding the lock file, you will have to ensure that the memcached user can write the log file to /var/lock/subsys (This lock file ensures you don’t run multiple memcacheds at the sametime). What I did is:
chown root:daemon /var/lock/subsys #allow daemon group, group control over subsys chmod 775 /var/lock/subsys #make subsys dir writable by group owner
You can now test your memcached server by following these steps outlined here: http://melikedev.com/category/memcache/.
Now that you have an up and running cache server you should develop a caching class within your app that will access your memcached server so you can save commonly run query results within the caching layer. Don’t get carried away though, try to be strategic about what you are populating your cache with, and don’t forget to develop some mechanism for clearing cache when needed.
- virsh – Refusing to undefine while domain managed save image exists
- Git – Revert Multiple Locally Changed Files
- Linux – SELinux – SEModule – Compile .pp module from .te file
- Linux – Using Curl to Test Response Headers
- Puppet – Wrong Header Line Format – Error
- CentOS – sealert – Convert audit messages into human readable (and understandable) format
- CentOS – PHP – Install Memcached (with a d) Stack
- Apache – Httpd – SELinux – Permission denied – File Permissions Deny Server Access
- MySQL – Percona – Release Notes – Identifying Changes Between Versions
- Linux – CentOS – Install Mycrypt