How To Install Memcached on Ubuntu 14.04

Overview

Memcached is a high performance distributed memory object caching system. Originally created to speed up dynamic web application by reducing database load but can be used as generic caching system.

Objective

In this tutorial we’ll learn how-to install and configure Memcached on Ubuntu 14.04 (Trusty Tahr).

Prerequisite

This tutorial assumes you have clean install of Ubuntu Server 14.04. If you plan to install memcached on production server please check your system to ensure it’s ok to install Memcached.

Install Memcached

Before we install memcached let’s update our base install.

Update apt metadata.


$ sudo apt-get update

Update system packages to latest update.


$ sudo apt-get upgrade

Now we can install memcached from Ubuntu repository.


$ sudo apt-get install memcached

We can check memcached status using command below


$ sudo service memcached status
 * memcached is running

we can also check memcached status using netstat by finding memcached process


$ sudo netstat -naptu | grep memcached
tcp        0      0 127.0.0.1:11211         0.0.0.0:*               LISTEN      2602/memcached  
udp        0      0 127.0.0.1:11211         0.0.0.0:*                           2602/memcached  

Another way to check memcached service is by finding memcached default port from netstat output


$ sudo netstat -naptu | grep 11211
tcp        0      0 127.0.0.1:11211         0.0.0.0:*               LISTEN      2602/memcached  
udp        0      0 127.0.0.1:11211         0.0.0.0:*                           2602/memcached

We can also check connectivity to memcached using nc


$ nc -vz localhost 11211
Connection to localhost 11211 port [tcp/*] succeeded!

Configuring Memcached

Now we have memcached running let’s learn how to configure memcached. Memcached comes with two configuration files.

  • /etc/default/memcached
  • /etc/memcached.conf

We can enable or disable memcached on boot by changing parameter on /etc/default/memcached file. The default value on this file is


ENABLE_MEMCACHED=yes

To disable memcached on boot we need to change the line above to


ENABLE_MEMCACHED=no

Now let’s dissect /etc/memcached.conf configuration file.

Memcached Log

Memcached log configuration have one logfile parameter where we can specify log file path.


logfile /var/log/memcached.log

To increase verbosity of the log file we can add -v or -vv below the logfile line.

Memcached Memory Limit

By default memcached will use 64 MB of memory. Memcached doesn’t reserve the memory on start but the memory usage will grow as needed with the limit as specified in -m option.

To increase memory limit to 1GB for example, we can change the line below


-m 64

to


-m 1024

Memcached Port

We can configure memcached port using -p option. By default memcached use port 11211. If you change this port to non default you need to configure your application to also pointing to the same port.


-p 11211

User Running Memcached Process

This options is to configure which user run memcached process. We will rarely need to change this.


-u memcache

Memcached listen address

By default memcached will listen on all network interfaces. We can configure on which address memcached listen to.


-l 127.0.0.1

Please note that this is the only security mechanism that memcached have. If you plan to open your memcached server from another server ensure you add firewall to the memcached server.

If you use cloud service like Amazon Web Services you can use security groups.

If you use provider that doesn’t offer firewall or you install on your own infrastructure you can use iptables.

Memcache Limit incoming connections

We can limit connection using -c option. The default value is 1024 connections.


-c 1024

Further References

You can visit memcached.org to learn more about memcached.

Summary

In this tutorial we learned how-to install Memcached on Ubuntu 14.04 from Ubuntu repository. We also learned basic configuration of Memcached. I hope this tutorial will help you installing and configuring memcached on your infrastructure to improve your application performance.