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 7

Introduction

In previous article, Install and configure memcached on CentOS 7, we showed you how to install and configure memcached in-memory object cache system. The binary RPM installation explained in that article 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 file for each process.

Requirements

Installation and configuration steps described here require root user access to the CentOS 7 server. Memcached service needs to be installed from CentOS 7 base YUM repository.

Install memcached from YUM repository

Use following YUM command to install memcached package from CentOS 7 base repository:

yum -y install memcached

This installation provides a memcached instance with configuration file /etc/sysconfig/memcached.

Run multiple memcached processes

Assume that you have a requirement of running two memcached processes with following configurations:

config1 – configuration of first process

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

config2 – configuration of second process

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

Let us use config1 configuration for the default memcached process. For that, edit /etc/sysconfig/memcached using your favorite editor (say nano text editor) and make following settings in it:

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

Start the first memcached process:

systemctl start memcached.service

Enable it on server boot:

systemctl enable memcached.service

Check status of first memcached process; it should be running if no problem happened:

systemctl status memcached.service
memcached.service - Memcached
   Loaded: loaded (/usr/lib/systemd/system/memcached.service; enabled)
   Active: active (running) since Mon 2014-12-15 15:20:00 EDT; 17s ago
 Main PID: 24217 (memcached)
   CGroup: /system.slice/memcached.service
           └─24217 /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024 -l 127.0.0.1
Dec 15 15:20:00 localhost.localdomain systemd[1]: Starting Memcached...
Dec 15 15:20:00 localhost.localdomain systemd[1]: Started Memcached.

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

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

Create systemd unit file /etc/systemd/system/memcached2.service with following settings:

[Unit]
Description=Memcached2
Before=httpd.service
After=network.target

[Service]
Type=simple
EnvironmentFile=-/etc/sysconfig/memcached2
ExecStart=/usr/bin/memcached -u $USER -p $PORT -m $CACHESIZE -c $MAXCONN $OPTIONS

[Install]
WantedBy=multi-user.target

Start the second memcached process:

systemctl start memcached2.service

Enable it on server boot:

systemctl enable memcached2.service

Check status of second memcached process; it should be running if no problem happened:

systemctl status memcached2
memcached2.service - Memcached2
   Loaded: loaded (/etc/systemd/system/memcached2.service; enabled)
   Active: active (running) since Mon 2014-12-15 15:20:03 EDT; 20s ago
 Main PID: 24226 (memcached)
   CGroup: /system.slice/memcached2.service
           └─24226 /usr/bin/memcached -u memcached -p 11212 -m 128 -c 1024 -l 127.0.0.1

Dec 15 15:20:03 localhost.localdomain systemd[1]: Starting Memcached2...
Dec 15 15:20:03 localhost.localdomain systemd[1]: Started Memcached2.

To verify listening sockets of memcached processes, use following ss command:

ss -lnp | grep memcached
tcp UNCONN 0 0 127.0.0.1:11211 *:* users:(("memcached",24217,27))
tcp UNCONN 0 0 127.0.0.1:11212 *:* users:(("memcached",24226,27))
tcp LISTEN 0 128 127.0.0.1:11211 *:* users:(("memcached",24217,26))
tcp LISTEN 0 128 127.0.0.1:11212 *:* users:(("memcached",24226,26))

You can see that the two memcached processes are listening to ports 11211 and 11212 respectively on loopback IP address 127.0.0.1.

If you require more memcached processes, create separate configuration and systemd unit files. Then manage the processes using systemctl as explained above.

Conclusion

In this article, we showed you a method to run multiple memcached processes on a CentOS 7 Linux system by extending the default binary RPM installation. Each extra memcached process owns a separate configuration file and is managed by a separate systemd service unit file.

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>