Memcached explained in plain terms means understanding the simple caching system that keeps the results of slow work in fast memory, so a website does not have to do the same work twice. The central question is how a busy site can answer thousands of requests a second when its database can only answer a few hundred, and the answer is that most of those requests are answered from a cache instead.
Memcached is a free, open-source, in-memory key-value store, first released in 2003 by Brad Fitzpatrick, who built it to make the blogging platform LiveJournal faster. It is deliberately simple: you store a value under a key, and you ask for it back by that key, and everything else is left to the application.
That simplicity is the point. Memcached does one thing, and it does it very quickly, which is why it still powers some of the largest websites in the world more than twenty years later. This article explains what memcached is, how it works, and where it is used.
Key takeaways
- Memcached stores key-value pairs in RAM for very fast access.
- It is a cache, not a database: data can disappear at any time.
- It spreads across many servers, with clients choosing the server.
- Items expire after a set time, or are evicted when memory fills up.
- It is used to take load off databases on busy websites.
What memcached is
Memcached is a distributed memory caching system, which is a precise way of saying that it is a shared place to keep temporary data in RAM, where it can be read in microseconds instead of the milliseconds a disk or a database would need. It holds no permanent data, keeps no copy on disk, and makes no promise that anything stored will still be there later.
The system was created at LiveJournal, where a single database server could no longer keep up with the site’s growth. Fitzpatrick’s answer was to put a layer of memory in front of the database: the application would check the cache first, and only ask the database when the cache did not have the answer. The database load fell dramatically, and the pattern spread to almost every large website.
Memcached runs as a server process, and applications talk to it over a simple text protocol, or a binary one, usually through a client library in the application’s own language. A cluster is just several memcached servers side by side, and the client decides which server holds each key, so the system scales by adding more memory. Our guide to Redis explains the more feature-rich store that is often compared with memcached, and our guide to caching explains the wider idea that memcached is one example of.
How memcached works
Storing a value is a SET command with a key, a lifetime, and the data itself. The server places the data in RAM and notes when it should expire. Reading it back is a GET command with the same key, and the answer is either the stored value, called a cache hit, or nothing at all, called a cache miss, which tells the application to do the slow work and then store the result for next time.
The data model is deliberately flat. A key is a short string, up to 250 characters, and a value is an opaque block of bytes, up to one megabyte by default. Memcached never looks inside a value, never indexes it, and never queries it, which is what keeps it fast: there is no query language to parse and no structure to maintain.
Memory is managed in slabs. Instead of allocating memory in whatever sizes requests need, memcached divides its memory into classes of fixed-size chunks, and each item goes into the smallest class that fits it. This avoids fragmentation, the slow waste of memory that happens when differently sized items are mixed together, and it keeps allocation almost free.
When memory fills up, memcached evicts the least recently used items to make room, a policy known as LRU. Nothing is written to disk first, because the data is only a copy of something the database already holds, and losing it simply means the next request will be a cache miss. Items also expire on their own when their lifetime ends, which is how stale data is kept from lingering.
Distribution is handled by the client, not the server. Each client hashes the key to choose a server, usually with consistent hashing, so that adding or removing a server moves as few keys as possible. The servers never talk to each other, which keeps the design simple, but it also means a failed server simply loses its keys, and the application rebuilds them from the database as needed.

