Vincent's Site

Setting up multiple Memcached instances

Date: 2024-12-16
Category: Tutorial
Tags: tips

In a previous post I showed how to add Caches to Django, In this post I will go over how you install and configure Memcached on Debian. I will also modify the configuration to allow us to run multiple instances of memcached on the same machine, this is usefull because we want to run multiple apps on the same machine.

Installing Memcached

First, install the memcached package via apt

sudo apt install memcached

Next, copy the default service file to /etc/systemd/system/

cp /lib/systemd/system/memcached.service /etc/systemd/system/memcached@.service

and edit that new file like so:

[Unit]
Description=memcached daemon (instance %i)
After=network.target
Documentation=man:memcached(1)

[Service]
ExecStart=/usr/share/memcached/scripts/systemd-memcached-wrapper /etc/memcached_%i.conf
PIDFile=/var/run/memcached/memcached_%i.pid

[Install]
WantedBy=multi-user.target

Then copy the default config and create an instance's config file:

cp /etc/memcached.conf /etc/memcached_vincent-blog.conf

and give it its own port (the -p option in the config) and the pidfile (-P on the bottom) to be the same as the instance (ie: /var/run/memcached/memcached_vincent-blog.pid)

Then stop the main memcached service and start the new instance

systemctl daemon-reload
systemctl stop memcached.service
systemctl disable memcached.service
systemctl start memcached@vincent-blog.service
systemctl enable memcached@vincent-blog.service

that's it! Now you can connect your site to 127.0.0.1:<port> and use memcached!

Previous post Next post