Introduction
Memcached is a high-performance, distributed memory object caching system. This guide will walk you through the installation and advanced configuration of Memcached on Debian and RedHat-based systems.
Installation
On Debian
# Update the package database
sudo apt update
# Install Memcached
sudo apt install memcached libmemcached-tools
# Start Memcached
sudo systemctl start memcached
# Enable Memcached to start on boot
sudo systemctl enable memcached
# Check Memcached status
sudo systemctl status memcached
On RedHat
# Enable the EPEL repository
sudo yum install epel-release
# Install Memcached
sudo yum install memcached
# Start Memcached
sudo systemctl start memcached
# Enable Memcached to start on boot
sudo systemctl enable memcached
# Check Memcached status
sudo systemctl status memcached
Basic Configuration
Memcached configuration file is typically located at /etc/memcached.conf
. Here are some basic configuration options:
# Set the IP address that Memcached will listen on
-l 127.0.0.1
# Set the port that Memcached will listen on
-p 11211
# Set the maximum memory usage
-m 64
# Set the maximum number of simultaneous connections
-c 1024
Advanced Configuration
Logging
Enable detailed logging for Memcached for monitoring and troubleshooting.
# Enable verbose logging
-vv
# Specify the log file (using a log management tool like syslog is recommended)
# Example for systemd:
# Edit the file /etc/systemd/system/memcached.service.d/override.conf
# Add the following lines:
[Service]
ExecStart=
ExecStart=/usr/bin/memcached -u memcache -m 64 -p 11211 -c 1024 -vv
Security
Securing Memcached involves setting proper access controls and limiting access to trusted networks.
# Listen on a specific IP address (local only or trusted network)
-l 127.0.0.1
# Configure firewall to restrict access to Memcached port (11211)
# Example using iptables:
sudo iptables -A INPUT -p tcp --dport 11211 -s 127.0.0.1 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 11211 -j DROP
# Example using firewalld (RedHat/CentOS):
sudo firewall-cmd --permanent --add-port=11211/tcp --zone=trusted
sudo firewall-cmd --reload
Performance Tuning
Optimize Memcached performance by configuring various parameters.
# Adjust the size of each slab page (default is 1MB)
-I 2m
# Enable large pages support (if supported by the system)
-L
# Adjust the size of the memory chunk allocated for each slab class
--chunk-size 64
# Enable CPU affinity to bind Memcached process to specific CPU cores
-a 1
Monitoring and Statistics
Monitor Memcached performance and usage statistics.
# Enable detailed stats output
-vv
# Use the 'stats' command to get detailed statistics
echo "stats" | nc localhost 11211
# Use the 'stat' command to get stats for specific settings
echo "stats settings" | nc localhost 11211
Conclusion
By following this guide, you should have a well-configured Memcached instance on either Debian or RedHat. Proper configuration and regular maintenance are crucial for leveraging the full potential of Memcached.