How to Install and Configure Memcached on CentOS 8


Memcached is a distributed memory object caching system that can significantly improve the performance of your website or application by storing frequently accessed data in memory, reducing the number of database queries required. In this article, we will guide you through the process of installing and configuring Memcached on CentOS 8.

Step 1: Install Memcached on CentOS 8

The first step is to install Memcached on your CentOS 8 system. You can do this by running the following command −

sudo dnf install memcached

This command will download and install Memcached on your system.

Step 2: Start Memcached and Enable it at Boot Time

After the installation is complete, you need to start the Memcached service and enable it to start automatically at boot time. You can do this by running the following command −

sudo systemctl start memcached
sudo systemctl enable memcached

The first command starts the Memcached service, and the second command enables it to start automatically at boot time.

Step 3: Configure Memcached

By default, Memcached listens on port 11211 and uses the UDP protocol. However, you can configure Memcached to listen on a different port and use the TCP protocol. To do this, you need to modify the Memcached configuration file.

sudo nano /etc/sysconfig/memcached

This will open the Memcached configuration file in the Nano text editor. You can modify the configuration file as follows −

PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="-l 127.0.0.1"

PORT − This specifies the port on which Memcached listens. You can change it to any port you want.

USER − This specifies the user that Memcached runs as.

MAXCONN − This specifies the maximum number of simultaneous connections that Memcached can handle.

CACHESIZE − This specifies the amount of memory that Memcached can use for caching data.

OPTIONS − This specifies additional options for Memcached. In this example, we set it to listen on the localhost only.

Save the changes and exit the Nano text editor.

Step 4: Test Memcached

After you have installed and configured Memcached, you can test it to make sure it is working correctly. You can do this by using the telnet command to connect to Memcached and set and retrieve a value.

telnet 127.0.0.1 11211
set mykey 0 60 5
hello
get mykey

This will connect to Memcached on port 11211 and set the value of the mykey key to hello with a TTL of 60 seconds. Then, it retrieves the value of the mykey key.

If everything is working correctly, you should see the following output −

STORED
VALUE mykey 0 5
hello
END

This means that Memcached was able to store and retrieve the value of the mykey key.

Step 5: Install the Memcached Extension for PHP

If you are using PHP for your website or application, you can install the Memcached extension for PHP to easily interact with Memcached from your PHP code.

You can do this by running the following command −

sudo dnf install php-memcached

This will download and install the Memcached extension for PHP.

Step 6: Configure the Memcached Extension for PHP

After you have installed the Memcached extension for PHP, you need to configure it to connect to Memcached.

You can do this by modifying the memcached.ini file located in the PHP configuration directory. You can find the directory by running the following command −

php --ini

This will display the location of the PHP configuration directory. Navigate to the directory and edit the memcached.ini file −

sudo nano /etc/php.d/40-memcached.ini

In the memcached.ini file, you need to add the following line −

extension=memcached.so

This line enables the Memcached extension for PHP.

Save the changes and exit the Nano text editor.

Step 7: Test the Memcached Extension for PHP

After you have installed and configured the Memcached extension for PHP, you can test it to make sure it is working correctly.

You can do this by creating a PHP file with the following code −

<?php

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

   $memcached->set('mykey', 'hello', 60);

   echo $memcached->get('mykey');

?>

This code creates a new instance of the Memcached class, connects to Memcached on localhost on port 11211, sets the value of the mykey key to hello with a TTL of 60 seconds, and retrieves the value of the mykey key.

If everything is working correctly, you should see the following output when you run the PHP file −

hello

This means that the Memcached extension for PHP was able to store and retrieve the value of the mykey key.

Use Memcached with a Load Balancer

If you have multiple web servers that serve your website or application, you can use a load balancer to distribute incoming requests across the servers. By using Memcached with a load balancer, you can ensure that all servers have access to the same cached data.

To do this, you need to configure Memcached to use a consistent hashing algorithm. This will ensure that the same key is always stored and retrieved from the same Memcached server. You can do this by modifying the OPTIONS line in the Memcached configuration file −

OPTIONS="-l 127.0.0.1 -o hash_algorithm=jenkins -o hash_mem_multiplier=1.1"

In this example, we use the jenkins hashing algorithm and set the hash_mem_multiplier option to 1.1, which increases the amount of memory available for caching data.

Monitor Memcached

To ensure that Memcached is running smoothly and efficiently, you should monitor it regularly. You can use tools like memcached-tool or telnet to check the status of Memcached and view statistics such as the number of items in the cache and the cache hit rate.

For example, to check the status of Memcached, you can run the following command −

sudo memcached-tool 127.0.0.1:11211 stats

This will display statistics such as the number of connections, the cache hit rate, and the memory usage of Memcached.

Use Memcached with a Content Delivery Network (CDN)

If you have a website or application that serves a global audience, you can use a content delivery network (CDN) to improve the performance and reliability of your site. A CDN caches your content on servers located around the world, reducing the latency and load on your web servers.

You can use Memcached with a CDN by configuring your CDN to cache the data in Memcached. This will allow your CDN to quickly retrieve the cached data from Memcached, reducing the load on your web servers.

Use Memcached for Database Caching

In addition to caching data for your web application, you can also use Memcached for caching database queries. By caching frequently used queries in Memcached, you can reduce the load on your database server and improve the performance of your application.

To do this, you need to modify your application code to check if the data is already cached in Memcached before making a database query. If the data is already cached, your application can retrieve it from Memcached instead of making a new query.

Use Memcached with a TTL Strategy

When using Memcached, it's important to have a strategy for setting the Time-To-Live (TTL) for cached data. The TTL specifies how long the data should remain in the cache before it is evicted.

If you set the TTL too low, the data may be evicted from the cache before it can be reused, which can cause your application to slow down. If you set the TTL too high, the data may become stale and out of date, which can also cause problems.

A good strategy for setting the TTL is to base it on the frequency with which the data is accessed. For frequently accessed data, you can set a shorter TTL to ensure that the data is always fresh. For less frequently accessed data, you can set a longer TTL to reduce the load on the cache.

Conclusion

In this article, we have guided you through the process of installing and configuring Memcached on CentOS 8. We have also shown you how to install the Memcached extension for PHP and how to configure it to connect to Memcached. By using Memcached, you can significantly improve the performance of your website or application by reducing the number of database queries required.

Updated on: 28-Apr-2023

815 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements