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:
debian@test-memcached:~$ sudo apt-get install memcached
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 127.0.0.1, 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.
memcache-top v0.6 (default port: 11211, color: on, refresh: 3 seconds)
INSTANCE USAGE HIT % CONN TIME EVICT/s GETS/s SETS/s READ/s WRITE/s
127.0.0.1:11211 88.9% 69.7% 1661 0.9ms 0.3 47 9 13.9K 9.8K
AVERAGE: 88.9% 69.7% 1661 0.9ms 0.3 47 9 13.9K 9.8K
TOTAL 0B/ 88.9% 69.7% 1661 0.9ms 0.3 47 9 13.9K 9.8K
(ctrl-c to quit.)
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.