Lower WordPress TTFB with Redis as a persistent object cache
WordPress caches options, posts, terms and user data in memory, but only for one request. The next request asks MySQL again. A persistent object cache keeps these values in Redis between requests, so a page that needed 80 queries needs 10. It helps where a page cache cannot: logged-in users, the admin, WooCommerce carts and AJAX requests.
Problem: Uncached pages, the admin and the shop for logged-in customers take 600 to 900 ms until the first byte, and Query Monitor shows dozens of repeated queries for options and meta.
Solution: Install Redis on the server, bind it to localhost and limit its memory, install the Redis Object Cache plugin and enable its object-cache.php drop-in with wp redis enable, set a key prefix per site in wp-config.php, and measure TTFB and the hit rate before and after.
Install Redis
sudo apt install redis-server php8.1-redis
# /etc/redis/redis.conf bind 127.0.0.1 ::1 maxmemory 256mb maxmemory-policy allkeys-lru
sudo systemctl restart redis-server php8.1-fpm redis-cli ping # PONG
allkeys-lru evicts the least recently used keys when the memory is full, which is exactly right for a cache. Without maxmemory, Redis grows until the server runs out of RAM. Listening only on localhost keeps it unreachable from the internet; on a shared server, also set requirepass.
Connect WordPress
// wp-config.php, above "That's all, stop editing!" define( 'WP_REDIS_HOST', '127.0.0.1' ); define( 'WP_REDIS_PORT', 6379 ); define( 'WP_REDIS_PREFIX', 'example_com:' ); define( 'WP_REDIS_DATABASE', 0 );
wp plugin install redis-cache --activate wp redis enable wp redis status
wp redis enable copies the drop-in to wp-content/object-cache.php; WordPress loads it automatically, no other constant is needed. WP_CACHE is for something else: it loads advanced-cache.php, the drop-in of page cache plugins. The prefix keeps several sites on one Redis instance apart.
Measure
for i in 1 2 3 4 5; do curl -s -o /dev/null -w '%{time_starttransfer}\n' -b 'wordpress_logged_in_x=1' https://example.com/shop/; done
redis-cli info stats | grep -E 'keyspace_hits|keyspace_misses'
The fake cookie bypasses most page caches, so the numbers show the PHP time. A hit rate (hits divided by hits plus misses) above 90 percent after some traffic is normal. In Query Monitor, the “Object Cache” panel shows the hit rate per request.
When it does not help
If TTFB stays high, the time is not spent on repeated simple queries but on a few slow ones or on PHP itself: a heavy plugin, a remote API call, an autoload option of several megabytes. Redis also does not replace a page cache for anonymous visitors; see Nginx FastCGI cache.
NOTE: With a persistent object cache, transients are stored in Redis, not in wp_options, and they disappear when Redis is restarted or evicts them; code must always handle a missing transient. How to use the cache in your own code: wp_cache_set(), groups and Redis.