WordPress Object Cache Deep Dive: Redis and Memcached Configuration

A comprehensive guide to WordPress object caching with Redis and Memcached, covering installation, configuration, and best practices for high-traffic sites.

Object caching is one of the most effective performance optimizations for WordPress. While page caching serves entire HTML pages, object caching stores the results of database queries in memory. This dramatically reduces database load, speeds up admin pages, and improves dynamic content delivery. The two most popular object caching backends are Redis and Memcached.

This guide covers how to set up and optimize object caching for WordPress using Redis and Memcached.

Why Object Caching Matters

WordPress makes a lot of database queries. A typical page with 20 plugins runs 30-50 queries. Each query takes time and consumes database resources. Page caching handles anonymous visitors, but logged-in users, admin pages, and dynamic content still need database queries. Object caching stores the results of these queries in memory so they don’t need to be repeated on every page load.

Without object caching, each page load repeats the same queries — options queries, post meta queries, term queries — from scratch. With object caching, these queries are served from memory in microseconds.

Redis Setup

Redis is an in-memory data structure store that works as an excellent WordPress object cache backend. Install Redis on your server with the package manager — apt-get install redis-server on Ubuntu. Install the Redis PHP extension. With both installed, install a Redis object cache plugin like Redis Object Cache or Predis.

The Redis Object Cache plugin connects to your Redis server, replaces WordPress’s default object cache with Redis, and provides a dashboard showing cache hit rates, memory usage, and connection status.

Memcached Setup

Memcached is the older alternative to Redis, designed specifically for caching. It’s simpler than Redis but also more limited — it doesn’t support persistence or complex data structures. Install Memcached and the PHP Memcached extension on your server. Install a Memcached object cache plugin to connect WordPress to Memcached.

Memcached requires less RAM than Redis for equivalent workloads but doesn’t survive server restarts — a warm cache is lost when Memcached restarts. Redis is the recommended choice for new installations.

Configuration Best Practices

Configure Redis or Memcached to use a dedicated port (default 6379 for Redis, 11211 for Memcached). Set a RAM limit appropriate for your site — 64MB for small sites, 256MB for medium sites, 1GB+ for large sites. Configure eviction policy — allkeys-lru for Redis removes the least recently used keys when memory is full.

Monitor your cache hit rate. A hit rate above 80% indicates good caching efficiency. Below 50% suggests your cache is too small or your data access patterns aren’t cache-friendly. Use a monitoring tool like RedisInsight or the plugin’s dashboard to track hit rates.

Cache Invalidation

Object cache invalidation happens automatically in WordPress. When data changes — a post is updated, a setting is saved — WordPress calls wp_cache_delete() to remove the affected cache keys. Object cache plugins also handle group-based invalidation, clearing all keys in a group when any data in that group changes.

If your plugin caches custom data with wp_cache_set(), ensure you call wp_cache_delete() when the underlying data changes. Stale cached data causes subtle bugs that are difficult to diagnose.

The Bottom Line

Redis object caching is one of the highest-impact performance optimizations for WordPress sites. It reduces database load, speeds up admin pages, and improves the experience for logged-in users. Install Redis on your server, add a Redis object cache plugin, and monitor your hit rate. Most sites see a 50-80% reduction in database queries after enabling Redis, with corresponding improvements in page load times.

Leave a Reply

Your email address will not be published. Required fields are marked *