# Memcached

URL: https://softwaredictionary.org/terms/memcached
Category: Databases
Last updated: 2026-10-06
Pronunciation: MEM-kash-DEE

In short: Memcached is an open-source, in-memory key-value cache that keeps small pieces of data in RAM across servers to take load off databases and speed up apps.

## What is Memcached?

Memcached is a simple caching server that keeps data in memory as keys and values. Brad Fitzpatrick wrote it in 2003 to speed up LiveJournal, and it went on to help scale sites such as Facebook and Wikipedia. It does one job well: an application stores a value under a key with `set` and reads it back with `get`, and the answer comes back in well under a millisecond because nothing touches the disk.

The design is deliberately minimal. Values are opaque bytes, usually serialized objects or rendered HTML fragments, with keys of up to 250 characters and values of up to 1 MB by default. Each item can have an expiry time, and when memory fills up, the least recently used items are evicted. Memcached servers don't talk to each other: the client library hashes each key to pick a server, usually with consistent hashing, so adding servers adds capacity and losing one only loses that slice of the cache.

Applications typically use it with the cache-aside pattern: look in the cache first, and on a miss, read the database and store the result with a time to live. It's like a sticky note on your monitor with a number you dial often: much faster than opening the address book, and no harm done if it falls off. Memcached should never be reachable from the internet: in 2018, exposed servers were abused for record-breaking DDoS amplification attacks, and UDP has been turned off by default ever since.

Memcached is most often compared with Redis. Redis is also an in-memory key-value store, but it offers data types such as lists, sets and sorted sets, can save data to disk, replicates to other servers and supports pub/sub messaging. Memcached has none of that and loses its data on restart, but it is multithreaded, simple to run and very efficient for large, plain caches, so it suits teams that only need a fast, disposable cache.

## Key takeaways

- Memcached is an in-memory key-value cache with a very small set of commands.
- Items expire after a set time, and the least recently used ones are evicted when memory is full.
- Clients spread keys across servers; the servers don't share data with each other.
- By default, data lives only in memory and is lost on restart, so it should only hold data that can be rebuilt.
- Redis adds data structures, persistence and replication; Memcached stays simpler and multithreaded.

## Example: The cache-aside pattern with Memcached (Python)

```python
from pymemcache.client.base import Client

cache = Client(("localhost", 11211))   # Memcached's default port

def get_product_name(product_id):
    key = f"product:{product_id}:name"
    cached = cache.get(key)            # 1. try the cache first
    if cached is not None:
        return cached.decode()
    name = db.fetch_product_name(product_id)   # 2. on a miss, ask the database
    cache.set(key, name, expire=300)   # 3. keep it for 5 minutes
    return name
```

## Frequently asked questions

**What is the difference between Memcached and Redis?**

Both keep key-value data in memory. Memcached is a plain, multithreaded cache for strings that loses everything on restart, while Redis adds data structures, optional persistence to disk, replication and messaging features.

**Is Memcached still used?**

Yes. Very large sites such as Facebook still run it at enormous scale, and major cloud providers offer it as a managed service, although many new projects pick Redis or Valkey because they do more.

**Does Memcached save data to disk?**

Not by default. Everything lives in RAM and disappears when the server restarts, so it should only cache data whose real copy is stored somewhere else, such as a database.

## Sources

- [Memcached documentation](https://docs.memcached.org/)
- [Memcached protocol (protocol.txt)](https://github.com/memcached/memcached/blob/master/doc/protocol.txt)
- [Nishtala et al.: Scaling Memcache at Facebook (NSDI 2013)](https://www.usenix.org/conference/nsdi13/technical-sessions/presentation/nishtala)

---

Software Dictionary: https://softwaredictionary.org/ · https://softwaredictionary.org/llms.txt
