What is Memcached? The Simple Cache That Actually Works

Memcached doesn't try to be clever. You stick data in RAM, you get it back fast. Brad Fitzpatrick wrote it in 2003 because LiveJournal's database was getting hammered by the same queries over and over. Instead of making the database cry, cache the results in memory. Revolutionary? No. Effective? Absolutely.

Memcached Architecture Diagram

The "Shit, Our Database is Slow" Problem

Here's the thing - MySQL starts choking when you hit it hard enough with complex queries and high load. PostgreSQL handles more but still hits walls. Your database is designed for consistency and durability, not raw speed. So when your homepage makes 20 database calls and takes 2 seconds to load, that's not a database problem - that's a caching problem.

Had this exact problem at a startup - some Reddit post about our product hit the front page and our single MySQL instance just gave up. Couldn't remember the exact user count, but our monitoring showed database response times going from 50ms to 8 seconds. Threw Memcached in front of the product queries and bought ourselves time to fix the real problem. Setup took 45 minutes because I kept fucking up the cache key naming.

The Architecture is Stupidly Simple

Memcached servers don't talk to each other. At all. No clustering, no replication, no coordination. Your client library handles everything using consistent hashing. You hash your key, pick a server, done.

## This is literally all the complexity there is
telnet localhost 11211
set mykey 0 3600 5
hello
STORED
get mykey
VALUE mykey 0 5
hello
END

When a server dies, the client just redirects those keys to other servers. You lose that cached data, but cache is supposed to be disposable. If losing cache breaks your app, you're doing it wrong. Learned this when our primary Memcached node crashed at 2am and logged out 80% of our users because some genius was storing sessions without fallback to the database.

Memory Management (LRU or GTFO)

Memcached uses slab allocation which sounds fancy but isn't. It pre-allocates memory chunks of different sizes (64 bytes, 128 bytes, 256 bytes, etc.). When you store data, it goes in the smallest chunk that fits. This prevents memory fragmentation that would otherwise fuck you over after running for weeks.

When memory fills up, LRU eviction kicks in. Least recently used data gets booted. No configuration needed - it just works. Want to see what's getting evicted? Check stats evictions and cry if it's high.

Performance Reality Check

The benchmarks claim 200K ops/sec, and they're not lying - on perfect hardware with perfect network conditions. In the real world:

  • Your cloud VM with shared networking: 50K ops/sec if you're lucky
  • Decent dedicated hardware with 1Gb network: 100K-150K ops/sec
  • High-end server with 10Gb network: Yeah, you might hit 200K+ ops/sec

Sub-millisecond latency is real though. I've measured 0.1-0.5ms response times consistently on production systems. Compare that to database queries that take 5-50ms and you understand why caching works.

The multi-threaded model means you can use multiple CPU cores, unlike Redis which is single-threaded for data operations. More cores = more throughput, up to network limits.

Memcached Architecture

The Protocol (So Simple You Can Debug It With Telnet)

The text protocol is human-readable, which is amazing for debugging. When something's broken at 3am, you can literally telnet to port 11211 and see what's happening:

## Debug like a human
echo \"stats\" | nc localhost 11211
## See your hit rate, memory usage, connections, everything

Client libraries exist for every language that matters: Python, PHP, Java, Node.js, Ruby, Go, .NET, Rust. They all work the same way because the protocol is simple.

Installation and Configuration (The Easy Part That Goes Wrong)

What You Actually Get

Memcached does exactly 6 things and nothing more:

  • GET: Retrieve a value by key
  • SET: Store a key-value pair with optional TTL
  • DELETE: Remove a key
  • ADD: Set only if key doesn't exist (useful for preventing race conditions)
  • REPLACE: Set only if key exists
  • STATS: Show what the hell is happening

That's it. No JSON parsing, no data structures, no complex queries. Keys are strings up to 250 characters, values are binary blobs up to 1MB. Latest is 1.6.39 from July. It works.

Memcached Usage Pattern

Installation is Quick Unless Windows Hates You

Ubuntu/Debian (the easy path):

sudo apt update && sudo apt install memcached libmemcached-tools
## Starts automatically on port 11211 with 64MB RAM
sudo systemctl status memcached

