Optimize your database performance by using a Memcached server with your PHP and Python applications
If you have ever felt the pain of a high database load causing a slowdown in your web application and wondering “Is there a way to reduce latency caused by DB requests?”, Then the answer to that question is a big yes. Memcached environmentally friendly memory cache daemons are here to solve all your woes! Caching the DB is one of the simplest methods to reduce DB load and speed up dynamic web applications.
Memcached defines itself as a high distributed memory object caching system, generic in nature, but originally intended to speed up dynamic web applications by reducing database load. Developed by Brad Fitzpatrick for his LiveJournal website in 2003.
In this article, we will look at installing and configuring Memcached on Ubuntu 20.04 and looking at language-specific clients.
The system installed with Ubuntu 20.04 with users with administrative rights, namely sudo users.
Memcached is available on the official Ubuntu 20.04 repository, besides Memcached we will also install a CLI tool known as libcached-tools for managing Memcached. Just run the following command to install both
sudo apt install memcached libmemcached-tools
Verification of Installation
After the installation is complete, the Memcached daemon starts in the background by itself. To verify the installation, we can use the command from the libmemcached-tools package to get Memcached server statistics. Good run
memcstat --servers localhost
memcstat --servers 127.0.0.1
The cat command shows statistics running the server. The above command will produce the output shown below.
Various statistics such as active time in seconds, version and PID will be displayed as output. However, if no output is displayed, it is possible Memcached is not running. Therefore, you must run the following command to start the Memcached server.
sudo systemctl start memcached
To run Memcached server at system startup use the following command.
sudo systemctl enable memcached
If your Memcached is installed on the website server itself, you don’t need to change the configuration file because Memcached is already configured to work with localhost.
On the other hand, if you have installed Memcached on a separate system, you need to change the configuration to allow remote server access to the Memcached server.
Setting Up Remote Access for Memcached Servers
Memcached is vulnerable to DDoS (Distributed Denial of Service) attacks. Incorrect firewall rules and open UDP ports will make your server open and vulnerable to DDoS attacks.
To reduce the risk, we can disable the UDP protocol for locked configurations or create a firewall just to allow trusted servers.
Out of the box, Ubuntu is shipped without an open TCP or UDP port. Additionally, the ufw daemon firewall (uncomplicated Firewall) is not activated by default.
We will activate the firewall and set the Memcached configuration so that we can reduce DDoS vulnerabilities.
First, activate ufw by running the following command:
sudo systemctl enable ufw
Then start the UFW service by running the command below:
sudo systemctl start ufw
With Firewall running, we can finally set the firewall rules. First, activate port 22 to allow SSH connections. SSH is required to access the desired server remotely.
sudo ufw allow 22
Second, you need to know the client’s IP address, which is the web application host and the server’s IP address, which is the Memcached server.
For this example, let’s assume the Client IP becomes 192.168.0.4 and Memcached server IP becomes 192.168.0.5 on the local network.
So to allow Remote Access Memcached Server to Client Server, run:
sudo ufw allow from 192.168.0.4 to any port 11211
Replace 192.168.0.4 with the Client’s IP address that you want.
Next, edit the Memcached configuration file located in /etc/memcached.conf by running the nano command.
sudo nano /etc/memcached.conf
The memcached.conf configuration file will open with nano editor, look for line -l 127.0.0.1 in the configuration and replace 127.0.0.1 with your Memcached IP Server or in this example 192.168.0.5.
After replacing ctrl + o to write to the configuration file and press enter, press ctrl + x to exit nano.
Restart the Memcached server and ufw firewall by running the command below.
sudo systemctl restart memcached ufw
Now we are done with installing and configuring the Memcached server on Ubuntu 20.04.
Connect to Memcached Server
To use the Memcached server, you must install a language-specific client. Fortunately, Memcached has support for many popular languages.
So let’s look at how to install php and python clients for Memcached.
PHP is the most popular server-side scripting language and Memcached is mostly used by web developers to improve the performance of web application servers supported by PHP.
To install memcached support on php, run:
sudo apt install php-memcached
Python also has several libraries that can work and interact with Memcached servers such as pymemcached or python-memcached.
You can install memcached for python by running the following pip command:
pip install pymemcache
pip install python-memcached
In conclusion, we have seen the installation, configuration, and some language-specific Memcached clients on Ubuntu 20.04.
To find out more about the ins and outs and the continued use of Memcached, see Memcached Wiki.