docker.recipes

Memcached

beginner

High-performance distributed memory caching system.

Overview

Memcached is a high-performance, distributed memory object caching system originally developed by LiveJournal in 2003 to reduce database load and improve application response times. As an open-source, in-memory key-value store, memcached operates as a simple caching layer that stores frequently accessed data in RAM, eliminating the need for repeated database queries or expensive computations. Its minimalist design focuses purely on speed and simplicity, making it one of the most widely adopted caching solutions for web applications. This Docker configuration deploys memcached using the Alpine Linux base image for minimal resource footprint while maintaining full functionality. The setup allocates 256MB of memory by default and exposes the standard memcached port 11211 for both text and binary protocol communications. The containerized approach ensures consistent memcached behavior across different environments while simplifying deployment and scaling operations. Developers building web applications, mobile backends, or API services will find this memcached deployment particularly valuable for reducing latency and database load. Startups experiencing rapid growth can implement this caching layer to handle increased traffic without immediately scaling their database infrastructure, while established companies can use it to optimize existing applications and reduce infrastructure costs through improved resource utilization.

Key Features

  • Memory-only storage with configurable allocation using -m flag parameter
  • Dual protocol support for both text-based telnet commands and binary protocol clients
  • LRU (Least Recently Used) eviction policy for automatic memory management when cache is full
  • Multi-threaded architecture with configurable connection limits and thread pools
  • Distributed caching support through consistent hashing across multiple memcached instances
  • Sub-millisecond response times for cache hits with minimal CPU overhead
  • Built-in statistics and monitoring through stats commands for performance analysis
  • Alpine Linux base image reducing container size to under 10MB for efficient deployment

Common Use Cases

  • 1Session storage for web applications to enable horizontal scaling across multiple server instances
  • 2Database query result caching to reduce load on MySQL, PostgreSQL, or MongoDB databases
  • 3API response caching for frequently requested data in REST or GraphQL services
  • 4Page fragment caching for dynamic websites to store rendered HTML components
  • 5User profile and preference caching for social media platforms and user-driven applications
  • 6Shopping cart data storage for e-commerce platforms requiring fast access to temporary data
  • 7Rate limiting and throttling data storage for API gateways and security applications

Prerequisites

  • Docker Engine 20.10+ and Docker Compose V2 for container orchestration
  • Minimum 512MB available RAM on host system (256MB allocated to memcached by default)
  • Port 11211 available and not conflicting with existing services
  • Basic understanding of key-value caching concepts and TTL (Time To Live) principles
  • Network connectivity planning for client applications to reach memcached instance
  • Monitoring tools or scripts to track cache hit ratios and memory utilization

For development & testing. Review security settings, change default credentials, and test thoroughly before production use. See Terms

docker-compose.yml

docker-compose.yml
1services:
2 memcached:
3 image: memcached:alpine
4 container_name: memcached
5 restart: unless-stopped
6 command: memcached -m 256
7 ports:
8 - "11211:11211"
9 networks:
10 - memcached-network
11
12networks:
13 memcached-network:
14 driver: bridge

.env Template

.env
1# Memcached default configuration

Usage Notes

  1. 1Docs: https://memcached.org/about
  2. 2Default port 11211 - text and binary protocols
  3. 3Simple key-value caching - no persistence by design
  4. 4Test: echo 'stats' | nc localhost 11211
  5. 5256MB memory by default - adjust with -m flag
  6. 6Ideal for session storage, query caching, page caching

Quick Start

terminal
1# 1. Create the compose file
2cat > docker-compose.yml << 'EOF'
3services:
4 memcached:
5 image: memcached:alpine
6 container_name: memcached
7 restart: unless-stopped
8 command: memcached -m 256
9 ports:
10 - "11211:11211"
11 networks:
12 - memcached-network
13
14networks:
15 memcached-network:
16 driver: bridge
17EOF
18
19# 2. Create the .env file
20cat > .env << 'EOF'
21# Memcached default configuration
22EOF
23
24# 3. Start the services
25docker compose up -d
26
27# 4. View logs
28docker compose logs -f

One-Liner

Run this command to download and set up the recipe in one step:

terminal
1curl -fsSL https://docker.recipes/api/recipes/memcached/run | bash

Troubleshooting

  • Connection refused on port 11211: Verify container is running with 'docker ps' and check port mapping configuration
  • Out of memory errors or frequent evictions: Increase memory allocation by modifying '-m 256' to higher value like '-m 512' or '-m 1024'
  • Cache miss ratio too high: Check TTL settings in client applications and verify keys are being set correctly
  • Stats command returns no data: Use 'echo stats | nc localhost 11211' to verify memcached is responding properly
  • Container exits immediately: Check Docker logs for startup errors and verify command syntax in docker-compose configuration
  • Client connection timeouts: Adjust memcached connection limits with '-c' parameter or check network connectivity between services

Community Notes

Loading...
Loading notes...

Download Recipe Kit

Get all files in a ready-to-deploy package

Includes docker-compose.yml, .env template, README, and license

Components

memcached

Tags

#memcached#cache#in-memory#distributed

Category

Database Stacks
Ad Space