CentOS/RHEL/Rocky (slightly more painful):

sudo dnf install memcached libmemcached
sudo systemctl enable --now memcached

macOS (Homebrew saves your sanity):

brew install memcached
## Won't start automatically - run manually:
memcached -d -m 512 -p 11211

Docker (for the sane):

docker run -d --name memcached -p 11211:11211 memcached:1.6.39
## This is how I test everything locally

Windows (abandon hope):
The Windows installer failed 3 times on my machine before I gave up. You can technically build from source, but fuck that noise - just use Docker Desktop. At least when Docker breaks, you can restart it and pretend everything's fine.

Docker Memcached Setup

Configuration Reality Check

The default settings work for exactly zero production systems:

## Default configuration (useless for anything real)
memcached -d -m 64 -p 11211 -u memcached

## Production configuration (what you actually need)
memcached -d -m 2048 -p 11211 -u memcached -c 4096 -t 8 -I 5m

Memory (-m): The default 64MB is an insult. Either give it real memory or just use application-level caching. Amazon recommends 10-20% of your total RAM. I usually allocate whatever I can spare - cache misses hurt more than memory costs.

Max Item Size (-I): The 1MB limit will bite you when that 'small' JSON response becomes 1.1MB after you add user preferences. Silent failure city, party of you. That "small" JSON response that becomes 1.2MB after compression? Silent failure city. Bump to 5MB or 10MB and save yourself debugging hell.

Connections (-c): Default 1024 connections sounds like a lot until Black Friday. I've seen e-commerce sites hit 2000+ connections during traffic spikes. Set to 4096 and monitor.

Threads (-t): One thread per CPU core, up to 8. More than 8 threads usually hurt performance due to lock contention.

The Security Problem Nobody Talks About

Memcached has no authentication. None. Zip. Anyone who can connect to port 11211 can read/write/flush your entire cache. The security model assumes you're running inside a trusted network.

Found out the hard way when some junior dev exposed our Memcached to the internet and we got scraped by bots for 3 days before monitoring caught it. Security made me write a 5-page incident report. Shodan shows thousands of exposed instances right now. Don't be one of them.

Firewall Rules (mandatory):

## Ubuntu UFW
sudo ufw allow from 10.0.0.0/8 to any port 11211
sudo ufw allow from 172.16.0.0/12 to any port 11211  
sudo ufw allow from 192.168.0.0/16 to any port 11211

## RHEL/CentOS firewalld
firewall-cmd --permanent --add-rich-rule=\"rule family='ipv4' source address='10.0.0.0/8' port protocol='tcp' port='11211' accept\"

Production Configuration Disasters I've Seen

Disaster #1: Running out of memory with no evictions
Memcached doesn't crash when it runs out of memory - it just stops caching new data. Your hit rate drops to 0% and nobody knows why. Monitor evictions in stats.

Disaster #2: The 1MB limit strikes
Spent an entire afternoon debugging why cache hit rate dropped to shit. No error message, just silent failures. Turns out our "small" JSON responses grew to 1.2MB after we added user preferences. Memcached just discards anything over 1MB without telling you. Set -I 5m and move on with your life.

Disaster #3: Binding to 0.0.0.0 on production
Default configuration binds to all interfaces. Your cache becomes the internet's cache. Use -l 127.0.0.1 for local-only or -l 10.0.1.100 for specific internal IPs.

Disaster #4: Not monitoring connections
Hit the 1024 connection limit during a traffic spike. New connections get dropped, cache hit rate plummets. Set -c 4096 and monitor curr_connections vs max_connections.