Comparing memcached with Redis
Memcached and Redis are the two best-known in-memory stores, and they are often compared, but they were designed for different jobs. Memcached is a pure cache, while Redis is a data structure server that happens to keep data in memory, and the difference shapes everything else.
The table below sets out the main contrasts between the two.
| Aspect | Memcached | Redis |
|---|---|---|
| Data model | Strings only | Strings, lists, sets, hashes |
| Persistence | None, memory only | Optional, to disk |
| Scaling | Clients shard the keys | Cluster mode built in |
| Extra features | Almost none | Pub-sub, transactions, scripts |
| Best for | Simple, huge caches | Rich data and queues |
The practical result is that memcached wins when the job is purely to cache strings at enormous scale with minimal fuss, while Redis wins when the application needs data structures, persistence, or features beyond a simple key and value.
Where memcached is used
Social networks and large websites are the classic case. Facebook, which once ran one of the largest memcached deployments in the world, used it to cache the rendered fragments of pages, user sessions, and database query results, so that the databases behind the site were asked only a fraction of the questions the site actually answered.
Session storage is a second major use. When a user logs in to a website, the details of their session are stored in memcached under a random key, and the user’s browser carries only the key. Any web server in the fleet can then handle the next request, because the session is in the shared cache rather than on one machine.
Database query results are cached the same way. An expensive query, such as the list of popular posts or the count of unread messages, is run once and its result stored under a key built from the query itself. The next thousand requests for the same result are answered from memory, and the database is spared the repeated work.
Rendered page fragments are cached too. A website can store the HTML of a sidebar, a navigation menu, or a product listing, and reuse it across many pages and many users, rebuilding it only when the underlying data changes. This is how a site can serve personalised pages without rebuilding every piece for every visitor.
Rate limiting and counters use memcached as well. A counter that tracks how many requests an address has made in the last minute is a natural fit for a fast key-value store, and the fact that the data can disappear is acceptable, since the worst case is a counter that restarts from zero.
Strengths and limits
The greatest strength of memcached is its speed and simplicity. It does one thing, it does it in microseconds, and it has almost no configuration to get wrong. A team can add it to a struggling website in an afternoon and see the database load fall the same day, which is why it became the default first step in scaling a web application.
It is also a strength that memcached scales by adding memory. More servers mean more cache, and because the clients do the sharding, there is no central coordinator to become a bottleneck. The system grows in a straight line with the hardware.
The limits are real, though. Memcached stores only strings, so anything more complex must be serialised by the application, and it offers no persistence, no replication, and no security beyond a firewall, since anyone who can connect can read and write everything. A cache that holds session data must therefore sit behind a private network, and any data it holds must be treated as temporary.
Memcached checklist
- Cache only data that can be rebuilt from the database if it disappears.
- Set a sensible expiry on every item, so stale data does not linger.
- Build cache keys from the exact query or request, so different answers never collide.
- Invalidate cached items when the underlying data changes.
- Monitor the hit rate: a low rate means the cache is not helping.
- Give memcached enough memory, since a small cache evicts constantly.
- Keep memcached behind a firewall, since it has no authentication of its own.
- Remember that a server failure loses its keys, and the system must cope.
- Measure before and after, so you know the cache is earning its place.
Frequently asked questions
What is memcached in simple terms?
Memcached is a fast, shared memory store where a website keeps the results of slow work, so it can answer the same question from memory instead of asking the database again.
Is memcached a database?
No. It is a cache, which means the data it holds is temporary and can disappear at any time. A database is the permanent store; memcached is the fast copy in front of it.
What is the difference between memcached and Redis?
Memcached stores only strings and offers no persistence or extra features, which keeps it very fast and simple. Redis stores richer data structures and can save data to disk, at the cost of more complexity.
What happens when memcached runs out of memory?
It evicts the least recently used items to make room. Nothing is saved to disk, since the data is only a copy, and the next request for an evicted item will simply be a cache miss.
Can memcached lose data?
Yes, and that is by design. A restart, a server failure, or an expiry all remove data, and the application is expected to rebuild it from the database when it is next needed.
Is memcached still used today?
Yes, widely. It remains the standard choice for simple, large-scale caching, and it runs behind many of the busiest websites, even as Redis takes the cases that need richer features.
The bottom line
Memcached explained in one line is this: a deliberately simple, very fast memory store that lets a website answer repeated questions from RAM instead of asking its database every time.
It does one job, and it does it well enough that it has survived two decades of fashion in infrastructure. When the problem is a database that cannot keep up, the answer is usually not a bigger database, but a cache in front of it, and memcached is still the simplest way to build one.
This article is general information about how memcached works, not advice about caching or scaling a database for a particular system.







