Memcached

Master high-performance distributed memory caching for accelerating web applications

30 min read
Not Started

What is Memcached?

Memcached is a high-performance, distributed memory caching system designed to speed up dynamic web applications by alleviating database load. It stores data and objects in RAM to reduce the number of times an external data source (such as a database or API) must be read, providing sub-millisecond response times for cached data.

Originally developed by Brad Fitzpatrick for LiveJournal in 2003, Memcached has become one of the most widely used caching solutions in web development. It follows a simple key-value store model and is designed to be simple, fast, and easily scalable across multiple servers. Major companies like Facebook, Twitter, and YouTube rely on Memcached to serve billions of requests daily.

Memcached Performance Calculator

3072MB
Total Memory
0.1ms
Avg Latency
91%
Hit Ratio
3M
Objects Stored

Bandwidth: 80 Kbps

CPU per Node: 7%

Utilization: 7%

Memcached Core Features

Simple Protocol

Text-based protocol that's easy to implement and debug.

• GET/SET operations
• ADD/REPLACE commands
• DELETE operations
• INCREMENT/DECREMENT
• STATS monitoring

High Performance

Optimized for speed with minimal overhead.

• Sub-millisecond latency
• 50K+ requests/second per node
• Event-driven architecture
• Multi-threaded design
• Zero-copy operations

Distributed Design

Client-side distribution for horizontal scaling.

• Consistent hashing
• No single point of failure
• Linear scalability
• Client-side sharding
• Automatic failover

Memory Management

Efficient memory allocation and LRU eviction.

• Slab allocation
• LRU eviction policy
• Memory pre-allocation
• Configurable item sizes
• Memory usage statistics

Memcached Architecture

Client-Server Model

Simple architecture where clients connect directly to Memcached servers.

Client-Server Architecture
[Application] → [Memcached Client Library] → [Memcached Server Pool]
                                                        ↓
                            [Server1] [Server2] [Server3]

Consistent Hashing

Distributes keys evenly across servers with minimal reshuffling.

• Hash ring distribution
• Minimal key migration
• Server addition/removal
• Load balancing

Slab Allocation

Memory management system that reduces fragmentation.

• Fixed-size chunks
• Different slab classes
• Efficient allocation
• Reduced fragmentation

Memcached Use Cases

Database Query Caching

Cache expensive database query results to reduce load.

  • • Frequently accessed data
  • • Complex query results
  • • Aggregated statistics
  • • User profile data

Session Storage

Store user session data for web applications.

  • • User authentication tokens
  • • Shopping cart contents
  • • User preferences
  • • Temporary form data

API Response Caching

Cache external API responses to improve performance.

  • • Third-party API responses
  • • Rate-limited API calls
  • • Expensive computations
  • • Static content

Content Delivery

Cache rendered content and static assets.

  • • Rendered HTML pages
  • • Image metadata
  • • CSS/JS fragments
  • • Template output

Memcached Commands

Basic Operations

Basic Memcached Commands
# Store data
set key 0 3600 5
hello
STORED

# Retrieve data
get key
VALUE key 0 5
hello
END

# Delete data
delete key
DELETED

Storage Commands

• set - Store data
• add - Store if not exists
• replace - Store if exists
• append - Append to existing
• prepend - Prepend to existing
• cas - Check and set

Retrieval Commands

• get - Retrieve single key
• gets - Get with CAS value
• mget - Multiple key retrieval
• gat - Get and touch (refresh TTL)
• touch - Update expiration time

Numeric Operations

• incr - Increment numeric value
• decr - Decrement numeric value
• Atomic operations
• 64-bit unsigned integers
• Overflow protection

Administrative

• stats - Server statistics
• flush_all - Clear all data
• version - Server version
• quit - Close connection
• slabs - Slab information

Real-World Memcached Implementations

Facebook

Massive Memcached deployment serving billions of requests daily.

  • • 28TB of cached data
  • • Millions of requests per second
  • • Custom optimizations
  • • Multi-tier architecture

Wikipedia

Caches rendered articles and database queries for fast page loads.

  • • Article content caching
  • • User session storage
  • • Database query results
  • • Multi-language support

YouTube

Uses Memcached for video metadata and user data caching.

  • • Video metadata
  • • User preferences
  • • View count data
  • • Recommendation cache

Reddit

Caches post data, comments, and user sessions for fast response times.

  • • Post and comment data
  • • User authentication
  • • Vote counting
  • • Subreddit metadata

Memcached Best Practices

✅ Do

  • • Use consistent key naming conventions
  • • Set appropriate expiration times
  • • Monitor cache hit ratios
  • • Use connection pooling
  • • Implement proper error handling
  • • Consider using binary protocol
  • • Monitor memory usage and stats

❌ Don't

  • • Store critical data without backup
  • • Use Memcached as a persistent store
  • • Ignore cache warming strategies
  • • Store large objects (>1MB)
  • • Forget to handle cache misses
  • • Use predictable key patterns
  • • Neglect security considerations

📝 Memcached Quiz

1 of 6Current: 0/6

What type of storage system is Memcached?