Install and configure memcached on CentOS 6
Introduction
Memcached is an open source, high-performance memory object cache system which it is widely used in dynamic web applications to reduce load on database systems and as a fast storage for frequency accessed data. Its simple design promotes quick deployment and ease of development. Memcached stores data as key-value pairs in memory, thus very fast.
Requirements
Installation and configuration steps described here require root user privilege on the CentOS 6 Linux server.
Install memcached
Mecached is available on CentOS base YUM repository and can be installed as follows:
yum -y install memcached
Start the service and enable it on server boot:
service memcached start chkconfig memcached on
Configure memcached
By default, memcached service listens to port 11211 of all IP addresses available on server and has a maximum concurrent connections limit of 1024 and mamimum memory allocation of 64MB for the in-memory datastore. These settings can be changed in the configuration file /etc/sysconfig/memcached. It has following default configuration:
PORT="11211" USER="memcached" MAXCONN="1024" CACHESIZE="64" OPTIONS=""
PORT – port to which memcached service listens
USER – user ownership of memcached service
MAXCONN – maximum number of concurrent connections that memcached accepts
CACHESIZE – size of in-memory datastore
OPTIONS – additional options given to memcached service
To make memcached to listen to a specific IP address available in the server, use -l switch in OPTIONS setting. For example, to restrict memcached to listen to loopback IP address 127.0.0.1 on port 11211 with a maximum concurrent connections of 10240 and a maximum in-memory datastore of 1024MB, use following settings:
PORT="11211" USER="memcached" MAXCONN="10240" CACHESIZE="1024" OPTIONS="-l 127.0.0.1"
Change in configuration settings in /etc/sysconfig/memcached requires a service restart:
service memcached restart
To check status of memcached service, type following:
service memcached status
A status message similar to following shows that service is running fine:
memcached (pid 1934) is running...
If service is stopped, following status message is shown:
memcached is stopped
If there is any error in configuration values given to memcached, service fails to start and shows following status message:
memcached dead but pid file exists
Conclusion
In this article, we described the installation and configuration of memcached in-memory object cache system on a CentOS 6 Linux server. When properly configured and integrated with the web application, memcached can alleviate load on database server and can serve fast storage and retrieval of frequently accessed data, thus greatly improving application response time.