Redis Object Cache for WordPress and Magento 2: Setup and Best Practices

Redis can make WordPress, WooCommerce and Magento noticeably faster, especially for pages that cannot be page-cached. Here is how to set it up correctly and avoid common mistakes.

Page caching makes anonymous visits fast. But many requests cannot be served from the page cache: logged-in users, cart, checkout, admin and search. For those, PHP and the database do the work on every request.

Redis is an in-memory data store that speeds up exactly these requests by caching database results and storing sessions in memory. This guide shows how to set it up for WordPress/WooCommerce and Magento 2.

What Redis does in a web shop

Use What it stores Benefit
Object cache (WordPress) Results of database queries and computed data Fewer database queries per request
Default cache (Magento) Configuration, layout, blocks, collections Faster page generation
Page cache (Magento, if no Varnish) Full pages Faster anonymous pages
Sessions User sessions Faster, works across multiple web servers

Install Redis

On Ubuntu/Debian:

sudo apt install redis-server
sudo systemctl enable --now redis-server
redis-cli ping

You should see PONG. Install the PHP extension as well (for example php8.3-redis) and restart PHP-FPM.

Basic Redis configuration

In /etc/redis/redis.conf:

bind 127.0.0.1 ::1
protected-mode yes
maxmemory 512mb
maxmemory-policy allkeys-lru
  • bind to localhost (or a private network). Never expose Redis to the internet
  • maxmemory limits memory use; size it based on your data
  • allkeys-lru removes the least recently used keys when memory is full, which is right for a cache

For sessions, eviction means users get logged out or lose their cart. Use a separate Redis instance (or at least separate memory planning) for sessions with a policy that does not evict active sessions unexpectedly, such as noeviction with enough memory, or volatile-lru if sessions have expiry times.

WordPress and WooCommerce

1. Install an object cache plugin

The popular Redis Object Cache plugin adds a drop-in (wp-content/object-cache.php) that sends WordPress cache calls to Redis. Some hosts provide their own integration.

2. Configure wp-config.php

define( 'WP_REDIS_HOST', '127.0.0.1' );
define( 'WP_REDIS_PORT', 6379 );
define( 'WP_REDIS_DATABASE', 0 );
define( 'WP_REDIS_PREFIX', 'shop_' );

Use a unique prefix per site if several sites share one Redis server.

3. Enable it

In the plugin settings, or with WP-CLI:

wp redis enable
wp redis status

4. Check the effect

Use the Query Monitor plugin to compare database queries per request before and after. Cart, checkout, account and admin pages should show the biggest improvement.

Magento 2

Magento supports Redis for the default cache, page cache and sessions, configured with bin/magento setup:config:set.

Default cache

bin/magento setup:config:set --cache-backend=redis --cache-backend-redis-server=127.0.0.1 --cache-backend-redis-port=6379 --cache-backend-redis-db=0

Page cache (only if you do not use Varnish)

bin/magento setup:config:set --page-cache=redis --page-cache-redis-server=127.0.0.1 --page-cache-redis-port=6379 --page-cache-redis-db=1

In production, Varnish is the recommended full page cache (see the Varnish for Magento 2 guide).

Sessions

bin/magento setup:config:set --session-save=redis --session-save-redis-host=127.0.0.1 --session-save-redis-port=6380 --session-save-redis-db=0

The example uses a second Redis instance on port 6380 for sessions. This way, flushing the cache never logs customers out.

After changing configuration:

bin/magento cache:flush

More commands in the Magento 2 commands cheat sheet.

Security

  • Bind to localhost or a private network only, and firewall port 6379
  • Set a password (requirepass) or ACL users if Redis is reachable from other machines
  • Keep Redis updated
  • Do not run Redis as root

Monitoring and troubleshooting

redis-cli info memory
redis-cli info stats | grep -E "keyspace_hits|keyspace_misses|evicted_keys"
redis-cli --latency
  • High evictions on the cache instance: increase maxmemory
  • Evictions on the session instance: customers losing carts. Increase memory or fix the policy
  • Low hit rate: cache flushed too often, or keys not reused
  • Connection errors: check that PHP’s Redis extension is installed for the right PHP version
  • Stale data after deploy: flush the cache (not the session store) as part of deployment

Frequently asked questions

Does Redis replace page caching?

No. Page caching (Varnish, CDN, plugin) serves complete pages to anonymous visitors. Redis speeds up the pages that cannot be page-cached. Use both.

Redis or Memcached?

Both work as object caches. Redis offers persistence options and more data types, and is the common choice for Magento and many WordPress hosts.

How much memory does Redis need?

It depends on catalog size and traffic. Start with a few hundred megabytes for a medium store, monitor memory and evictions, and adjust.

Want your store’s caching set up properly?

I configure Redis, Varnish and page caching for WordPress, WooCommerce and Magento stores. See my servers and backups service or request a performance review.