Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Deploying and Tuning Memcached: Setup and Runtime Options

Tech May 15 1

Installing Memcached Across Platforms

On Debian/Ubuntu
Update your package index and install the server with client utilities.

sudo apt update
sudo apt install memcached libmemcached-tools

To enable and immediately launch the service:

sudo systemctl enable --now memcached

On RHEL/CentOS
Refresh your repository metadata and install the package.

sudo yum update
sudo yum install memcached

After the installation, start it with systemd:

sudo systemctl enable --now memcached

On macOS via Homebrew
Ensure Homebrew is on the system, then pull in the formula.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew update && brew install memcached

Launch the daemon in the background:

memcached -d

On Windows
Obtain a prebuilt binary from memcached-win64 and extract it. After adding the folder to your PATH, register and start the service:

memcached.exe -d install
memcached.exe -d start

Runtime Parameters and Configuration Files

Memcached behavior is adjusted through command-line flags or a configuration file. Essential flags include:

  • -m : allocated cache memory in megabytes
  • -p : TCP listening port (default 11211)
  • -c : maximum simultaneous connections (default 1024)
  • -u : system user to drop privileges to

Command-line invocation

memcached -d -m 1024 -p 11211 -u memcache -c 2048

File‑based configuration

Create a file such as /etc/memcached.conf:

# Memory ceiling (MB)
-m 1024
# Network port
-p 11211
# Connection cap
-c 2048
# Runtime user
-u memcache

Point the daemon at the file:

memcached -d -f /etc/memcached.conf

Setting Up a Persistent System Service

For automatic startup, define a systemd unit.

Unit file — /etc/systemd/system/memcached.service

[Unit]
Description=Memcached cache daemon
After=network.target

[Service]
User=memcache
ExecStart=/usr/bin/memcached -m 512 -p 11211 -u memcache -c 1024
ExecStop=/bin/kill -s QUIT $MAINPID
Restart=on-failure

[Install]
WantedBy=multi-user.target

Reload the manager and activate the unit:

sudo systemctl daemon-reload
sudo systemctl enable --now memcached

Troubleshooting Common Issues

Service fails to start
Inspect the logs (often /var/log/memcached.log or journalctl -u memcached). Verify port availability with ss -tlnp | grep 11211.

Memory exhaustion
Increase the -m value and ensure the OS has sufficient free RAM. Restart the daemon to apply the change.

Connection overload
Raise the -c limit. Monitor peak connections and adjust accordingly.

memcached -c 4096

Permission issues
Confirm the runtime user owns the working directory and log paths.

sudo chown -R memcache:memcache /var/log/memcached

Network restrictions
Open port 11211 in the local firewall.

sudo ufw allow 11211/tcp   # Ubuntu
# Or firewalld
sudo firewall-cmd --add-port=11211/tcp --permanent
sudo firewall-cmd --reload

Practical Configuration Recipes

Development single‑instance

-m 256
-p 11211
-c 1024
-u nobody

Production‑grade setup
Bind to localhost, anable verbose logging, and fine‑tune slab allocation.

-m 4096
-p 11211
-c 4096
-u memcache
-l 127.0.0.1
-vv
-logfile /var/log/memcached.log
-I 2m
-t 4

Multi‑node environment with HAProxy
Two backend instances with a round‑robin frontend.

Node 10.0.0.1

-m 1024
-p 11211
-c 2048
-u memcache
-l 10.0.0.1

Node 10.0.0.2

-m 1024
-p 11211
-c 2048
-u memcache
-l 10.0.0.2

HAProxy backend stanza

backend memcache_cluster
  balance roundrobin
  server cache1 10.0.0.1:11211 check
  server cache2 10.0.0.2:11211 check
Tags: memcached

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.