Skip to main content

WordPress Object Cache: Redis vs Memcached (Setup Guide)

9 months ago
0
0
0

If your WordPress site already has page caching but still feels sluggish in the admin or on dynamic pages (search, account, cart), you’re bottlenecked by database queries. That’s where the WordPress Object Cache comes in. With Redis or Memcached, WordPress can fetch repetitive query results from memory instead of hitting MySQL every time—fewer queries, faster TTFB, happier users.

What Object Caching Actually Speeds Up

  • Database-heavy requests: WP_Query results, options, user/session data, transients
  • Admin screens: Posts list, plugin/theme pages, WooCommerce product/admin screens
  • APIs & headless: Repeated lookups during REST requests

Object cache ≠ page cache. Keep your page cache (LiteSpeed Cache, Nginx FastCGI, etc.) for anonymous visitors and add object cache for dynamic/database work.


Redis vs Memcached (Which Should You Choose?)

  • Pros: Persistence options, introspection tools, rich data types, great plugin ecosystem
  • Cons: Slightly more involved to configure optimally (auth, eviction policy)

Memcached

  • Pros: Ultra-simple, fast in-memory cache, easy to scale horizontally
  • Cons: No persistence, fewer introspection options, fewer WP-specific tools

Rule of thumb: If your host supports Redis easily (socket or localhost + password), use Redis. Pick Memcached only if Redis isn’t available or your team standardizes on it.


Prereqs (Server or Hosting Panel)

  • Managed hosting: Toggle Redis/Memcached in your panel. Note socket path or host:port and password.
  • Self-managed VPS: Install the daemon (redis-server or memcached) and ensure it starts on boot. Prefer UNIX socket over TCP when the cache and PHP-FPM run on the same machine.

Install & Enable Redis Object Cache (WordPress)

1) Add the plugin

Use a reputable “Redis Object Cache” plugin (the one that installs the object-cache.php drop-in).

  • Via dashboard: Plugins → Add New → search Redis Object Cache → Install & Activate
  • Via WP-CLI:
wp plugin install redis-cache --activate

2) Tell WordPress how to connect

Add these constants to wp-config.php (adjust for your environment):

// Prefer a UNIX socket if your host provides it:
define('WP_REDIS_SCHEME', 'unix');
define('WP_REDIS_PATH', '/var/run/redis/redis.sock');

// Or use TCP host/port instead of socket:
// define('WP_REDIS_HOST', '127.0.0.1');
// define('WP_REDIS_PORT', 6379);

// Optional hardening & tuning:
define('WP_REDIS_PASSWORD', 'strong-password-here');   // if enabled
define('WP_REDIS_DATABASE', 0);                        // 0..15
define('WP_REDIS_PREFIX', 'pofii_');                   // unique per site
define('WP_REDIS_TIMEOUT', 1.0);                       // seconds
define('WP_REDIS_READ_TIMEOUT', 1.0);

3) Enable the drop-in

From the plugin screen, click Enable Object Cache, or via WP-CLI:

wp redis enable
wp redis info

You should see a status screen showing Status: Connected, memory usage, and keyspace stats.


Install & Enable Memcached (Alternative)

1) Ensure Memcached is running

  • Managed host: toggle in panel
  • Self-managed: apt/yum install memcached and enable the service

2) Install a Memcached object-cache plugin/drop-in

Use a plugin that provides object-cache.php for Memcached and the PHP extension (php-memcached) enabled.

3) Configure (if needed)

Many plugins auto-detect 127.0.0.1:11211. Otherwise, define host/port constants per plugin docs.


Validate It’s Actually Working

WordPress-level checks

  • WP-CLI: wp redis info wp cache get healthcheck group
  • Plugin dashboard: look for Cache hits rising after a few requests
  • Query Monitor plugin: watch database query count and time drop on repeated loads

Server-level checks (Redis)

# Is Redis alive?
redis-cli ping

# Live stats
redis-cli info stats | grep hits
redis-cli info memory

Expect first request = miss, subsequent requests = hit, especially on admin pages you revisit.


Smart Configuration Tips (Real-World)

1) Eviction policy (Redis)

For shared servers, set a sensible policy so the cache stays healthy under pressure:

  • maxmemory set to a safe fraction of available RAM
  • maxmemory-policy allkeys-lru (or volatile-lru if you set expirations carefully)

Your host may manage this globally; if not, add it in redis.conf.

2) Use a unique prefix per site

Multisite or multiple installs on the same Redis? Set a unique WP_REDIS_PREFIX so keys don’t collide.

3) WooCommerce specifics

  • Cart fragments & sessions: Object cache generally helps, but validate checkout/cart behavior.
  • Exclude super-chatty transients if your plugin allows it, or lower TTLs for them.
  • After enabling object cache, clear Woo caches and test checkout end-to-end.

4) Page cache still matters

Object cache accelerates queries; it doesn’t replace page cache/CDN for anonymous traffic. Keep both. Learn here how to setup Cloudflare CDN.

5) Security & access

  • Bind Redis to localhost or a socket. If remote, require a password and allowlist IPs.
  • Don’t expose Redis on the public internet.

6) Purge logic

When you clear your page cache, also flush the object cache after major plugin/theme updates or big imports:

wp cache flush
# or
wp redis flush

Troubleshooting (Fast Fixes)

  • “Object cache drop-in not found” → Re-enable in plugin; confirm wp-content/object-cache.php exists.
  • Connected but no hits → Requests might be fully served from page cache or CDN. Test admin pages or logged-in views.
  • Random logouts / odd sessions → Ensure your session/auth plugins play nicely; clear caches and retest.
  • High memory usage → Lower Redis maxmemory, choose LRU eviction, verify no plugin is stuffing huge objects.
  • Slow first byte persists → Bottleneck may be PHP, external HTTP calls, or slow queries that don’t repeat. Profile with Query Monitor, New Relic, or Xdebug profiler.

Minimal Setup Checklist (Copy/Paste)

  • Enable Redis (or Memcached) on server/hosting panel
  • Install Redis Object Cache (or Memcached) plugin
  • Configure wp-config.php (socket or host/port, prefix, password)
  • wp redis enable and verify Connected
  • Test admin + dynamic pages twice (miss → hit)
  • Keep page cache/CDN enabled; set purge + flush rules
  • Monitor memory/evictions; adjust maxmemory/policy if needed

FAQ

Do I still need a page cache if I have Redis?
Yes. Page cache serves full HTML to anonymous users; object cache speeds up database work. They solve different problems and stack together.

Will Redis help WooCommerce?
Usually, yes—especially for admin/product screens and repeated queries. Always test carts and checkout after enabling.

Socket or TCP for Redis?
If PHP and Redis are on the same server, use a UNIX socket (lower overhead). Use TCP with strong auth when remote.

How do I know it’s worth it?
Compare TTFB and DB query time before/after on the same page (logged-in). If query time drops and repeat loads are faster, it’s working.


5 min read
Share this post:

0 comments

Leave a Comment

Please, enter your comment.
Please, enter your name.
Please, provide a valid email address.
Enjoy this post? Join our newsletter
Don’t forget to share it

Related Articles

All posts