Have a Question?

If you have any question you can ask below or enter what you are looking for!

Install and configure memcached on CentOS 7

Introduction

Memcached is a free, open source, high-performance, in-memory object cache system widely used in dynamic web applications to reduce load on database systems and as a fast datastore for frequency accessed data. It has a simple design which promotes quick deployment and ease of development. Memcached stores data as key-value pairs in memory, thus very fast.

This article decribes installation of memcached on a CentOS 7 Linux server and gives an introduction to memcached configuration options.

Requirements

Installation and configuration steps explained here require root user privilege on the CentOS 7 Linux server.

Install memcached

CentOS base YUM repository has RPM package for memcached service and it can be installed as follows:

yum -y install memcached

Start the service and enable it on server boot:

systemctl start memcached.service
systemctl enable memcached.service

Configure memcached

The default configuration of memcached makes it listen to port 11211 of all IP addresses available on server and has a maximum concurrent connections limit of 1024 and a maximum 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 size of 1024MB, use following settings:

PORT="11211"
USER="memcached"
MAXCONN="10240"
CACHESIZE="1024"
OPTIONS="-l 127.0.0.1"

To restrict memcached to listen to more than one IP addresses in the server, use comma separated list of IP addresses with -l option. For example, following option makes memcached to listen to IP addresses 127.0.0.1 and 192.168.0.100 available on server:

OPTIONS="-l 127.0.0.1,192.168.0.100"

Restart memcached to effect the configuration change in /etc/sysconfig/memcached:

systemctl restart memcached.service

Verify memcached listen sockets with ss program:

ss -lnp | grep memcached
udp  UNCONN  0  0    192.168.0.100:11211   *:*   users:(("memcached",1523,29))
udp  UNCONN  0  0        127.0.0.1:11211   *:*   users:(("memcached",1523,28))
tcp  LISTEN  0  0    192.168.0.100:11211   *:*   users:(("memcached",1523,27))
tcp  LISTEN  0  0        127.0.0.1:11211   *:*   users:(("memcached",1523,26))
tcp  LISTEN  0  0    192.168.0.100:11211   *:*   users:(("memcached",1523,27))
tcp  LISTEN  0  0        127.0.0.1:11211   *:*   users:(("memcached",1523,26))

As you can see, memcached listens to TCP and UDP ports by default. To disable, listening to UDP port, use -U 0 option:

OPTIONS="-l 127.0.0.1,192.252.214.229 -U 0"

Restart memcached and verify the change with ss:

ss -lnp | grep memcached
tcp  LISTEN  0  0    192.168.0.100:11211   *:*   users:(("memcached",1789,27))
tcp  LISTEN  0  0        127.0.0.1:11211   *:*   users:(("memcached",1789,26))
tcp  LISTEN  0  0    192.168.0.100:11211   *:*   users:(("memcached",1789,27))
tcp  LISTEN  0  0        127.0.0.1:11211   *:*   users:(("memcached",1789,26))

To check status of memcached service, type following:

systemctl status memcached.service

A status message similar to following shows that service is running fine:

memcached.service - Memcached
 Loaded: loaded (/usr/lib/systemd/system/memcached.service; enabled)
 Active: active (running) since Sun 2014-12-14 02:19:45 EDT; 2min 30s ago
 Main PID: 831 (memcached)
 CGroup: /system.slice/memcached.service
 └─831 /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024

Dec 14 02:19:45 server.subhosting.net systemd[1]: Started Memcached.

If service is stopped, the status message may similar to following:

memcached.service - Memcached
 Loaded: loaded (/usr/lib/systemd/system/memcached.service; enabled)
 Active: inactive (dead) since Sun 2014-12-14 02:25:02 EDT; 1min 27s ago
 Main PID: 831 (code=killed, signal=TERM)

Dec 14 02:19:45 server.subhosting.net systemd[1]: Started Memcached.
Dec 14 02:25:02 server.subhosting.net systemd[1]: Stopping Memcached...
Dec 14 02:25:02 server.subhosting.net systemd[1]: Stopped Memcached.

If there is any error in configuration values given to memcached, service fails to start and shows following status message which may include details of error occured:

memcached.service - Memcached
 Loaded: loaded (/usr/lib/systemd/system/memcached.service; enabled)
 Active: failed (Result: exit-code) since Sun 2014-12-14 02:28:24 EDT; 2s ago
 Process: 1013 ExecStart=/usr/bin/memcached -u $USER -p $PORT -m $CACHESIZE -c $MAXCONN $OPTIONS (code=exited, status=1/FAILURE)
 Main PID: 1013 (code=exited, status=1/FAILURE)

Dec 14 02:28:24 server.subhosting.net systemd[1]: Started Memcached.
Dec 14 02:28:24 server.subhosting.net memcached[1013]: /usr/bin/memcached: option requires an argument -- 'l'
Dec 14 02:28:24 server.subhosting.net memcached[1013]: Illegal argument "?"
Dec 14 02:28:24 server.subhosting.net systemd[1]: memcached.service: main process exited, code=exited, status=1/FAILURE
Dec 14 02:28:24 server.subhosting.net systemd[1]: Unit memcached.service entered failed state.

Conclusion

In this article, we described the installation and configuration of memcached in-memory object cache system on a CentOS 7 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>