Categories: Ubuntu

How to install Memcached on Ubuntu

Memcached is an object caching system that helps to get a good response rate while getting data from web applications. It can be used with a long range of programming languages and protocols such as Python, Java, PHP, JS, Ruby, TCP, and UDP protocols. With the help of Memcached, database load can be reduced and increase the response of dynamic web applications, and it stores key-value pairs in memory. We quote here an example in simpler words to let you understand the usage of Memcached: let’s say we have a PHP-based application and a SQL Database, here we will fetch the data from DB and store it in Memcached to speed up the webpages. It stores key-value pairs in memory. Databases store data on your physical storage drive while Memcached stores data on the servers, thus nullifying the use of physical storage devices that resultantly increase the speed.

How
Sponsored
Memcached works

This section guides you to get the working flow of Memcached in simpler words, Memcached comprises of the four components (Server software, LRU, Client software, and Hashing algorithm), and the working mechanism of Memcached that is based on those components is listed below:

Step 1: Request of data by client : Firstly, a request is made by the client to Memcached, and it checks for the availability of data; if the data is available in the cache, the Memcached will return it, and in case if data is not available in the cache, it is retrieved from the database and saved in the Memcached.

Step 2: Update the Cache : The data is manipulated frequently, so if the information is changed or expired, Memcached updates the cache so that it contains updated data.

Generally, Memcached consists of several servers and many clients: clients and servers communicate through a hashing algorithm, clients’ instructions to the server are passed through this hash, and servers follow the hashing algorithms to determine where to store data. This phenomenon of Memcached makes the load distributable.

How to install Memcached on Ubuntu

This core part of writing will guide to install Memcached on Ubuntu:

Open the Ubuntu terminal (Alt+Ctrl+T) and update the system by using the command:

$ sudo apt update

Once the update command is executed, you can install the Memcached using the command written below:

$ sudo apt install memcached

To get extensive support for the Memcached server, you should add the tools library of Memcached; this library enables you to work alongside the Memcached server:

$ sudo apt install libmemcached-tools

After successful completion of this step, the Memcached service will be installed on your server, and you can check the status of this service by using the command given below, and it can be seen that the service is active:

$ sudo systemctl status memcached

How to configure Memcached service on Ubuntu

This section lists down the major methods to configure Memcached on your Ubuntu; by following this section, you will be able to manipulate the settings of the configuration file as well to enable this service for various applications.

How to make the connection of Memcached with the client

The configuration setting of this service resides inside file “/etc/memcached.conf,”; and you can edit the settings by accessing the configuration file using nano editor:

Sponsored
$ sudo nano /etc/memcached.conf

By opening the file, you will observe the following notable actions that this service takes when connecting to a client: Memcached listens to port 11211 and the IP address of localhost (127.0.0.1). For instance, if any client (other than where Memcached resides) requests to make a connection with the Memcached service, the IP address of that client must be replaced with the default IP address of the localhost (127.0.0.1). It is to notice that the client and server systems must be in a local area network. Moreover, after changing the data in the config file, you must restart the Memcached service by using the following command:

$ sudo systemctl restart memcached

And allow the firewall through port 11211 by executing the following command:

$ sudo ufw allow 11211/tcp

Now reload your firewall to successfully fetch the changes; you can do it simply by the following command:

$ sudo ufw reload

How to enable Memcached service for various applications

As we have mentioned in the introduction that it works with several languages and protocols. So, it is quite easy to enable the service for different applications.

For PHP based applications: If you want to get this service for PHP based applications, you must execute the following command:

$ sudo apt install php-memcached

For Python-based applications: For python-based applications, you have to add the python Memcached repository by using the pip package manager: pip is a package manager to install several packages from the python package index:

$ sudo pip install python-memcached

You may get an error that pip is not installed; so you can install the pip manager by the following command:

$ sudo apt install pip

Conclusion

Memcached is a service provider that distributes load to boost up the response of dynamic web pages, and it supports a long list of applications and few protocols as well. On realizing the importance of Memcached, this guide is compiled to get the basic use, working mechanism, and installation of this service on your Ubuntu. Moreover, we have also provided the process to configure this service and to get this service for several programming language-based applications. Resultantly, it would be beneficial for the users who want to learn Memcached from scratch to an advanced level.

Ubuntu Server Admin

Recent Posts

Ubuntu vs Debian: Linux Distributions Compared Deep Dive

Debian and Ubuntu are two popular Linux distributions. In this deep dive we will guide…

6 hours ago

How to Install Google Cloud BigQuery Python client library on Linux

In this article, we will see how to Install Google Cloud BigQuery Python client library…

2 days ago

Wallpaper Contest for Xfce 4.20 open for voting

Nov 15,2024 Wallpaper Contest for Xfce 4.20 open for voting The submission phase for the…

2 days ago

Canonical announces the first MicroCloud LTS release

MicroCloud 2.1.0 LTS is now available, expanding the number of Canonical infrastructure solutions with a…

3 days ago

Join Canonical in Paris at Dell Technologies Forum

Canonical is thrilled to be joining forces with Dell Technologies at the upcoming Dell Technologies…

4 days ago

Bringing automation to open source 5G software at Ubuntu Summit 2024

In today’s massive private mobile network (PMN) market, one of the most common approaches to…

5 days ago