How to install and configure memcached on centos 8Memcached is an object caching system used to enhance dynamic web applications by decreasing database load. In this article we’ll cover installing and configuring Memcached on a CentOS 8 server.

Memcached listens for connections on port 11211 by default. Additionally, an optional configuration setting (options) allows for additional settings that you would like Memcached to take into consideration.

What is Memcached?

Memcached is a free high-performance in-memory key-value data store, used to accelerate dynamic web applications by offloading database load. It works by caching results of database queries for faster retrieval; or other objects, like images or HTML. Setup and use are both simple, with support provided from an extensive developer community.

Memcached data can be retrieved using client software by sending a request to one of your network Memcached servers, with each response from each Memcached server providing a list of available values with their keys and which can then be selected from by selecting from among these values, while selecting one and retrieving it from Memcached; should any value have changed or its expiry time has expired automatically replacing its older value with one from another server’s list or querying a database to locate relevant information then store this in Memcached.

Once data has been retrieved from Memcached servers, it will be saved there for future requests from that same client. Memcached does not overwrite itself so all clients will have access to the same value – drastically reducing database queries sent directly to MySQL or PostgreSQL servers.

If your PHP app requires caching mechanisms, installing the Memcache PHP extension on your server is highly recommended to facilitate faster website load times and decrease load time issues. Memcached is an extension that connects PHP applications directly with Memcached so they can access their cache quickly and efficiently.

To set up Memcached, edit the /etc/sysconfig/memcached file. Here you can change several options, such as maximum cache size (in megabytes), whether to enable large memory pages and which protocol it should auto-negotiate with clients, as well as changing its default listening port to any other open port on your system. If using with remote servers, it may also be wise to set up firewall protection to limit access from outside and prevent port conflicts.

Install Memcached in CentOS 8

Memcached is a high-performance distributed memory object caching system used for speeding up dynamic web applications by relieving database load. Written in C and designed to work with PHP, Python, Perl, Ruby and Java clients. This guide will take you through the steps required for installing and configuring Memcached on a CentOS 8 server.

Before we begin the installation process, ensure your server fulfills the following criteria. In addition, login as root and have an understanding of command line interface.

Start the installation by updating the packages repository using dnf:

sudo dnf update -y

Then install Memcached by running this command:

dnf install memcached libmemcached -y

This command will install both Memcached RPM packages as well as any necessary libraries on your server.

Once Memcached has been installed, start memcached and set memcached to start on boot:

systemctl start memcached
systemctl enable memcached

Verify the memcached status with:

systemctl status memcached

Now, open up /etc/sysconfig/memcached :

nano /etc/sysconfig/memcached

Set your preferences for TCP and UDP connections on port 11211 by default; if your application resides remotely you must make necessary changes so as to allow access from its IP address:

     1     PORT="11211"
     2     USER="memcached"
     3     MAXCONN="1024"
     4     CACHESIZE="64"
     5     OPTIONS="-l,::1"

Now you should have a functional Memcached server. To verify this, create a simple app that queries Memcached for data:

telnet 11211
set mykey 0 60 5
get mykey

For instance, using this code would call Memcached with hello value of 60 seconds TTL before retrieving. This should demonstrate that Memcached is working correctly and storing information properly.

If you require more advanced solutions, the libmemcached package provides several command-line tools for managing Memcached. For more information on using these command-line tools please see the Memcached Wiki.

After testing Memcached, consider upgrading your web applications to use it. It will greatly enhance website performance while making it more user-friendly. If you have any feedback about this guide or have any queries or need clarification please leave your thoughts in a comment below – thanks!

Further Memcached Configuration in CentOS 8

Memcached is an open-source distributed memory object caching system which speeds up dynamic websites by storing data in memory. It’s used by several high-profile organizations including YouTube, Facebook, Twitter, Reddit, Drupal and Zynga and it provides free high performance systems with several libraries to accommodate various programming languages such as PHP, Perl, Ruby, and Python.

Start with a cloud server running a PHP-based app, as well as the php-pecl-memcache or pymemcache library installed to allow applications to communicate with Memcached. Next, disable SELinux so Memcached can run, and open the Memcached port 11211 so your applications can connect.

To configure memcached, edit the file /etc/sysconfig/memcached as follows:

# vi /etc/sysconfig/memcached

Change the values as shown below and add the IP address of your server on which memcached is installed.

# max connection 2048
# set ram size to 2048 - 2GiB
# disable UDP and listen to loopback ip, for network connection use real ip e.g.,
OPTIONS="-U 0 -l"

How to configure your logs to /var/log/memcached.

To configure logs

In order to capture memcached logs, configure /etc/sysconfig/memcached as follows:

OPTIONS="-vv >> /var/log/memcached 2>&1"

Start memcached

Start or Stop memcached

After the above configurations, it is time to start the memcached service as below:

# chkconfig memcached on
# service memcached start

In order to stop/restart memcached, run the following commands:

# service memcached stop
# service memcached restart
Root Cause
  • High resource utilization can happen due to low configurations settings for maximum connections and cachesize.
    Increase the value of “maxconn” and “cachesize”
# cat /etc/sysconfig/memcached*
# max connection 2048
# set ram size to 4096 - 4GiB

Enable Memcached for Applications

Now, we will discuss how to enable memcached for applications that should have access to memcached caching.

Memcached is easy to set up and can work seamlessly with many web servers like Apache and Nginx, thanks to not writing data to disk. Furthermore, its fast performance enables its use with PHP, Python and Ruby programming languages – saving both money and performance overhead in applications. Once configured you’ll experience significant performance gains while simultaneously improving website overall performance! Plus it’s totally open-source!

Start by logging into your server with SSH and accessing /etc/sysconfig/memcached with your preferred text editor. This is where the default settings for Memcached can be found:

nano /etc/sysconfig/memcached

Here, you can make necessary modifications by editing this file as follows:

# max connection 2048
# set ram size to 4096 - 4GiB

Memcached offers four main settings. By default, Memcached listens on port 11211 for UDP connections; you may alter this value to suit any other port you need. MAXCONN sets the maximum simultaneous connections that Memcached can handle; though you are free to change this number to any value of your choosing, more connections may increase its load and potentially slow down websites or applications using Memcached. CACHESIZE determines how much memory Memcached uses when caching data; for each query query in your application you can specify an amount of memory to cache as desired but most applications usually leave it as default value unless specified otherwise.

Once you have altered the settings in the config file, save and exit Nano text editor. Next step should be setting Memcached’s permissions so it can be accessed from PHP or Python applications; to do this add these lines to /etc/sysconfig/memcached file:

Finally, to keep Memcached up-to-date you must set its TTL. This can be accomplished by editing your application code so that it checks Memcached before running database queries.

Memcached can also be combined with a content delivery network (CDN) to increase performance and availability of your website or application. CDNs store your content across servers around the globe, alleviating server load while simultaneously increasing site performance.

Avatar of editorial staff

Editorial Staff

Rad Web Hosting is a leading provider of web hosting, Cloud VPS, and Dedicated Servers in Dallas, TX and Phoenix, AZ.