Definition
What it is
Memcached is a free, open-source, high-performance distributed memory caching system. It stores small chunks of arbitrary data — strings, objects, HTML fragments, query results — in RAM, keyed by a string, and retrieves them in constant time. It is not a database: it does not persist, it does not guarantee that anything stays cached, and it does not care about durability. Its only job is to answer a key lookup fast, usually in under a millisecond, so the application can skip a slow backend (database, API, filesystem) that would take tens or hundreds of milliseconds.
Memcached was written in 2003 by Brad Fitzpatrick for LiveJournal, one of the first high-traffic social platforms. LiveJournal had a database that could not keep up with the site's read traffic. Fitzpatrick's insight was simple: most reads return the same data repeatedly, so cache the result of the first read and serve it from RAM to every subsequent request. That cache became Memcached, and it is still deployed in production at massive scale at Facebook, Wikipedia, Twitter, and thousands of other sites.
Why it matters
The bottleneck in most web applications is not CPU. It is the round-trip to a slow backend: a SQL query, an HTTP call, a file read. Each round-trip can cost 10-100ms. Memcached reduces that to under 1ms for cached keys. When 90% of reads hit cache, the effective average latency of the backend drops by an order of magnitude, and the load on the database drops by the same amount.
Memcached's design constraint — RAM-only, no persistence, no replication — is what makes it fast. Every feature that would add durability adds latency. The cost of that speed is that the application must always be prepared for a cache miss, and the cache must never be the source of truth for data that matters. When that discipline holds, Memcached is one of the simplest and most effective scaling tools available.
How it works
The mechanism is a classic cache-aside pattern. The application owns the logic. On read: build a deterministic key from the request, ask Memcached for the value, and if it is there, serve it. If it is not there, query the backend, store the result in Memcached with a TTL, and serve it. On write: update the backend, then either delete the affected cache entry (recommended) or update it (harder, races exist). On miss storms: use a lock or a "single flight" pattern so only one request recomputes the value while others wait or serve stale data.
graph TD
R[Read request] --> K[Build key]
K --> C{Memcached?}
C -->|hit| S[Serve cached value]
C -->|miss| D[Query backend]
D --> W[Store in Memcached with TTL]
W --> S
U[Write request] --> B[Update backend]
B --> I[Delete cache entry]
Memcached itself is deliberately simple. It speaks a text and a binary protocol over TCP (or UDP for legacy clients). It has no concept of a schema, no access control, no clustering protocol of its own. Distribution is handled entirely by the client: each client hashes the key with a consistent hashing function and chooses which Memcached server to talk to. That means Memcached scales horizontally by adding servers and updating the client's server list. It is a design choice that has aged very well: Memcached servers are stateless to each other, so a failed server loses its keys and nothing else.
Deploying Memcached correctly
Put it close to the application
Network latency dominates Memcached performance. Put the Memcached server in the same rack or the same availability zone as the application, not across the internet.
Size by working set, not by total data
Memcached should hold the data that is read most often. If your working set is 8GB, a 16GB Memcached server has plenty of headroom. Sizing for the entire dataset wastes RAM.
Use a namespace prefix per environment
Prefix keys with the environment and service ("prod:users:42"). This prevents cross-environment collisions when servers are shared.
Monitor hit rate, evictions, and memory
A hit rate below 80% usually means the working set does not fit or the TTLs are too short. High evictions mean the cache is too small. Both are actionable.
The hit rate is the single most important operational metric. A hit rate above 90% means the cache is doing its job; below 70% means the backend is doing most of the work anyway, and the cache is overhead rather than savings. Evictions indicate the cache is too small or the TTLs are too long. Memory usage close to the limit is normal; growth without bound is not.
Common failures
A second failure is the cache stampede. When a popular key expires, dozens of concurrent requests all see a miss, all query the backend, and all write the result back. The backend is hammered. The fix is a lock (only one request recomputes) or a probabilistic early expiry (a technique popularized by Facebook's "leases" paper). Either way, the cache must coordinate misses, not just serve hits.
A third failure is unbounded key growth. If the key space is derived from user input (e.g. caching per unique search query) without normalization or limiting, the cache fills with low-value entries, evicts the good ones, and the hit rate collapses. Keys must be bounded: normalize, truncate, or use a TTL that guarantees turnover.
Origin
LiveJournal in 2003 had a MySQL database that could not keep up with the site's read traffic. Brad Fitzpatrick wrote a small daemon that stored key-value pairs in RAM and served them over a simple text protocol. The daemon was named "memcached" — "mem" for memory, "cache" for cache, "d" for daemon. It was released as open source almost immediately and spread quickly through the early Web 2.0 companies (Flickr, Twitter, Facebook, Wikipedia), each of which contributed patches and operational lessons.
We had a database that was falling over under load, and we needed something that was faster than the database without being a database. That is the cache.
— Brad Fitzpatrick, on the origins of Memcached, 2003
Memcached's design informed later systems. Redis (2009) added persistence, replication, and richer data types on top of the same in-memory key-value model. Facebook's TAO (2013) built a geographically distributed graph cache that generalized Memcached's lessons. Memcached itself remains in production at enormous scale: Wikipedia serves every page view through it.
Evidence
FAQ
- Is Memcached the same as Redis?
No. Both are in-memory key-value stores, but they differ in scope. Memcached is deliberately minimal: multithreaded, cache-only, no persistence. Redis adds persistence, replication, pub/sub, and rich data types (lists, sets, hashes, sorted sets). Memcached is faster per-core for plain key-value; Redis is more versatile. Choose Memcached when you want a pure cache and nothing else.
- Does Memcached persist data across restarts?
No. Memcached holds everything in RAM and loses it all on restart. That is by design. If you need data to survive a restart, use Redis with persistence, or a database. Memcached is for the data you can recompute from the source of truth.
- How do I choose the TTL for a key?
Match the TTL to how quickly the data changes and how expensive the backend query is. A user profile that changes weekly can have a TTL of hours. A product price that changes daily can have a TTL of minutes. A live stock ticker should not be cached with a TTL at all. The TTL is the interval between "this is fresh" and "this is a stale guess".
- What is a cache stampede and how do I prevent it?
A cache stampede happens when a popular key expires and dozens of concurrent requests simultaneously miss, all query the backend, and all write back. The backend is overloaded. Prevent it with a lock (only one request recomputes) or a probabilistic early expiry (expire slightly before the TTL so one request refreshes it before the rest arrive). Facebook's "leases" paper is the standard reference.
- Should I use Memcached for session storage?
Usually not, because sessions need to survive a restart and Memcached loses everything. Redis or a database is a safer place for sessions. Memcached can be used for session data if losing sessions on restart is acceptable, but the choice should be made deliberately, not by default.
Connections
● broader ● narrower ● related
Broader
Related
Used in the garden
- The DBA-Free Playbook: Unlocking MySQL's Hidden Power for Your SaaS (2×)
- Architecting API Dominance: Design a REST API That Scales From Day One (1×)
- Mastering Content Curation: Build Your Own RSS Aggregator with PHP & SimplePie (1×)
- The Zero-Downtime WordPress Migration Blueprint for High-Stakes Sites (1×)
Sources
Memcached (2024). Memcached wiki. https://github.com/memcached/memcached/wiki
Fitzpatrick, B. (2003). Distributed Caching with Memcached. Linux Journal. https://www.linuxjournal.com/article/7451
Wikimedia (2024). Memcached. Wikitech. https://wikitech.wikimedia.org/wiki/Memcached
Facebook Engineering (2013). McDipper: A cache for flash storage. https://www.usenix.org/system/files/conference/nsdi13/nsdi13-final170_update.pdf
Atwood, J. (2015). Caching at Scale. USENIX ;login:. https://www.usenix.org/publications/login/summer2015/atwood
Etymology
Origin: compound
mem(noun): clipping of "memory", the store where the cache livescache(noun): from French cache, a hiding placed(noun): from "daemon", a background process
Semantic network
● broader ● narrower ● related
Broader
Related
Used in the garden
- The DBA-Free Playbook: Unlocking MySQL's Hidden Power for Your SaaS (2×)
- Architecting API Dominance: Design a REST API That Scales From Day One (1×)
- Mastering Content Curation: Build Your Own RSS Aggregator with PHP & SimplePie (1×)
- The Zero-Downtime WordPress Migration Blueprint for High-Stakes Sites (1×)
Sources
Memcached (2024). Memcached wiki. https://github.com/memcached/memcached/wiki
Fitzpatrick, B. (2003). Distributed Caching with Memcached. https://www.linuxjournal.com/article/7451
Wikimedia (2024). Memcached. https://wikitech.wikimedia.org/wiki/Memcached
{"Facebook Engineering (2013). McDipper":"A cache for flash storage. https:\/\/www.usenix.org\/system\/files\/conference\/nsdi13\/nsdi13-final170_update.pdf"}
Atwood, J. (2015). Caching at Scale. https://www.usenix.org/publications/login/summer2015/atwood