Memcached is an in-memory caching system that stores database results and other data to speed up dynamic websites.

Memcached is a simple, very fast key-value store that keeps data in a server's memory (RAM) instead of on disk. Applications use it to remember the results of expensive work, such as a database query or a rendered chunk of a page, so they don't have to repeat that work on every request.

For example, a news site might run the same query to fetch its ten latest headlines thousands of times an hour. With Memcached, the first request runs the query and stores the answer under a key like latest_headlines. Every request after that reads the answer straight from memory in a fraction of a millisecond, until it expires.

How Memcached works

Memcached runs as a small service on a server, listening for requests from your application. The application does three basic things:

  1. Get: ask Memcached for a key. If it's there (a cache hit), use it.
  2. Set: if it isn't (a cache miss), do the slow work, then store the result with an expiry time.
  3. Delete: remove a key when the underlying data changes.

When Memcached runs out of memory, it evicts the least recently used items to make space. Everything lives in RAM, so restarting the service empties the cache. That's fine, because a cache only holds copies of data that's stored properly elsewhere.

Why Memcached matters for your website

Dynamic sites built on PHP and MySQL spend much of their time waiting for the database. Caching repeated results in memory cuts that waiting, lowers database load and improves server response time. It's one way to provide an object cache for software like WordPress, Drupal and Magento.

Memcached vs Redis

Memcached and Redis are the two most common in-memory caches, and both are fast. The main differences:

  • Data types: Memcached stores plain strings. Redis also supports lists, sets, hashes and more.
  • Persistence: Memcached is memory-only. Redis can optionally save its data to disk.
  • Features: Redis adds replication, pub/sub messaging and scripting.
  • Threads: Memcached is multi-threaded, which can help on very large, busy servers.

For a typical website, either works well as an object cache. Redis is more commonly offered by hosts today because one service covers caching, sessions and queues.

Common Memcached mistakes

  • Caching data without an expiry, so visitors see stale content.
  • Forgetting to clear the cache after changing data directly in the database.
  • Giving Memcached too little memory, so items are evicted before they're reused.
  • Treating it as permanent storage. Anything in Memcached can disappear at any time.

Memcached questions

Is Memcached better than Redis?
Not for most websites. Both are fast enough as an object cache, but Redis offers more data types and optional persistence, which is why many hosts and plugins favour it.
Does Memcached store data permanently?
No. Memcached keeps everything in memory, so data is lost when the service restarts or when it needs room for newer items.
Can WordPress use Memcached?
Yes. With an object cache plugin or drop-in, WordPress can store database query results in Memcached, which reduces database load on busy sites.
Was this definition helpful?
Share this definition