Memcached

Memcached

Memcached is a high-performance, open-source in-memory caching system designed to speed up dynamic web applications by reducing database load. It is a free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering. Memcached is simple yet powerful. Its simple design promotes quick deployment, ease of development, and solves many problems facing large data caches. Its API is available for most popular languages.

In computing, Memcached is a general-purpose distributed memory caching system. It is often used to speed up dynamic database-driven websites by caching data and objects in RAM to reduce the number of times an external data source (such as a database or API) must be read.

Memcached runs on Unix, Linux, Windows and Mac OS X and is distributed under a permissive free software license.

Memcached's APIs provide a giant hash table distributed across multiple machines. When the table is full, subsequent inserts cause older data to be purged in least recently used (LRU) order. Applications using Memcached typically layer requests and additions into RAM before falling back on a slower backing store, such as a database.

Why use Memcached?

You use Memcached to:

• Reduce database load
• Improve response times
• Handle high-traffic applications
• Cache frequently requested data

It’s especially useful when the same data is requested repeatedly.

When should you use Memcached?

Memcached is a good fit when:

• You need simple, fast caching
• Your data is temporary and disposable
• You want a lightweight solution
• You’re scaling horizontally across multiple servers

Less ideal when:

• You need persistence (Memcached does not save data to disk)
• You need advanced data structures
• You want built-in replication or clustering logic

Key components of Memcached

1. Client

• Your application interacts with Memcached via a client library

2. Server nodes

• Store cached data in RAM
• Can be distributed across multiple machines

3. Key-value storage

• Everything is stored as: key → value

Key features of Memcached

• Extremely fast in-memory caching
• Simple key-value model
• Multi-threaded architecture
• Distributed caching support
• LRU (Least Recently Used) eviction
• Minimal configuration required

Advantages

• Very lightweight and fast
• Easy to set up and use
• Efficient for read-heavy workloads
• Scales horizontally (add more nodes)

Disadvantages

• No persistence (data is lost on restart)
• Supports only simple data types (strings)
• No built-in replication or failover
• Less feature-rich compared to Redis
• Limited querying or processing capabilities

Common use cases

• Caching database query results
• Session storage (simple cases)
• API response caching
• Reducing backend load in high-traffic sites

Alternatives

1. Redis

• More powerful and flexible
• Supports persistence and data structures

2. Varnish Cache

• Focused on HTTP caching (web layer)

3. Ehcache

• Popular in Java ecosystems

Architecture of the Memcached

The system uses a client–server architecture. The servers maintain a key–value associative array; the clients populate this array and query it. Keys are up to 250 bytes long and values can be at most 1 megabyte in size.

Clients use client-side libraries to contact the servers which, by default, expose their service at port 11211. Each client knows all servers; the servers do not communicate with each other. If a client wishes to set or read the value corresponding to a certain key, the client's library first computes a hash of the key to determine the server to use. Then it contacts that server. The server will compute a second hash of the key to determine where to store or read the corresponding value.

The servers keep the values in RAM; if a server runs out of RAM, it discards the oldest values. Therefore, clients must treat Memcached as a transitory cache; they cannot assume that data stored in Memcached is still there when they need it. MemcacheDB, Couchbase Server, Tarantool and other database servers provide persistent storage while maintaining Memcached protocol compatibility.

If all client libraries use the same hashing algorithm to determine servers, then clients can read each other's cached data.

A typical deployment will have several servers and many clients. However, it is possible to use Memcached on a single computer, acting simultaneously as client and server.

Memcached Security

Most deployments of Memcached exist within trusted networks where clients may freely connect to any server. There are cases, however, where Memcached is deployed in untrusted networks or where administrators would like to exercise control over the clients that are connecting. For this purpose Memcached can be compiled with optional SASL authentication support. The SASL support requires the binary protocol.

A presentation at BlackHat USA 2010 revealed that a number of large public websites had left Memcached open to inspection, analysis, retrieval, and modification of data.

Contents related to 'Memcached'

Redis
Redis
Oracle Coherence
Oracle Coherence
EHCache
EHCache
Redis Cache Invalidation Strategies Explained with C# Examples and Best Practices
Redis Cache Invalidation Strategies Explained with C# Examples and Best Practices
LRU Cache Implementation in C# with Examples and Best Practices
LRU Cache Implementation in C# with Examples and Best Practices