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 commands

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"
Parameter Description
PORT Specifies the port on which Memcached listens (default: 11211)
USER Specifies the user that Memcached runs as
MAXCONN Maximum number of simultaneous connections Memcached can handle
CACHESIZE Amount of memory (MB) that Memcached can use for caching data
OPTIONS Additional options for Memcached (here set to listen on localhost only)

Save the changes and restart the service for the configuration to take effect

sudo systemctl restart memcached

Step 4: Test Memcached Connection

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

Once connected, test storing and retrieving data

set mykey 0 60 5
hello
get mykey
quit

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

STORED
VALUE mykey 0 5
hello
END

This confirms that Memcached is able to store and retrieve cached data successfully.

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.

sudo dnf install php-memcached

After installation, restart your web server to load the extension

sudo systemctl restart httpd

Step 6: Test the Memcached Extension for PHP

Create a PHP test file to verify the Memcached extension is working correctly

<?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 port 11211, sets a cached value with a 60-second TTL, and retrieves it.

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

hello

Monitoring Memcached Performance

To ensure that Memcached is running efficiently, you should monitor it regularly. You can use the memcached-tool to check statistics

sudo memcached-tool 127.0.0.1:11211 stats

This displays important metrics such as cache hit rate, memory usage, and the number of active connections.

Best Practices

TTL Strategy

When using Memcached, implement a proper Time-To-Live (TTL) strategy. Set shorter TTL for frequently changing data and longer TTL for relatively static content to optimize cache efficiency.

Database Query Caching

Use Memcached to cache expensive database queries. Check if data exists in cache before executing database queries to reduce server load and improve response times.

Security Considerations

By default, Memcached listens only on localhost (127.0.0.1). If you need to access it from other servers, configure firewall rules and consider using authentication mechanisms.

Conclusion

Memcached is a powerful caching solution that can significantly improve application performance by storing frequently accessed data in memory. Proper installation, configuration, and monitoring ensure optimal performance and reliability for your web applications.

Updated on: 2026-03-17T09:01:38+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements