Skip to content

WordPress object cache: Redis or Memcached, and whether you need one

Which persistent object cache backend to run for WordPress, what WP_Object_Cache actually stores, when a persistent object cache buys you nothing, and how to prove it helped instead of assuming it did.

For a WordPress site that needs a persistent object cache, Redis is the right default. Not because it is faster — at the get-and-set traffic a WordPress request generates, the two backends are indistinguishable on a single host — but because the WordPress half of the decision is lopsided: the Redis drop-ins are actively maintained and the Memcached ones are not. Choose Memcached only when it is already installed, already serving something else on the box, and already wired to a drop-in you trust.

The more useful question is the one people skip: does this site need a persistent object cache at all? A brochure site whose traffic is anonymous and served from a full-page cache will see almost nothing, because PHP barely runs. A logged-in-heavy site — WooCommerce, a membership plugin, a busy wp-admin — is where the object cache changes the shape of the request.

What you’ll accomplish

  • A clear answer on whether this particular site benefits from a persistent object cache, based on measurement rather than a Site Health nag.
  • The backend chosen on the grounds that actually differ: drop-in maintenance, eviction behaviour, isolation, and what the host already runs.
  • A drop-in installed, salted, and isolated so two sites cannot read or flush each other’s keys.
  • An eviction policy and memory ceiling set deliberately rather than inherited from a package default.
  • A before-and-after measurement you can show someone, with hit ratio and query count rather than a vague “it feels faster”.

Prerequisites

  • WordPress 6.0 or newer. WordPress 6.6 changed the wp_options.autoload column to accept on, off, and auto alongside the historical yes and no, so the autoload query below has a version note.
  • WP-CLI available and able to bootstrap the site (wp core version returns a number rather than an error).
  • Shell access on the host running PHP, plus the ability to edit wp-config.php.
  • A Linux host. The nc -q1 flag used for Memcached stats is a GNU netcat option; the BSD netcat on macOS does not have it.
  • Either redis-server or memcached reachable from the PHP host, and the matching PHP extension. Shared hosting often gives you a Redis instance you do not control — that changes several steps and is covered under boundaries at the end.
  • A staging copy. Installing a drop-in on production first is how you find out that a plugin caches something it should not have.

How it works

WordPress ships an object cache whether or not you install anything. WP_Object_Cache is an in-memory associative array populated during the request by wp_cache_set() and read by wp_cache_get(), keyed by a key and a group. Out of the box it is non-persistent: the array is built from scratch on every request and thrown away when PHP finishes. Its only job is deduplication within a single page load, which is why fifty calls to get_option( 'siteurl' ) cost one query, not fifty.

Dropping a wp-content/object-cache.php file into place replaces that class with one backed by an external store, and the array survives the request. The next visitor does not re-fetch the option, the post object, the term relationships, or the user meta — they come back over a socket instead of over SQL. Groups matter here: most are per-site, but groups registered through wp_cache_add_global_groups() (users, user metadata, site options) are shared across every site in a multisite network. The reference is WP_Object_Cache in the WordPress developer handbook.

The reason this does nothing for a brochure site is upstream of PHP entirely.

Diagram comparing two WordPress request paths — an anonymous request served by the full-page cache that never reaches PHP, the object cache, or MySQL, and a logged-in or cart request that bypasses the page cache and consults the object cache before MySQL

If the page cache answers the request, nothing downstream runs. The object cache only earns its keep on requests that reach PHP. That distinction, and the failure modes that come from confusing the two layers, are worked through in object cache versus page cache.

Once you have decided you need one, the backends differ in ways that are real but narrower than the marketing suggests:

  • Persistence. Redis can write RDB snapshots or an append-only file, so a restart does not empty the cache; Memcached is memory-only and starts cold every time. For a cache role this matters less than it sounds, but it does affect the first minutes after a restart on a large site.
  • Eviction. Memcached does LRU per slab class and that is the whole story. Redis offers a choice, and for a cache the choice is allkeys-lru: every key is a candidate for eviction when memory fills. The default in many packages is noeviction, which makes Redis return an error on writes once it is full instead of making room. The policies and their trade-offs are documented in Redis key eviction.
  • Memory layout. Memcached allocates memory in fixed-size slab classes, so the mixed value sizes WordPress produces — a 40-byte option next to a multi-megabyte alloptions blob — can leave one slab class starved while another sits half empty. Redis allocates per key.
  • Threading. Memcached is multithreaded; Redis executes commands on a single thread. On a WordPress workload this rarely decides anything, because the bottleneck is round trips and PHP, not backend CPU.
  • Drop-in support. This is the one that actually decides it. The widely used, actively maintained WordPress drop-ins target Redis. Memcached drop-ins exist and work, but see far less traffic and far fewer fixes.

Decision tree for WordPress persistent object caching — first whether traffic reaches PHP at all, then whether logged-in views dominate, then whether Memcached is already wired up, ending at Redis with allkeys-lru as the default

Step-by-step

  1. Find out what the site does today. Before installing anything, read the current state.

    wp cache type
    wp eval 'var_dump( wp_using_ext_object_cache() );'
    ls -l wp-content/object-cache.php

    On a site with no persistent cache you get Default, bool(false), and a “No such file or directory” error. Anything else means a drop-in is already in place and you are debugging, not installing.

  2. Measure the baseline on a page that actually runs PHP. Query counts on an anonymous page tell you nothing if that page is served from the edge. Profile a logged-in or cart URL instead.

    wp package install wp-cli/profile-command:@stable
    wp profile stage --url=https://example.com/my-account/

    The table reports query_count, query_time, cache_hits, cache_misses, and cache_ratio per bootstrap stage. Write those numbers down: query_count on the main_query and template stages is the figure to beat.

  3. Check the autoloaded options first. A bloated alloptions blob makes every request pull the whole thing across the socket, and an object cache can make that worse rather than better.

    wp db query "SELECT ROUND(SUM(LENGTH(option_value))/1024) AS kb, COUNT(*) AS n FROM wp_options WHERE autoload = 'yes';"

    On WordPress 6.6 and newer, change the condition to autoload IN ('yes','on','auto'). Under a few hundred kilobytes is healthy; several megabytes means you fix autoload before you add a cache backend.

  4. See what the host already has. The answer here often settles the Redis-or-Memcached question on its own.

    php -m | grep -E '^(redis|memcached|memcache)$'
    redis-cli -h 127.0.0.1 -p 6379 ping
    echo -e 'version\r' | nc -q1 127.0.0.1 11211

    A working Redis answers PONG. A working Memcached answers VERSION 1.6.21 or similar. If neither responds, install the daemon and the matching PHP extension before continuing. If only Memcached responds and it is already serving another application, do not add a second daemon just to have Redis.

  5. Install the drop-in (Redis path). The plugin writes wp-content/object-cache.php for you; do not copy it by hand.

    wp plugin install redis-cache --activate
    wp redis enable
    wp redis status

    Success looks like Status: Connected, a Client: line naming PhpRedis or Predis, and Drop-in: Valid. Drop-in: Invalid means some other file is sitting at that path.

  6. Salt and isolate the keys before any traffic hits it. This is the step people skip and then spend an afternoon on. Add to wp-config.php, above the “That’s all, stop editing” line:

    define( 'WP_REDIS_HOST', '127.0.0.1' );
    define( 'WP_REDIS_PORT', 6379 );
    define( 'WP_REDIS_DATABASE', 3 );
    define( 'WP_REDIS_PREFIX', 'shopprod:' );
    define( 'WP_CACHE_KEY_SALT', 'shop.example.com:' );
    define( 'WP_REDIS_MAXTTL', 86400 );

    WP_CACHE_KEY_SALT is the core-level constant that prefixes cache keys; WP_REDIS_DATABASE and WP_REDIS_PREFIX are the drop-in’s own isolation. Use all three when several installs share one daemon — two staging copies on one unsalted Redis will read each other’s user sessions, and a flush on one empties the other. WP_REDIS_MAXTTL caps keys that would otherwise never expire.

  7. Set the memory ceiling and the eviction policy deliberately. A cache with noeviction is not a cache; it is a store that starts failing writes when it fills.

    redis-cli config set maxmemory 512mb
    redis-cli config set maxmemory-policy allkeys-lru
    redis-cli config rewrite

    Each command returns OK. config rewrite persists the change into redis.conf so a restart does not lose it — without it, the setting survives only until the daemon stops. Size maxmemory against what the host can spare with PHP-FPM’s own footprint accounted for, not against total RAM.

  8. If you are staying on Memcached, know what you are choosing. Point the drop-in at the daemon, then watch the slabs rather than a single memory number.

    memcached-tool 127.0.0.1:11211 display

    The output lists each slab class with its item size, item count, and eviction time. A class evicting every few seconds while others sit idle is slab starvation; the fix is restarting Memcached with a different growth factor, or moving to Redis.

