Performance7 min readPublished: 19 Dec 2024Updated: 9 Nov 2026

WordPress Object Caching with Redis and Memcached

What object caching does for WordPress, when it helps and when it will not, how to choose between Redis and Memcached, and how to prove it is working.

G7Cloud Engineering
Platform team
Share:
Abstract illustration representing wordpress object caching with redis and memcached.
  • Object caching keeps database query results in memory and reuses them across requests, not only within one.
  • It helps most where page caching cannot: logged-in users, carts, checkouts and the admin area.
  • Redis is the right default. Memcached works but has fewer WordPress features and loses everything on a restart.
  • Without a persistent object cache, WordPress rebuilds the same query results on every request.
  • Set maxmemory-policy allkeys-lru on Redis, or a full cache starts refusing writes instead of dropping old keys.

What does object caching do for WordPress?

It keeps the results of database queries in memory so the next request can reuse them. WordPress already has an object cache, but by default it only lasts for a single request. A persistent backend such as Redis keeps it between requests, so a query that ran once can answer many visitors.

Default WordPressWith Redis
Cache lifetimeOne requestUntil the data changes or expires
Same query on the next requestRuns again against the databaseRead from memory
Helps logged-in usersNoYes
Helps the admin areaNoYes
Helps WooCommerce checkoutNoYes

The last three rows are the point: a full-page cache cannot help with any of those pages.

It does not replace page caching

Page caching stores finished pages for visitors who are not logged in, and is the bigger win on a content site. Object caching stores query results and helps everywhere, including pages that can never be cached whole. Busy sites want both. See setting up an Nginx reverse proxy cache.

When will object caching actually help?

The gain depends on how much of your traffic cannot be page cached, and how many queries your pages run. A small brochure site behind a page cache will barely notice. A shop where every visitor has a session will notice a lot.

Site typeExpected benefitWhy
Brochure site, mostly anonymousSmallPage caching already answers nearly every request
Busy blog with commentsModerateLogged-in commenters bypass the page cache
WooCommerce shopLargeCarts and checkout are never page cached
Membership or LMS siteLargeAlmost every visitor is logged in
Site with a very large options tableLargeAutoloaded options are read on every request
Multisite networkLargeSite and network options are queried constantly

If most of your traffic is logged out and already cached, spend the effort elsewhere first.

Is your site a good candidate?
cd /var/www/example.com

# Queries per page load: over 100 suggests real headroom
wp eval 'global $wpdb; get_posts(); echo $wpdb->num_queries . " queries" . PHP_EOL;'

# Autoloaded options: over 1 MB and object caching will help a lot
wp db query "SELECT ROUND(SUM(LENGTH(option_value))/1024/1024, 2) AS mb
  FROM wp_options WHERE autoload='yes'"

# Is a persistent object cache already active?
wp cache type
A large autoloaded options figure (settings WordPress reads on every request) is the clearest sign.

Redis or Memcached: which should I use?

Use Redis, unless your setup already standardises on Memcached. Both are fast enough that speed does not decide it. Redis has better tooling, more data types, optional persistence (saving to disk) and a better-maintained WordPress plugin.

RedisMemcached
Speed for WordPressFastFast: no meaningful difference
Survives a restartYes, optionallyNo, cache is lost
Data typesStrings, hashes, lists, setsStrings only
Grouped cache invalidationYesLimited
WordPress pluginRedis Object Cache, actively maintainedOlder, less active
Also usable forSessions, queues, rate limitingCaching only

Restarts matter more than they sound: a Memcached restart empties the cache and sends every request to the database at once.

Install and enable Redis
# 1. Redis server and the PHP extension
sudo apt install -y redis-server php8.3-redis
sudo systemctl enable --now redis-server

# 2. Confirm PHP can see it
php -m | grep redis
redis-cli ping        # expect PONG

# 3. Enable it in WordPress
cd /var/www/example.com
wp plugin install redis-cache --activate
wp redis enable
wp redis status
The plugin writes an object-cache.php drop-in file to wp-content. If wp redis status says Connected, it is live.
Redis configuration that matters
# /etc/redis/redis.conf

# Localhost only. Redis has no authentication by default.
bind 127.0.0.1 ::1
protected-mode yes

# Cap the memory Redis may use
maxmemory 512mb

# Evict the least recently used keys when full. This line is essential.
maxmemory-policy allkeys-lru

# A cache does not need disk persistence; skip the write overhead
save ""
appendonly no

sudo systemctl restart redis-server
Without maxmemory-policy, Redis defaults to noeviction: once full, it returns errors on writes instead of making room.

Never expose Redis to the internet

Redis has no password by default. Anyone who finds an internet-facing instance can read and write it, and attackers have used that to plant SSH keys on servers. Bind it to localhost, or use a private network and a password.

How do I confirm the object cache is working?

Verification
cd /var/www/example.com

# Should report 'redis', not 'default'
wp cache type

# Plugin's own view: connection status, hit ratio, keys stored
wp redis status

