How to Install and Configure Memcached on Ubuntu?


Memcached is a free and open-source, high-performance, distributed memory caching system. It is commonly used to speed up dynamic web applications by caching data and objects in memory, which reduces number of times an application must query a database or other data store. Memcached is also commonly used in a microservices architecture, where it is used as a shared cache between different services.

In this article, we will discuss how to install and configure Memcached on Ubuntu.

Step 1: Install Memcached

The first step is to install Memcached package on your Ubuntu system. You can install it using following command −

sudo apt-get update
sudo apt-get install memcached

After installation is complete, you can start Memcached using following command −

sudo systemctl start memcached

Step 2: Configure Memcached

By default, Memcached listens on localhost ( interface and uses port 11211. This means that Memcached will only be accessible from same machine that it is running on. If you want to access Memcached from other machines, you need to change configuration file.

To do this, open Memcached configuration file using your favorite text editor. configuration file is located at /etc/memcached.conf.

sudo nano /etc/memcached.conf

In this file, you can change following settings −

  • -l − This sets IP address that Memcached listens on. By default, it is set to, which means that Memcached only listens on localhost interface. If you want to listen on all available network interfaces, you can set this to

  • -p − This sets port that Memcached listens on. By default, it is set to 11211.

  • -m − This sets amount of memory that Memcached is allowed to use. By default, it is set to 64 megabytes. You can increase this value depending on your needs.

After you have made your changes to configuration file, save it and exit text editor.

Step 3: Test Memcached

To test if Memcached is working properly, you can use telnet command to connect to Memcached and run some basic commands.

telnet localhost 11211

This will open a telnet session to Memcached. You can then enter following commands −

set mykey 0 60 5

This will store string "hello" under key "mykey" in Memcached for 60 seconds. You can then retrieve value of key by running following command −

get mykey

This should return value "hello". If it does, then Memcached is working properly.

Step 4: Configure Memcached for your Application

Once you have confirmed that Memcached is working properly, you can configure your application to use Memcached as a caching mechanism. exact steps for doing this will depend on programming language and framework that you are using.

Here is an example of how to use Memcached with PHP −

$memcached = new Memcached();
$memcached->addServer('localhost', 11211);

$key = 'mykey';
$value = $memcached->get($key);

if (!$value) {
   $value = 'hello';
   $memcached->set($key, $value, 60);

echo $value;

In this example, we create a new instance of Memcached class and add localhost server to it. We then try to retrieve value of key "my key" from Memcached. If value does not exist, we set it to "hello" and store it in Memcached with a TTL of 60 seconds. Finally, we print out value.

Here are some additional tips and best practices for using Memcached −

  • Use a consistent key naming convention − When using Memcached to cache data, it is important to use a consistent naming convention for your keys. This will make it easier to retrieve data later on. A common convention is to prefix key with name of application or service.

  • Monitor Memcached performance − Memcached has built-in performance metrics that can be monitored using tools like memcached-tool or telnet. Monitoring these metrics can help you identify potential performance bottlenecks and tune your Memcached configuration for optimal performance.

  • Use a client library − While it is possible to interact with Memcached using raw telnet commands, it is much easier and more efficient to use a client library for your programming language or framework. Most client libraries provide an object-oriented interface that simplifies process of storing and retrieving data from Memcached.

  • Use Memcached with a backup data store − Memcached is an in-memory cache, which means that data is lost when server is restarted or crashes. To prevent data loss, it is a good idea to use Memcached in conjunction with a backup data store, such as a database or a file system.

  • Use Memcached with a load balancer − To ensure that Memcached is highly available and scalable, it is a good idea to use it with a load balancer. This will distribute load across multiple Memcached servers, preventing any one server from becoming a bottleneck.

  • Use Memcached with a consistent hashing algorithm − When using Memcached with multiple servers, it is important to use a consistent hashing algorithm to distribute data across servers. Consistent hashing ensures that each key is always mapped to same server, even if number of servers in pool changes.

  • Set appropriate TTL values − When storing data in Memcached, it is important to set appropriate TTL (time-to-live) values for each key. TTL values determine how long a key should be stored in Memcached before it is considered stale and deleted. Setting appropriate TTL values can help prevent stale data from being served to users.

  • Use Memcached for read-heavy workloads − Memcached is most effective when used for read-heavy workloads, such as web applications that serve a large number of read requests. For write-heavy workloads, it may be more effective to use a database or other data store.

  • Scale Memcached horizontally − Memcached is designed to be horizontally scalable, which means that you can add more servers to your Memcached cluster as your application grows. Horizontal scaling allows you to increase capacity of your Memcached cluster without having to upgrade individual servers.

  • Use Memcached in conjunction with other caching mechanisms − While Memcached is a powerful caching mechanism, it may not be appropriate for all use cases. For example, if you need to cache complex data structures or maintain consistency across multiple nodes, you may want to use a different caching mechanism, such as Redis or a database cache. By using Memcached in conjunction with other caching mechanisms, you can achieve best of both worlds: high-performance caching for simple data types and more advanced caching for complex data types.

By following these additional tips and best practices, you can further optimize your use of Memcached and ensure that it meets performance and scalability needs of your application.


In this article, we have discussed how to install and configure Memcached on Ubuntu. Memcached is a powerful tool that can be used to speed up dynamic web applications by caching data and objects in memory. It is easy to install and configure, and can provide a significant performance boost for your application. With a little bit of effort, you can start using Memcached in your application today.

Updated on: 12-May-2023


Kickstart Your Career

Get certified by completing the course

Get Started