Install & Configure Memcached

Memcached is a very simple key-value caching system that is often used in web environments, especially by PHP applications. This tutorial will show you how to install and configure a Memcached instance on a Debian GNU/Linux 9 system.

Installing the package

The first step is to install the package provided by Debian:

This is pretty straightforward thanks to the Debian package management system. After the installation, an instance will be started via systemd using the stock Debian-provided configuration file. This instance will allocate 64 MB of RAM, bind itself on port TCP/11211 on, under the memcache UNIX user.

Configuring the daemon

The memcached daemon is configured through the /etc/memcached.conf file.

The file basically contains raw command-line switches and options to tune the memcached daemon. The most commonly used settings are:

The maximum amount of memory memcached may use.

The TCP port to bind on.

The UNIX user under which the daemon will run.

The IP address on which to listen to.

The maximum allowed simultaneous connections.

Return an error when the memory is exhausted instead of removing objects. Use with caution!

The 64 MB default can be altered depending on the needs of your software and the amount of available memory on the server. Keep in mind that more isn’t always better — storing useless objects in the cache can be counter-productive. We recommend that you check the hit ratio before and after adjusting this variable to ensure that the cache is efficient.

Keeping it running

It is important to monitor your memcached instances to ensure they are properly running. A tool that we, at Jaguar Network, use to fine-tune our customers’ memcached instances is memcache-top. This is a simple Perl script that will get the stats from running memcached instances and display them so they can be easily interpreted.

There are several valuable informations in the output.

This column gives the amount of used memory. In optimal conditions, it shouldn’t reach 100%.

This column shows the hit rate of the instance. The higher, the better.

The number of evicted entries per second. The lower, the better.

The number of read and write queries handled by the memcached instance, per second.

Those informations will help you tune memcached properly.

Install & Configure Memcached