Have a Question?

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

How to run multiple memcached processes in CentOS 6

Introduction

The previous article, Install and configure memcached on CentOS 6, showed you installation and configuration of memcached in-memory object cache system. By default, this RPM installation provides a single instance of memcached process. In this article, we show you how to extend this installation to run multiple memcached processes with separate configuration for each process. The method used by us is explained with a specific example.

Requirements

Installation and configuration steps described here require root user access to the CentOS 6 server. To follow this method, memcached needs to be installed from binary RPM available on CentOS 6 base YUM repository.

Install memcached

Memcached package is available on CentOS 6 YUM repository. Type following command to install the package:

yum -y install memcached

The default installation provides a memcached process with configuration file /etc/sysconfig/memcached and service management script /etc/init.d/memcached.

Run multiple memcached processes

Let us assume that we have a requirement to run three memcached processes with following configuration:

config1

listen address: 127.0.0.1
listen port: 11211
maximum concurrent connections: 1024
maximum in-memory cache size: 128MB

config2

listen address: 127.0.0.1
listen port: 11212
maximum concurrent connections: 2048
maximum in-memory cache size: 256MB

config3

listen address: 127.0.0.1
listen port: 11213
maximum concurrent connections: 5120
maximum in-memory cache size: 1024MB

Configure the default memcached process with config1 settings. For that, edit its configuration file /etc/sysconfig/memcached using your preferred text editor (say, nano text editor) and write following settings:

nano /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="128"
OPTIONS="-l 127.0.0.1"

Start the first memcached process:

service memcached start

Make it start on server boot:

chkconfig memcached on

To create the second memcached process with config2 settings, create its configuration file /etc/sysconfig/memcached2 with following settings:

nano /etc/sysconfig/memcached2
PORT="11212"
USER="memcached"
MAXCONN="2048"
CACHESIZE="256"
OPTIONS="-l 127.0.0.1"

Next, we need to create service management script /etc/init.d/memcached2 for the second memcached process. For this copy /etc/init.d/memcached to /etc/init.d/memcached2:

cp -pa /etc/init.d/memcached /etc/init.d/memcached2

The service management script contains process specific paths of configuration, process ID (PID) and lock files and a program name. We need to replace these parameters in /etc/init.d/memcached2 specifically for second process. Use sed program for this purpose.

Replace configuration file path /etc/sysconfig/memcached with /etc/sysconfig/memcached2:

sed -i 's%/etc/sysconfig/memcached%/etc/sysconfig/memcached2%g' /etc/init.d/memcached2

Replace PID file path /var/run/memcached/memcached.pid with /var/run/memcached/memcached2.pid:

sed -i 's%/var/run/memcached/memcached.pid%/var/run/memcached/memcached2.pid%g' /etc/init.d/memcached2

Replace program name “memcached” with “memcached2”

sed -i 's%prog="memcached"%prog="memcached2"%g' /etc/init.d/memcached2

Replace lock file path /var/lock/subsys/memcached with /var/lock/subsys/memcached2:

sed -i 's%/var/lock/subsys/memcached%/var/lock/subsys/memcached2%g' /etc/init.d/memcached2

Configuration file and service management script are ready for second memcached process. Start the process:

service memcached2 start

Enable it on server boot:

chkconfig memcached2 on

To create the third memcached process, create the configuration file /etc/sysconfig/memcached3 as follows:

nano /etc/sysconfig/memcached3
PORT="11213"
USER="memcached"
MAXCONN="5120"
CACHESIZE="1024"
OPTIONS="-l 127.0.0.1"

Copy /etc/init.d/memcached to /etc/init.d/memcached3

cp -pa /etc/init.d/memcached /etc/init.d/memcached3

Run sed replacement commands to make required modifications:

sed -i 's%/etc/sysconfig/memcached%/etc/sysconfig/memcached3%g' /etc/init.d/memcached3
sed -i 's%/var/run/memcached/memcached.pid%/var/run/memcached/memcached3.pid%g' /etc/init.d/memcached3
sed -i 's%prog="memcached"%prog="memcached3"%g' /etc/init.d/memcached3
sed -i 's%/var/lock/subsys/memcached%/var/lock/subsys/memcached3%g' /etc/init.d/memcached3

Start the process:

service memcached3 start

Enable it on server boot:

chkconfig memcached3 on

Verify the running memcached processes with ps command. It should show three memcached processes.

ps -ef | grep memcached
499 29108 1 0 07:35 ? 00:00:00 memcached -d -p 11212 -u memcached -m 256 -c 2048 -P /var/run/memcached/memcached2.pid -l 127.0.0.1
499 29131 1 0 07:35 ? 00:00:00 memcached -d -p 11213 -u memcached -m 1024 -c 5120 -P /var/run/memcached/memcached3.pid -l 127.0.0.1
499 29154 1 0 07:35 ? 00:00:00 memcached -d -p 11211 -u memcached -m 128 -c 1024 -P /var/run/memcached/memcached.pid -l 127.0.0.1

Verify memcached listening sockets with netstat command. It should show three memcached processes listening to port 11211, 11212 and 11213 respectively on loopback IP address 127.0.0.1.

netstat -plan | grep LISTEN | grep memcached
tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 29154/memcached
tcp 0 0 127.0.0.1:11212 0.0.0.0:* LISTEN 29108/memcached
tcp 0 0 127.0.0.1:11213 0.0.0.0:* LISTEN 29131/memcached

Each memcached process can be separately managed with service command. For instance,

to restart second memcached process, type:

service memcached2 restart
Stopping memcached2: [ OK ]
Starting memcached2: [ OK ]

to show the status of third memcached process, type:

service memcached3 status
memcached (pid 29131) is running...

Conclusion

In this article, we showed you a method for running multiple memcached processes by extending the default RPM based installation. Each additional memcached process is created by using separate configuration file and service management script.

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>