Home › Blog › Article

What Redis Fixes in the WordPress Object Cache

What Redis Fixes in the WordPress Object Cache

Install Redis as a persistent WordPress object cache to stop repeating expensive database queries on every request. Point WP_REDIS_HOST and WP_REDIS_PORT at your instance, drop object-cache.php into wp-content/, and confirm hits with redis-cli info stats plus the Query Monitor panel. You will know it worked when keyspace_hits climbs, hit rate stays above roughly 90 percent, and wp_options autoloaded queries drop in your slow log. Roll back by renaming that drop-in to object-cache.php.bak and flushing Redis.

How the WordPress Object Cache Works

WordPress ships a default object cache in wp-includes/cache.php built on the WP_Object_Cache class. It stores data in a PHP array that dies when the request ends. Redis replaces that array with a network key-value store through a drop-in file at wp-content/object-cache.php, so cached groups survive across requests and across PHP workers.

When a plugin calls wp_cache_set( 'my_key', $data, 'my_group' ), the Redis drop-in serializes the value and writes it to redis under a namespaced key like wp:my_group:my_key. A later wp_cache_get() in a different request reads it back without touching MySQL. The WP_Object_Cache reference documents the API surface every drop-in must honor.

A Minimal Runnable Example

Put this in a mu-plugin at wp-content/mu-plugins/redis-demo.php after Redis is live. It writes, reads, and reports a hit so you can verify the backend is real:

<?php
// Verify the Redis object cache actually persists across requests.
add_action( 'init', function () {
    $key   = 'runokwp_demo_counter';
    $group = 'runokwp_demo';

    $count = wp_cache_get( $key, $group );
    if ( false === $count ) {
        $count = 0;
        wp_cache_set( $key, $count, $group, 3600 );
    }
    $count++;
    wp_cache_set( $key, $count, $group, 3600 );

    if ( isset( $_GET['redis_demo'] ) ) {
        wp_die( 'Counter value: ' . (int) $count );
    }
} );

Load /?redis_demo=1 twice. If the number climbs from 1 to 2, Redis is persisting state between requests. Delete the file when you are done.

Step-by-Step Setup Process for Redis Object Cache

  1. Install Redis 7.2 or newer on the host and confirm redis-cli ping returns PONG.
  2. Add define( 'WP_REDIS_HOST', '127.0.0.1' ); and define( 'WP_REDIS_PORT', 6379 ); to wp-config.php.
  3. Install the Redis Object Cache plugin or copy object-cache.php from the redis-cache package into wp-content/.
  4. Enable it from Settings → Redis and click "Flush Cache" once.
  5. Run redis-cli info stats and check that keyspace_hits is growing.
  6. Keep the drop-in file name handy so you can rename it to roll back fast.

Object Cache vs Page Cache

Get a Quote

Email + requirement + budget range — we reply with a quote within 24 hours.

A page cache such as Nginx FastCGI cache, Varnish, or a CDN stores the final rendered HTML for a URL and serves it before PHP boots. It helps logged-out traffic on mostly static pages. The object cache sits inside PHP and stores the pieces WordPress uses to build a page: options, post meta, term data, user sessions, and transient fragments.

They solve different problems. Page cache hides a slow backend; object cache makes the backend itself cheaper. A WooCommerce cart page, a REST API response, and a logged-in dashboard all bypass most page caches but lean hard on the object cache. If you run a membership site or a store, the object cache is where the win lives. For broader speed work see our notes on moving Core Web Vitals for real.

When a Redis Object Cache Pays Off

It pays off when your database is the bottleneck and queries repeat. High-traffic editorial sites, multisite networks, and stores with heavy wp_postmeta and wp_options reads see the clearest gains. If your MySQL instance shows hundreds of queries per request and most are identical, Redis removes them.

It does not pay off on a tiny brochure site with three pages and no dynamic data; the added ops surface is not worth it. It also will not fix bad queries, missing indexes, or an unoptimized theme. Use it when SHOW FULL PROCESSLIST or Query Monitor shows repeated SELECT calls against autoloaded options, not when you just want a bigger number on a synthetic benchmark.

Reading the Hit Rate

Hit rate is the share of wp_cache_get() calls served from Redis instead of the database. Compute it from Redis itself:

redis-cli info stats | grep -E 'keyspace_(hits|misses)'
# hit_rate = hits / (hits + misses)

A healthy production cache runs above 90 percent. Below 80 percent means your TTLs are too short, your groups are not persistent, or you are flushing too often. In the Redis Object Cache plugin settings screen you can also read Hits, Misses, and Ratio directly. Watch the ratio for a full traffic cycle, not five minutes after a flush, or you will misread cold-start misses as a broken config. For teams building custom backends, our custom WordPress development service tunes these numbers on live infrastructure.

Common Failure Modes and How to Roll Back

Common Failure Modes and How to Roll Back

The classic failure is a stale drop-in after a Redis upgrade: PHP cannot connect, every wp_cache_get() falls through to MySQL, and the site gets slower, not faster. Another is WP_CACHE_KEY_SALT collisions on staging clones sharing one Redis instance, where one site reads another site’s options.

Memory pressure shows up as maxmemory evictions in redis-cli info stats; set maxmemory-policy allkeys-lru in redis.conf to degrade gracefully. If the site white-screens after enabling the cache, roll back immediately:

mv wp-content/object-cache.php wp-content/object-cache.php.bak
redis-cli flushall

That restores the default in-memory cache within seconds. Always test the rollback path before you need it, and keep the drop-in under version control so you can diff config changes. More deployment patterns are in our technical column index.

Action Checklist

  • Confirm Redis answers PONG and note the exact version you run in 2026.
  • Set WP_REDIS_HOST and WP_REDIS_PORT in wp-config.php before installing the drop-in.
  • Verify persistence with the counter snippet and a second request.
  • Track keyspace_hits versus misses for a full day before tuning TTLs.
  • Practice the mv rollback so a bad deploy costs seconds, not hours.

Need the cache tuned on your stack rather than guessed at? See how our team handles custom WordPress development for production sites that need measurable wins.

Frequently asked questions

Does Redis replace my page cache?

No. Page cache serves HTML to logged-out visitors before PHP runs; Redis object cache removes repeated database queries during PHP execution. Most production sites run both layers together.

What hit rate should I target in 2026?

Aim for above 90 percent after a warm-up period. Below 80 percent means short TTLs, frequent flushes, or non-persistent groups; check redis-cli info stats over a full traffic day.

How fast can I undo a broken object cache?

Rename wp-content/object-cache.php to object-cache.php.bak and run redis-cli flushall. WordPress falls back to the default per-request cache instantly.

“, “faq”: [{“question”: “Does Redis replace my page cache?”, “answer”: “No. Page cache serves HTML to logged-out visitors before PHP runs; Redis object cache removes repeated database queries during PHP execution. Most production sites run both layers together.”}, {“question”: “What hit rate should I target in 2026?”, “answer”: “Aim for above 90 percent after a warm-up period. Below 80 percent means short TTLs, frequent flushes, or non-persistent groups; check redis-cli info stats over a full traffic day.”}, {“question”: “How fast can I undo a broken object cache?”, “answer”: “Rename wp-content/object-cache.php to object-cache.php.bak and run redis-cli flushall. WordPress falls back to the default per-request cache instantly.”}], “cover”: “Modern flat technical illustration of a WordPress server stack: a PHP worker node, a MySQL cylinder, and a glowing Redis key-value store linked by arrows showing cached query objects moving between them, clean vector style, no text or logos.

References & further reading

Need WordPress Development?

Free proposal and quote within 24 hours — we serve clients worldwide.

QQ