Validate it works

Re-run the profile from step 2 against the same URL and compare like with like:

wp profile stage --url=https://example.com/my-account/

cache_ratio should climb on the second and subsequent runs and query_count should fall on the main_query and template stages. A ratio that does not move between runs means the drop-in is loaded but not persisting.

Confirm the backend is genuinely being written to, in the database you configured:

redis-cli -n 3 dbsize
redis-cli -n 3 --scan --pattern 'shopprod:*' | head -5

dbsize returns something like (integer) 4213 and the scan prints real keys carrying your prefix. An empty index alongside a healthy wp redis status means the drop-in is talking to a different index than you think.

Then read the hit ratio at the backend, which is the number that matters over days rather than seconds:

redis-cli info stats | grep -E 'keyspace_hits|keyspace_misses|evicted_keys'
redis-cli info memory | grep -E 'used_memory_human|maxmemory_human|maxmemory_policy'

Hits should outnumber misses by a wide margin on a warm site, and steadily climbing evicted_keys means the ceiling is too low for the working set. On Memcached the equivalent read is:

echo -e 'stats\r' | nc -q1 127.0.0.1 11211 | grep -E 'get_hits|get_misses|evictions'

Troubleshooting

  • wp cache type still says Default after enabling the plugin. Either another plugin overwrote wp-content/object-cache.php or OPcache is serving a stale copy. Run wp redis update-dropin, then restart PHP-FPM. The file’s first lines name the plugin that claims it.
  • Two sites returning each other’s data. Unsalted keys on a shared daemon. Set WP_CACHE_KEY_SALT per install and give each one its own WP_REDIS_DATABASE, then flush both. Verify with redis-cli -n 0 --scan | head -20 that database 0 is not quietly holding everything.
  • OOM command not allowed when used memory > 'maxmemory' in the PHP log. The eviction policy is noeviction and Redis is full. Set allkeys-lru and config rewrite. If evictions continue at a high rate afterwards, raise maxmemory or shorten WP_REDIS_MAXTTL — the working set is larger than the ceiling.
  • Site got slower after enabling the cache. Usually a large alloptions value being serialised in and out on every request, or a plugin storing enormous transients. Re-run the autoload query from step 3 and look for single options in the hundreds of kilobytes. Fix the data, not the cache.
  • SERVER_ERROR object too large for cache on Memcached. The default item limit is 1 MB and something in WordPress exceeded it. Raising the limit with the daemon’s -I option is a workaround; the honest fix is finding the plugin storing a multi-megabyte value in a cache group.

When this is the wrong approach

A persistent object cache is not free, and there are sites where it is the wrong first move.

  • Anonymous traffic behind a working page cache. If most requests never reach PHP, spend the effort on page-cache hit ratio instead. Adding Redis here produces a green Site Health check and no measurable change.
  • A shared Redis you do not control. On shared hosting you often cannot set maxmemory-policy or pick a database index. The cPanel and WHM specifics — per-account daemons, drop-in placement, isolation — belong in persistent object cache on cPanel rather than here.
  • A LiteSpeed stack. LSCache has its own object cache integration and its own way of wiring the drop-in, so the steps above are not the ones you want. Follow the LiteSpeed object cache guide instead.
  • Covering for a plugin problem. A cache that hides a four-second query still leaves it taking four seconds on every miss and every cold start after a deploy. Profile first.
  • Treating the cache as durable storage. Redis persistence is for warm restarts, not for data you cannot lose. Anything that must survive belongs in MySQL.

Running WordPress that earns real money deserves an operator — Stack Harbor runs that stack under Enterprise WordPress; book an infrastructure consult.