WordPress

WordPress Object Caching Setup: Redis, Memcached and When Neither Helps

WordPress object caching setup with Redis or Memcached stores database query results in memory so repeat requests avoid the database. By default WordPress caches objects only in memory for a single request; a persistent drop-in in wp-content/object-cache.php keeps them across requests. In my builds the win comes on repeat-read-heavy sites, not on slow queries that need indexing.

By Saif Al Jahanwebdev-saif.com

What the WordPress object cache actually is

The WordPress object cache is an in-memory key-value store that the core and plugins use through the WP Object Cache API: wp_cache_get(), wp_cache_set(), wp_cache_add(), wp_cache_delete() and wp_cache_flush(). Every value lives under a group and a key, for example group "posts" with the key set to the post ID. Out of the box the cache is non-persistent: it exists only for the duration of one PHP request, then disappears. That still removes repeated database reads inside a single request, but it does nothing for the next visitor. It is also not a page cache — a cached object still requires PHP to rebuild the HTML. Understanding that distinction is the difference between a measured improvement and an unexplained one.

  • API calls: wp_cache_get(), wp_cache_set(), wp_cache_delete(), wp_cache_flush()
  • Values are addressed by group plus key, not by SQL query string
  • Default cache lifetime is one request; nothing survives to the next visitor
  • Object cache is distinct from page cache, opcode cache and browser cache

Persistent vs non-persistent cache

Persistence comes from a single drop-in file: wp-content/object-cache.php. WordPress loads it before plugins and routes every wp_cache_* call through it. With Redis or Memcached behind that drop-in, values survive between requests and across PHP workers, so the second visitor reads from memory instead of re-running the query. Redis gives you data types, optional disk persistence and keyspace notifications; Memcached is a simpler, multithreaded, pure-memory store with no persistence. Groups can be declared global with wp_cache_add_global_groups() so multisite sites share them, or non-persistent with wp_cache_add_non_persistent_groups() when values must never be reused. I use the latter for user-specific or session-bound data. Choosing the group policy matters more than the benchmark between the two engines.

  • Drop-in path: wp-content/object-cache.php, loaded before plugins
  • Global groups via wp_cache_add_global_groups() for multisite sharing
  • Non-persistent groups via wp_cache_add_non_persistent_groups() for user data
  • Redis: richer data types and optional persistence; Memcached: memory-only

Setting up Redis or Memcached as the drop-in

On a typical install I enable the Redis Object Cache plugin, confirm the PHP redis extension is loaded, then set WP_REDIS_HOST, WP_REDIS_PORT and WP_REDIS_DATABASE in wp-config.php before activating the drop-in. The plugin copies its own object-cache.php into wp-content; dropping in a file from a random gist is how people end up with a cache that silently fails on multisite. For Memcached I install the memcached extension and a maintained drop-in such as Memcached Redux, then verify the daemon is reachable over the configured socket or TCP port. After enabling, I confirm in Query Monitor that the Object Cache panel reports Redis or Memcached rather than the default in-memory cache, and run wp redis status or redis-cli INFO to check the connection.

  • Install the PHP redis or memcached extension first
  • Set WP_REDIS_HOST, WP_REDIS_PORT and WP_REDIS_DATABASE in wp-config.php
  • Activate the drop-in through the plugin, not a hand-copied unknown file
  • Verify in Query Monitor that the backend changed from the default cache
  • Confirm the daemon is reachable over TCP or a Unix socket

Cache invalidation: the part that breaks correctness

A persistent cache is only correct if writes invalidate what they change. WordPress core flushes groups on actions such as save_post, updated_option and switch_theme, and WooCommerce invalidates product and order caches on stock and price updates. The failure mode is custom code that calls wp_cache_set() with a key derived from a variable nobody ever deletes: the value becomes permanently stale until a manual flush. I avoid blanket wp_cache_flush() because it wipes the entire keyspace and causes a thundering herd on the next request. Instead I delete the exact group and key on the write hook, and keep a short expiry for data derived from external APIs. Watch the alloptions cache: a noisy option update can invalidate a large shared value on every request.

  • Delete the exact group and key on the write hook, not the whole store
  • Hook invalidation to save_post, updated_option and WooCommerce stock changes
  • Give third-party API-derived values a short expiry
  • Track the alloptions cache; frequent option updates cause mass invalidation
  • Prevent stampedes by regenerating one key rather than flushing everything

Measuring the hit ratio and verifying it works