Monitoring (Because You'll Need It)

## Essential stats to watch
echo \"stats\" | nc localhost 11211 | grep -E \"(hit_rate|evictions|curr_connections|bytes_written)\"

## Key metrics that matter:
## - get_hits / (get_hits + get_misses) > 0.95 (95% hit rate minimum)
## - evictions should be near 0
## - curr_connections shouldn't approach max_connections

Set up proper monitoring or you'll be debugging blind when things go wrong. Nagios plugins, Prometheus exporters, Datadog integrations - pick one and use it.

Memcached Distributed Architecture

Memcached Monitoring Dashboard

Real-World Usage (Where Memcached Actually Saves Your Ass)

The Big Players Who Depend On It

Netflix caches everything

  • user preferences, video metadata, recommendation algorithms.

When 50 million people want to binge-watch at 8pm, their EVCache infrastructure handles millions of requests per second.

Without caching, their MySQL clusters would melt.

Facebook built their empire on Memcached. They literally wrote the book on scaling Memcached to billions of users.

Their approach: thousands of Memcached servers with custom client libraries that handle failover, connection pooling, and geographical distribution.

Wikipedia serves 500+ million page views daily using Memcached clusters that cache rendered articles, user data, and multilingual content.

Their cache hit rate stays above 98%, which is the only reason Wikipedia loads fast despite running on donations.

Facebook Memcached Scale

Shit That Actually Works in Production

Database Query Caching (the obvious one):

## This pattern saves every database-backed website
def get_user_profile(user_id):
    cache_key = f"user_profile:{user_id}"
    profile = memcached.get(cache_key)
    if profile is None:
        profile = db.query("SELECT * FROM users WHERE id = %s", user_id)
        memcached.set(cache_key, profile, ttl=3600)  # 1 hour
    return profile

Session Storage (for when you need to scale horizontally):

Instead of sticky sessions that limit scaling, store session data in Memcached. Django, Rails, Express.js all have built-in Memcached session handlers.

API Response Caching (because external APIs are slow and expensive):

Cache expensive API calls for minutes or hours. Saved one client about $2k/month on their insane Google Maps API bill by caching geocoding responses for 6 hours instead of hitting the API every time someone searched for "pizza near me".

Fragment Caching (for complex pages):
Cache rendered HTML fragments instead of full pages.

E-commerce sites cache product description HTML, pricing widgets, and recommendation blocks separately.

Cloud Provider Reality Check

AWS ElastiCache for Memcached supports [versions up to 1.6.22](https://docs.aws.amazon.com/Amazon

ElastiCache/latest/mem-ug/engine-versions.html) but defaults to older versions because Amazon moves slow.

The managed service handles patches, backups, and monitoring, but you pay 3x what self-hosted costs. Worth it if your time is valuable. AWS ElastiCache defaults are garbage for anything real

  • you'll need to tune the configuration.

Google Cloud Memorystore supports versions 1.5.16 and 1.6.15 with better VPC integration than AWS.

Their pricing is more reasonable, but availability is limited to certain regions.

Azure Cache for Redis technically supports Memcached protocol compatibility but it's really Redis underneath.

Works fine if you need both Redis and Memcached features.

Performance Numbers That Matter

Real production deployments I've seen:

  • E-commerce site:

About 150K ops/sec during Black Friday on decent hardware

  • News website: Hit around 200K ops/sec with good hit rates
  • SaaS platform:

Maybe 80K ops/sec for session storage

  • Gaming backend: Pushed 300K+ ops/sec for leaderboards on expensive boxes

The [official benchmarks](https://github.com/Redis

Labs/memtier_benchmark) showing 1M+ ops/sec are achievable on dedicated hardware with 10GbE networking, but most cloud instances max out around 100-200K ops/sec due to network limitations.

Common Deployment Patterns That Work

**Pattern 1:

Database Query Cache**

App Servers → Memcached Cluster (3-5 nodes) → MySQL/PostgreSQL
Cache TTL: 5-60 minutes
Hit Rate: 85-95%

**Pattern 2:

Session Storage**

Load Balancer → App Servers → Memcached Cluster → Database
Cache TTL: 30 minutes (session timeout)
Hit Rate: 99%+ (sessions are read-heavy)

**Pattern 3:

Multi-Layer Cache**

CDN → Nginx → App Server → Memcached → Database
Page Cache: 5 minutes
Fragment Cache: 30 minutes  
Data Cache: 2 hours

Ecosystem and Tooling That Doesn't Suck

Client Libraries Worth Using:

  • Python: pymemcache (Pinterest maintains it, actually works)
  • Java: spymemcached (battle-tested at scale)
  • PHP: php-memcached (official extension, not the shitty user-space one)
  • Node.js: memcached (connection pooling, automatic failover)
  • Go: gomemcache (Brad Fitzpatrick wrote this too)

Monitoring Tools:

Actually useful dashboards and alerting

For self-hosted monitoring

Real-time key access monitoring

When Memcached is Overkill

Don't use Memcached if:

  • Your entire dataset fits in application memory (just use a HashMap)
  • You have < 1000 requests/day (database caching is premature optimization)
  • You need persistence (cache data disappears on restart
  • that's a feature)
  • You want complex data structures (use Redis instead)

The 3AM Test:

If losing all your cached data would break your application, you're not caching

  • you're using Memcached as a database, which is wrong. Saw a startup cache their entire user table for 24 hours. Worked great until someone updated their email and couldn't log in for a day because the cache wouldn't expire. Fixed: TTL set to 5 minutes, not 24 hours.

Session Storage with Memcached

Frequently Asked Questions (The Shit You Actually Need to Know)

Q

What version should I run?

A

Version 1.6.39 is current as of this writing. Unless you're running CentOS 6 or something equally ancient, just install whatever your package manager gives you. Fair warning: Memcached 1.6.17 has a weird stats bug on CentOS 7 where stats sometimes returns empty. Fixed in 1.6.22+, or just restart the service when your monitoring alerts fire.

Q

How much RAM should I give it?

A

Start with whatever you can spare. If you have 16GB on your server, give Memcached 4-8GB. Too little memory = high eviction rates = shitty cache performance. Too much memory = wasted resources. Monitor your hit rate

  • if it's below 95%, you probably need more memory or better cache key design.
Q

Does Memcached save data to disk?

A

No, and that's the point. When the server restarts, your cache is gone. If this scares you, you're thinking about caching wrong. Cache is disposable. If losing cached data breaks your app, fix your app design, don't try to make Memcached persistent.

Q

What's this 1MB value limit bullshit?

A

Keys: 250 characters max.

Values: 1MB max by default, configurable with -I. The 1MB limit will bite you eventually

  • some "small" JSON response grows to 1.1MB and silently fails to cache. Bump the limit to 5MB with -I 5m and save yourself hours of debugging.
Q

What happens when a server dies?

A

Your cache data on that server disappears. Client libraries failover to other servers automatically. Your hit rate drops temporarily until the cache warms up again. This is normal and expected. If you can't handle cache misses gracefully, you're doing caching wrong.

Q

Is Memcached secure?

A

No authentication whatsoever. Anyone who can reach port 11211 can read, write, or flush your entire cache. Use firewalls or you'll end up on Shodan for script kiddies to abuse. Don't run it on the public internet unless you enjoy pain.

Q

How many connections can it handle?

A

Default is 1024, which is fine until it isn't. Black Friday traffic? Marketing campaign goes viral? You'll hit that limit and new connections get dropped. Set -c 4096 and monitor curr_connections vs max_connections in stats.

Q

Should I use Memcached or Redis?

A

Memcached if you want simple, fast caching with zero complexity. Redis if you need fancy data structures, persistence, pub/sub, or Lua scripts. Redis is more popular because it does everything. Memcached does one thing really well. Pick based on what you actually need, not what's trendy.

Q

Can I run this in Docker?

A

bashdocker run -d --name memcached -p 11211:11211 -m 512m memcached:1.6.39 -m 400This works fine for development and small production deployments. Just don't expect the same performance as bare metal due to Docker networking overhead.

Q

Why is my hit rate terrible?

A

Common reasons:

  • Not enough memory - check evictions in stats
  • TTLs too short - cache expires before it's useful
  • Cache keys not distributed - all data on one server
  • Large values hitting 1MB limit - silent failures
  • No cache warming - cold cache after restart

Run echo "stats" | nc localhost 11211 and look at evictions, get_hits, and get_misses.

Q

How do I monitor this thing?

A
echo "stats" | nc localhost 11211 | grep -E "get_hits|get_misses|evictions|curr_connections|bytes"```

Key metrics: hit rate > 95%, evictions near zero, current connections well below max. Set up [proper monitoring](https://docs.datadoghq.com/integrations/memcached/) or you'll be debugging blind when shit hits the fan.
Q

Should I backup my cache?

A

Fuck no. Cache is not storage. If you're backing up cache data, you're using Memcached as a database, which is fundamentally wrong. Cache gets regenerated from your real data. If losing cache breaks your app, your architecture is broken, not your cache.

Q

My app is slow after Memcached restart

A

This is called a "cold cache" and it's normal. After restart, cache is empty so every request hits the database until cache warms up. Solutions:

  • Pre-warm critical cache keys after restart
  • Implement graceful degradation during cache misses
  • Use multiple Memcached servers so they don't all restart at once
Q

Connection timeouts are killing me

A

Usually means:

  • Network issues between app and Memcached
  • Memcached overloaded - check CPU and memory usage
  • Hit connection limit - increase -c parameter
  • Client library timeout too short - tune connection and read timeouts
  • Firewall dropping connections - check iptables/firewalld rules

Start debugging with telnet localhost 11211 - if that doesn't work, your network is fucked. Connection pooling in most client libraries is garbage by default and needs manual tuning to work properly.

Memcached Telnet Commands

Memcached vs The Competition (Realistic Comparison)

Reality Check

Memcached

Redis

Hazelcast

Apache Ignite

What It Actually Does

Fast key-value cache, nothing else

Swiss Army knife of data structures

Java-focused distributed cache

Enterprise data grid (overkill for most)

Real Performance

50-200K ops/sec on typical cloud VMs

80-150K ops/sec (single-threaded)

20-100K ops/sec (high overhead)

Highly variable, usually slower

Memory Overhead

Minimal (just your data + ~40 bytes per item)

2-3x more due to Redis features

3-5x more due to Java heap overhead

5-10x more, enterprise-grade bloat

Setup Complexity

Quick setup unless you mess up the config

Plan on half an hour, longer if you hit weird issues

Takes most of a day, enterprise complexity

Multiple days, consultant required

When Shit Breaks

telnet to port 11211, debug easily

redis-cli, decent debugging

JMX hell, enterprise tooling required

Good luck, you'll need consultants

Documentation

Simple, to the point

Excellent but overwhelming

Enterprise word salad

Academic papers disguised as docs

Clustering

Client handles it (consistent hashing)

Redis Cluster (complex but works)

Built-in but Java-only

Built-in, overly complex

Data Types

Strings/binary blobs only

Everything (strings, lists, sets, hashes, streams)

Java objects and collections

SQL tables, objects, compute tasks

Max Item Size

1MB default (configurable to ~128MB)

512MB default

Limited by JVM heap

Limited by available RAM

Persistence

None (it's a cache, dammit)

RDB snapshots + AOF logs

Optional, usually not used

ACID transactions (why?)

Language Support

Every language that matters

Every language + excellent clients

Mostly Java, some other JVM langs

Java/.NET primarily

Production Reality

Just works, scales horizontally

Single point of failure until you cluster

Requires Java expertise

Enterprise-only, expensive

Memory Sweet Spot

1-100GB per node

1-50GB per node (single-threaded)

4-64GB per node (JVM overhead)

16GB-1TB per node

Typical Use Cases

Database query cache, session storage

Cache + pub/sub + real-time features

Enterprise Java apps

Large corps with money to burn

Learning Time

1 hour to be productive

1 week to understand all features

1 month to not hate it

3-6 months + certification

Community Vibe

"It just works" practical engineers

Active, lots of tutorials and examples

Enterprise Java consultants

Academic researchers

License Cost

Free (BSD)

Free (BSD)

Free + paid cloud/support

Free + expensive enterprise features

Cloud Options

AWS ElastiCache, Google Memorystore

All major clouds with good pricing

Hazelcast Cloud ($$)

Available but pricey

Monitoring

Basic stats command + external tools

redis-cli + great ecosystem

JMX + enterprise monitoring

Complex but complete

Related Tools & Recommendations

compare
Similar content

Redis vs Memcached vs Hazelcast: Caching Decision Guide

Three caching solutions that tackle fundamentally different problems. Redis 8.2.1 delivers multi-structure data operations with memory complexity. Memcached 1.6

Redis
/compare/redis/memcached/hazelcast/comprehensive-comparison
100%
tool
Similar content

Redis Overview: In-Memory Database, Caching & Getting Started

The world's fastest in-memory database, providing cloud and on-premises solutions for caching, vector search, and NoSQL databases that seamlessly fit into any t

Redis
/tool/redis/overview
61%
integration
Recommended

Redis + Node.js Integration Guide

competes with Redis

Redis
/integration/redis-nodejs/nodejs-integration-guide
56%
tool
Similar content

Redis Cluster Production Issues: Troubleshooting & Survival Guide

When Redis clustering goes sideways at 3AM and your boss is calling. The essential troubleshooting guide for split-brain scenarios, slot migration failures, and

Redis
/tool/redis/clustering-production-issues
55%
compare
Recommended

Python vs JavaScript vs Go vs Rust - Production Reality Check

What Actually Happens When You Ship Code With These Languages

python
/compare/python-javascript-go-rust/production-reality-check
50%
tool
Similar content

ClickHouse Overview: Analytics Database Performance & SQL Guide

When your PostgreSQL queries take forever and you're tired of waiting

ClickHouse
/tool/clickhouse/overview
49%
tool
Similar content

Turborepo Overview: Optimize Monorepo Builds & Caching

Finally, a build system that doesn't rebuild everything when you change one fucking line

Turborepo
/tool/turborepo/overview
48%
tool
Similar content

mongoexport Performance Optimization: Speed Up Large Exports

Real techniques to make mongoexport not suck on large collections

mongoexport
/tool/mongoexport/performance-optimization
48%
tool
Similar content

SQLite Performance Optimization: Fix Slow Databases & Debug Issues

Your database was fast yesterday and slow today. Here's why.

SQLite
/tool/sqlite/performance-optimization
48%
tool
Similar content

Apache Cassandra Performance Optimization Guide: Fix Slow Clusters

Stop Pretending Your 50 Ops/Sec Cluster is "Scalable"

Apache Cassandra
/tool/apache-cassandra/performance-optimization-guide
45%
integration
Similar content

Redis Caching in Django: Boost Performance & Solve Problems

Learn how to integrate Redis caching with Django to drastically improve app performance. This guide covers installation, common pitfalls, and troubleshooting me

Redis
/integration/redis-django/redis-django-cache-integration
43%
tool
Similar content

Neon Production Troubleshooting Guide: Fix Database Errors

When your serverless PostgreSQL breaks at 2AM - fixes that actually work

Neon
/tool/neon/production-troubleshooting
43%
tool
Similar content

PostgreSQL: Why It Excels & Production Troubleshooting Guide

Explore PostgreSQL's advantages over other databases, dive into real-world production horror stories, solutions for common issues, and expert debugging tips.

PostgreSQL
/tool/postgresql/overview
41%
tool
Similar content

Neon Serverless PostgreSQL: An Honest Review & Production Insights

PostgreSQL hosting that costs less when you're not using it

Neon
/tool/neon/overview
41%
tool
Similar content

PocketBase Overview: SQLite Backend for Prototypes & Small Apps

Single-File Backend for Prototypes and Small Apps

PocketBase
/tool/pocketbase/overview
41%
tool
Similar content

Supabase Production Deployment: Best Practices & Scaling Guide

Master Supabase production deployment. Learn best practices for connection pooling, RLS, scaling your app, and a launch day survival guide to prevent crashes an

Supabase
/tool/supabase/production-deployment
41%
tool
Similar content

Nx Monorepo Overview: Caching, Performance & Setup Guide

Monorepo build tool that actually works when your codebase gets too big to manage

Nx
/tool/nx/overview
41%
tool
Similar content

SQLite: Zero Configuration SQL Database Overview & Use Cases

Zero Configuration, Actually Works

SQLite
/tool/sqlite/overview
39%
tool
Similar content

Weaviate: Open-Source Vector Database - Features & Deployment

Explore Weaviate, the open-source vector database for embeddings. Learn about its features, deployment options, and how it differs from traditional databases. G

Weaviate
/tool/weaviate/overview
36%
tool
Similar content

etcd Overview: The Core Database Powering Kubernetes Clusters

etcd stores all the important cluster state. When it breaks, your weekend is fucked.

etcd
/tool/etcd/overview
36%

Recommendations combine user behavior, content similarity, research intelligence, and SEO optimization