How to run multiple memcached instances

I had copied a PrestaShop site to another domain. Both domains are on same server, using same Memcached server for caching. When i visit one of the site, resources like css/js files are loading from other website. To fix this, i configured 2 Memcached servers running on 2 different ports, each site use its own Memcached instance.

On Ubuntu server, i installed memcached and supervisor with

apt install -y memcached memcached

Create file

vi /etc/supervisor/conf.d/memcached.conf 

Inside add

[program:memcached2]
priority=200
command=/usr/bin/memcached -m 64 -p 11212 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached2.pid
user=memcache
autorestart=true
autostart=true
redirect_stderr=true

By default Memcached runs on port 11211, above configuration will make an instance of Memcached that runs on port 11212.

If you want to create another instance of memcached, duplicate above lines, in the lines, change

[program:memcached2]

with

[program:memcached3]

Find

command=/usr/bin/memcached -m 64 -p 11212 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached2.pid

Replace port with different port, say 11213 and /var/run/memcached/memcached2.pid with /var/run/memcached/memcached2.pid

Here is the complete configuration with 2 Memcached instances running under supervisiord

[program:memcached2]
priority=200
command=/usr/bin/memcached -m 64 -p 11212 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached2.pid
user=memcache
autorestart=true
autostart=true
redirect_stderr=true

[program:memcached3]
priority=200
command=/usr/bin/memcached -m 64 -p 11213 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached3.pid
user=memcache
autorestart=true
autostart=true
redirect_stderr=true

Reload supervisor

supervisorctl reload

Check status

supervisorctl status

Here is an example with 4 memcached instances running

root@zen-keldysh:/etc/supervisor/conf.d# supervisorctl reload
Restarted supervisord
root@zen-keldysh:/etc/supervisor/conf.d# supervisorctl status
memcached_erikoisrahti           RUNNING   pid 337864, uptime 0:00:02
memcached_longdrink24            RUNNING   pid 337865, uptime 0:00:02
memcached_tulivesi               RUNNING   pid 337866, uptime 0:00:02
memcached_viskit                 RUNNING   pid 337867, uptime 0:00:02
root@zen-keldysh:/etc/supervisor/conf.d# 

Here are the configuration file for above 4 Memcached instance setup

root@server1:~# cat /etc/supervisor/conf.d/memcached.conf 
[program:memcached_longdrink24]
priority=200
command=/usr/bin/memcached -m 64 -p 11212 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached2.pid
user=memcache
autorestart=true
autostart=true
redirect_stderr=true

[program:memcached_viskit]
priority=200
command=/usr/bin/memcached -m 64 -p 11213 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached-viskit.pid
user=memcache
autorestart=true
autostart=true
redirect_stderr=true

[program:memcached_erikoisrahti]
priority=200
command=/usr/bin/memcached -m 64 -p 11214 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached-erikoisrahti.pid
user=memcache
autorestart=true
autostart=true
redirect_stderr=true

[program:memcached_tulivesi]
priority=200
command=/usr/bin/memcached -m 64 -p 11215 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached-tulivesi.pid
user=memcache
autorestart=true
autostart=true
redirect_stderr=true

root@server1:~# 

Comments

Leave a Reply

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