# Live Redis statistics
redis-cli info stats | grep -E 'keyspace_hits|keyspace_misses'
redis-cli info memory | grep -E 'used_memory_human|maxmemory_human'
redis-cli dbsize

# Watch commands in real time (development only, it is noisy)
redis-cli monitor
The hit ratio is keyspace_hits divided by hits plus misses. Above about 80 percent on a warm cache means it is working.
Measure the difference
# Queries per request, with the cache on and off
wp eval 'global $wpdb; get_posts(["posts_per_page"=>20]); echo $wpdb->num_queries . PHP_EOL;'

wp redis disable
# run it again, note the number
wp redis enable

# Time to first byte on a page that bypasses the page cache
for i in $(seq 1 10); do
  curl -s -o /dev/null -w '%{time_starttransfer}\n' \
    "https://example.com/my-account/?nocache=$RANDOM"
done | awk '{s+=$1} END {printf "avg %.3fs\n", s/10}'
Test a logged-in or account page. The homepage measures your page cache, not the object cache.
SymptomCauseFix
wp cache type says defaultThe drop-in is missingwp redis enable, and check wp-content is writable
Connection refusedRedis is not running or is on another portsystemctl status redis-server; redis-cli ping
Hit rate stays very lowCache is being flushed constantlyA plugin is calling wp_cache_flush on every request
OOM command not allowedRedis is full with noeviction setSet maxmemory-policy allkeys-lru
Stale content after an editA plugin is not invalidating its own keysFlush with wp cache flush, then report it

Row four is the one that takes a site down, and a one-line configuration change prevents it.

How do I use object caching safely on WooCommerce?

WooCommerce gains more than most sites, because carts, accounts and checkout can never be page cached. It also has the most to lose from a caching mistake, so follow these rules exactly.

  • Never page cache the cart, checkout or account pages. Object caching is safe there; full-page caching is how customers end up seeing each other's baskets.
  • Give Redis enough memory for your catalogue, or a large shop keeps dropping useful keys and the hit rate stays low.
  • Test stock levels under load after you enable it. Stale stock is the failure that costs money, and it only shows with many shoppers at once.
  • Flush the cache when you deploy, so old cached values cannot outlive the code that wrote them.
  • Keep watching the hit rate. A plugin added later that flushes the cache on every request quietly undoes the gain.
Flush safely
# Flush the WordPress object cache
wp cache flush

# On a Redis shared with other sites, give each site its own prefix and
# selective flush in wp-config.php, so wp cache flush only clears its keys:
#   define('WP_REDIS_PREFIX', 'example_com:');
#   define('WP_REDIS_SELECTIVE_FLUSH', true);

# Nuclear: empties EVERY database on the instance.
# Never run this on a Redis shared with other sites or services.
redis-cli FLUSHALL
Without a prefix and selective flush, wp cache flush clears the whole Redis database, and FLUSHALL clears every site's cache at once.

Your hosting should provide this layer

On G7Cloud each WordPress site has its own Redis inside its own container, at 127.0.0.1:6379, so a neighbour's traffic cannot push out your keys. We offer Redis rather than Memcached. See our WordPress caching and WooCommerce speed optimisation.

If the database itself is the problem rather than the number of queries, caching only hides it. Safely trimming a large WordPress or WooCommerce database deals with the size problem underneath.

Frequently asked questions

What is object caching in WordPress?

Keeping the results of database queries in memory so later requests reuse them instead of querying again. By default WordPress only does this within a single request. A persistent backend such as Redis keeps the cache between requests, which is where the gain comes from.

Do I need object caching if I already have page caching?

If nearly every visitor is anonymous and served from the page cache, the extra gain is small. If visitors log in, as on a shop or membership site, object caching helps exactly the pages page caching cannot.

Is Redis or Memcached better for WordPress?

Redis for most sites. Speed is similar, but Redis offers optional persistence, more data types, better cache invalidation and a better-maintained WordPress plugin. Choose Memcached only if your setup already standardises on it.

How much memory should I give Redis?

256 MB is enough for a typical content site; 512 MB to 1 GB suits a busy WooCommerce shop with a large catalogue. Watch used_memory_human against maxmemory_human and raise it if keys are often evicted. Always set maxmemory-policy to allkeys-lru.

Is object caching safe for WooCommerce?

Yes, and it helps a lot, because carts and checkout can never be page cached. Keep full-page caching off cart, checkout and account pages, and test stock behaviour under load after enabling it.

Why is my Redis hit rate so low?

Usually something flushes the cache constantly, often a plugin calling wp_cache_flush on every request, or the cache is too small and keys are evicted before they are reused. Check redis-cli info stats for evicted_keys, and raise maxmemory if that number keeps climbing.

Put this into practice on G7Cloud

Every site runs in its own dedicated container behind ScaleShield, with daily backups that are restore-tested every night. Start on the free plan, no card needed.

About G7Cloud Engineering

Platform team

Articles written by the engineers who build and run G7Cloud: UK managed hosting and the AI Website Builder. We write about what we operate every day: containers, backups, databases, and the small-business websites that run on them.

More about G7Cloud →