07.06.2023

Install memcached on Ubuntu 20.04

What is memcached and why should you use it

By default, much of "temporary" data is stores to the files on the disk. And it's so slow even if this drive is modern SSD. Obvious way to increase system performance is "drop" temp-files to the RAM. Most used method to do this is memcached.

Before the installation

To install secure service for caching you need:

Install the service and tools

Setup procedure is very-very simple. Just do this:

Authorize as privileged user and update system software cache
sudo -s
apt-get update

Main package and support tools installation:
apt-get install libmemcached-tools memcached -y

Basic configuration

Caching service stores its settings into the file /etc/memcached.conf. The most interesting options is:

-m # how much memory is reserves into the RAM for caching
-p # used TCP-port which service will listen
-U # UDP-port (optional)
-l # service-listening IP-address, usually 127.0.0.1 for local services or "external" address for "remote"
-c # how much simultaneous connections are allowed
-S # this flag needed to use authentication (see below)

To accept incoming connections please permit this on the system firewall side:

ufw allow <PORT>

Increase the safety

In case your server is accessible from the outside, some carrying about security would be nice.  In next steps we will implement simple authentication for memcached. Make follow steps to do it:

apt-get -y install sasl2-bin

mkdir /etc/sasl2; nano /etc/sasl2/memcached.conf

saslpasswd2 -c -f /etc/sasl2/memcached-sasldb2 -a memcached <MEMCACHED_USERNAME>
chown memcache:memcache /etc/sasl2/memcached-sasldb2

service memcached restart

Check the result

You can simply check is caching service run or not. Just run:

memcstat --servers="<LISTENING_IP>" --username=<MEMCACHED_USERNAME> --password=<MEMCACHED_PASSWORD>

You should see something like this

Summary

This instruction learns you what is caches using aim, how to setup, tuning and secure this.