I do not call a caching setup successful until I can show a hit ratio. redis-cli INFO stats gives keyspace_hits and keyspace_misses, and the ratio over a fixed window tells you whether anything is being reused. Query Monitor shows object cache hits, misses and the database queries that still run. On a read-heavy site a practical target is a hit ratio above 80 percent after warm-up; below that, the application is probably not using the object cache for the expensive calls. I compare median server response time before and after on the same traffic mix, and watch memory usage so the cache never evicts under load. Redis eviction policies such as allkeys-lru need a deliberate choice, not the default.

  • redis-cli INFO stats: keyspace_hits versus keyspace_misses
  • Query Monitor object cache panel: hits, misses and remaining queries
  • Practical target above 80 percent hit ratio after warm-up
  • Pick an eviction policy deliberately; allkeys-lru is not a blind default
  • Compare median server response time before and after on the same traffic mix

When object caching is not the fix

Object caching does not make a slow query fast — it makes a fast query run less often. If a query is slow because of a missing index, a cart product-variable explosion, or an unindexed meta_query, the cached copy merely hides the problem until it expires. It also cannot help when the bottleneck is external API latency, uncached full-page HTML, a missing OPcache, or an origin with no page cache in front of it. Low-traffic sites often gain nothing because there is no repeat read to reuse. Before adding Redis I check Query Monitor's slow query panel, the database indexes, and whether a page cache is already in place. When the database is the constraint, indexing and query shape beat any cache.

  • Missing indexes, unindexed meta_query and N+1 query loops
  • External HTTP API latency and slow third-party calls
  • No page cache or no OPcache in front of PHP
  • Low traffic: no repeat reads means no cache hits
  • Stale-cache bugs caused by gaps in invalidation

Where I draw the line in a WordPress build

In my WordPress and PHP builds I treat object caching as one layer, not a performance strategy. I first fix query shape and indexes, then add a page cache, then introduce Redis when measurements show the same reads repeating across requests. On WooCommerce stores I keep cart, session and customer-specific groups non-persistent so one shopper never sees another shopper's data. On custom PHP applications I use the same key-and-group discipline through a thin wrapper so invalidation stays explicit. The outcome I look for is fewer database round trips at the same correctness — not a dashboard that looks impressive. That is how I keep the builds on webdev-saif.com predictable under load.

Frequently asked questions

What is WordPress object caching?

WordPress object caching stores the results of expensive database queries in memory under a group and key, so later calls return the cached value instead of querying again. By default the cache lasts one PHP request. A persistent drop-in backed by Redis or Memcached keeps values across requests, which reduces database load on repeat reads.

What is the difference between persistent and non-persistent object cache?

A non-persistent cache exists only during a single request and is discarded when PHP finishes. A persistent cache writes through a wp-content/object-cache.php drop-in to Redis or Memcached, so values survive across requests and workers. Persistent cache reduces database load between visitors; non-persistent cache only removes duplicate reads inside one request.

How do I know object caching is actually working?

Check that a real backend loaded: Query Monitor's Object Cache panel should name Redis or Memcached, not the default in-memory cache. Then measure hit ratio with redis-cli INFO stats (keyspace_hits versus keyspace_misses) over a fixed window. Above roughly 80 percent after warm-up means values are being reused; near zero means the expensive calls are not using the cache.

Does Redis or Memcached speed up a slow WooCommerce site?

Only if the slowness comes from repeated reads. Object caching does not fix an unindexed meta_query, an N+1 query loop, slow external API calls, or the absence of a page cache. In my WooCommerce builds I first measure with Query Monitor, fix query shape and indexes, add full-page caching, then use Redis to remove the remaining repeated reads. Cart and session groups stay non-persistent.

What happens when cached data goes stale?

Stale data means a write did not invalidate the right key. WordPress core flushes relevant groups on save_post and updated_option, and WooCommerce invalidates on stock and price changes. Custom code must call wp_cache_delete() on the same group and key it wrote. I avoid blanket wp_cache_flush() because it wipes everything and causes a stampede on the next request; delete the exact key instead.

Should I use Redis or Memcached for WordPress?

Redis is the safer default: richer data types, optional disk persistence, keyspace notifications and broad WordPress plugin support through the Redis Object Cache plugin. Memcached is simpler and memory-only, which suits a cache that should never survive a restart. For most WordPress installs I pick Redis; the engine matters less than correct invalidation and a measured hit ratio.

Written by Saif Al Jahan, webdev-saif.com · Mymensingh, Bangladesh. Last updated .