Redis Object Caching Configuration Guide

Enhanced Performance for WordPress & WooCommerce Sites

Introduction to Redis Object Caching

Redis is a powerful in-memory data store that can be utilized for object caching to significantly enhance the performance of your WordPress and WooCommerce sites. By storing frequently accessed data in memory, Redis reduces database queries and dramatically improves response times.

Key Benefits:
  • Dramatically reduced database load
  • Faster page load times
  • Improved server scalability
  • Better handling of high traffic volumes
  • Enhanced user experience

Redis Object Caching Workflow

WordPress Request
Check Redis Cache
Return Cached Data
If not cached ↓
Query Database
Store in Redis
Return Data

Prerequisites

Before You Begin:
  • Root or sudo access to your Ubuntu server
  • PHP 8.3 (or your installed version) with FPM
  • WordPress installation with proper file permissions
  • Basic command-line knowledge
  • Backup of your current configuration

Step 1: Installing Redis Server

1Update Package List

First, update your system's package list to ensure you're installing the latest version:

sudo apt update

2Upgrade Existing Packages (Optional)

If there are packages available for upgrade, you can update them:

sudo apt upgrade

3Install Redis Server and PHP Extension

Install both Redis server and the PHP Redis extension in a single command:

sudo apt install redis-server php8.3-redis
Note: Replace php8.3 with your installed PHP version (e.g., php8.1, php8.2). The PHP extension allows PHP to interface with Redis server.

4Verify Redis Installation

Check the status of the Redis server service to ensure it's running:

sudo systemctl status redis-server
Expected Output: You should see "active (running)" status with no errors or warnings. The service should be enabled and running without issues.

Step 2: Troubleshooting Redis Installation

5Check Redis Log Files

Examine the Redis log file for any errors or warnings:

sudo cat /var/log/redis/redis-server.log

6Fix Memory Overcommit Warning

If you see a warning about "overcommit_memory is set to 0", you need to fix this issue:

Warning Message:
"WARNING overcommit_memory is set to 0! Background save may fail under low memory condition."

Navigate to the sysctl configuration directory:

cd /etc/sysctl.d/

Create a new configuration file for Redis:

sudo nano 11-redis.conf

Add the following directive to the file:

vm.overcommit_memory = 1
Explanation: This setting allows the kernel to overcommit memory, which is necessary for Redis background saves to function properly under low memory conditions.

7Reboot the Server

To apply the new configuration, reboot your server:

sudo reboot
Important: Wait a few minutes for your server to fully reboot before proceeding.

8Verify the Fix

After rebooting, check the Redis service status again:

sudo systemctl status redis-server

And check the log file to confirm the warning is resolved:

sudo cat /var/log/redis/redis-server.log
Success Indicator: You should see "Ready to accept connections" without the memory overcommit warning. Any IPv6 port binding warnings can be safely ignored.

Step 3: Configuring Redis Server

9Locate Redis Configuration

The main Redis configuration file is located in the /etc/redis/ directory:

ls -l /etc/redis/

10Create Backup

Before making changes, create a backup of the original configuration file:

sudo cp /etc/redis/redis.conf /etc/redis/redis.conf.back

11Edit Redis Configuration

Open the Redis configuration file:

sudo nano /etc/redis/redis.conf

12Configure Maximum Memory

Search for the maxmemory directive (press Ctrl+W in nano and type "maxmemory"):

Find this line (commented):
# maxmemory <bytes>
Change it to:
maxmemory 256mb

13Configure Cache Eviction Policy

Below the maxmemory setting, find and configure the eviction policy:

Find this line (commented):
# maxmemory-policy noeviction
Change it to:
maxmemory-policy allkeys-lru
Explanation: The allkeys-lru policy removes the least recently used (LRU) keys when the maximum memory limit is reached, ensuring optimal cache performance.
Eviction Policy Description Use Case
allkeys-lru Removes least recently used keys Recommended for object caching
allkeys-lfu Removes least frequently used keys Alternative caching strategy
volatile-lru Removes LRU keys with expiration set Mixed cache types
noeviction Returns errors when memory limit reached Default (not recommended for caching)

14Restart Redis Service

Apply the new configuration by restarting Redis:

sudo systemctl restart redis-server
Configuration Complete: Redis is now installed and configured on your server. It's ready to be used by any WordPress site on your server.

Step 4: WordPress Configuration

Important: Redis doesn't automatically work with WordPress sites. Each site that wants to use Redis must be individually configured.

15Navigate to Site Directory

Change to your WordPress site's document root:

cd /var/www/example.com/public_html/

16Edit wp-config.php

Open the WordPress configuration file:

sudo nano wp-config.php

17Add Cache Key Salt

Scroll down to the WordPress salt section and add the following directive:

define('WP_CACHE_KEY_SALT', 'example.com');
Critical: Replace example.com with your actual domain name. This prevents cache conflicts when multiple sites on the same server use Redis.

Example Configuration Location

/* Authentication Unique Keys and Salts */
define('AUTH_KEY',         'put your unique phrase here');
define('SECURE_AUTH_KEY',  'put your unique phrase here');
define('LOGGED_IN_KEY',    'put your unique phrase here');
define('NONCE_KEY',        'put your unique phrase here');
define('AUTH_SALT',        'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT',   'put your unique phrase here');
define('NONCE_SALT',       'put your unique phrase here');

/* Redis Object Cache Configuration */
define('WP_CACHE_KEY_SALT', 'example.com');

18Save and Reload PHP-FPM

After saving the wp-config.php file, reload the PHP-FPM service:

sudo systemctl reload php8.3-fpm

Step 5: WooCommerce Considerations

Important for WooCommerce Users: WooCommerce session data should not be cached in Redis as it can cause cart and checkout issues.

19Prevent Redis Caching of WooCommerce Sessions

Add the following directive to your wp-config.php file:

define('WP_REDIS_IGNORED_GROUPS', 'wc_session');

Complete WooCommerce Configuration Example

/* Redis Object Cache Configuration */
define('WP_CACHE_KEY_SALT', 'example.com');

/* Prevent Redis Caching WooCommerce Session Data */
define('WP_REDIS_IGNORED_GROUPS', 'wc_session');

WooCommerce Pages to Exclude from Caching

Page Reason
/cart/ Dynamic cart contents
/my-account/ User-specific data
/checkout/ Payment processing

WooCommerce Cookies to Exclude

  • woocommerce_cart_hash
  • woocommerce_items_in_cart
  • wp_woocommerce_session_*
  • woocommerce_recently_viewed
  • store_notice[notice_id]
  • _wc_session_*

Step 6: Installing Redis WordPress Plugin

Plugin Requirements: After configuring wp-config.php, you must install a Redis plugin to enable object caching.

Recommended Plugins Based on Caching Strategy

Caching Method Recommended Plugin Notes
FastCGI Caching Redis Object Cache Separate plugin required
WP Super Cache Redis Object Cache Separate plugin required
W3 Total Cache Built-in Support No additional plugin needed

Installing Redis Object Cache Plugin

  1. Log in to your WordPress admin dashboard
  2. Navigate to Plugins → Add New
  3. Search for "Redis Object Cache"
  4. Install and activate the plugin by Till Krüss
  5. Go to Settings → Redis
  6. Click "Enable Object Cache"
Verification: The plugin dashboard should show "Connected" status with cache statistics and hit/miss ratios.

Configuring W3 Total Cache for Redis

If using W3 Total Cache, Redis support is built-in:

  1. Go to Performance → General Settings
  2. Enable "Object Cache" and select "Redis" as the method
  3. Configure Redis server settings (usually localhost:6379)
  4. Save settings and clear all caches

Step 7: Testing and Verification

20Verify Redis Connection

Check if Redis is accepting connections:

redis-cli ping
Expected Response: PONG

21Monitor Redis Statistics

View Redis statistics and information:

redis-cli info stats

22Check Cache Keys

View all cached keys (use cautiously on production):

redis-cli keys '*'

23Monitor Memory Usage

Check Redis memory consumption:

redis-cli info memory

Performance Testing

Before and After Comparison:
  • Use tools like GTmetrix or Pingdom to measure page load times
  • Monitor database query counts (before: 50-100+ queries, after: 10-20 queries typical)
  • Check server response times in browser developer tools
  • Review Redis plugin dashboard for hit/miss ratios (aim for 80%+ hit ratio)

Step 8: Maintenance and Monitoring

Flushing Redis Cache

To clear all cached data from Redis:

redis-cli flushall
Warning: This command clears the entire Redis cache for all sites using this Redis instance.

Monitoring Redis Performance

View real-time Redis commands:

redis-cli monitor

Check slow queries:

redis-cli slowlog get 10

Regular Maintenance Tasks

Task Frequency Command/Action
Check Redis Status Weekly sudo systemctl status redis-server
Review Log Files Weekly sudo cat /var/log/redis/redis-server.log
Monitor Memory Usage Daily redis-cli info memory
Check Hit/Miss Ratio Daily WordPress plugin dashboard
Review Cache Keys Monthly redis-cli dbsize

Troubleshooting Common Issues

Issue 1: Redis Not Starting

Symptoms: Redis service fails to start or immediately stops.

Solutions:

  1. Check configuration file syntax: redis-cli --test-memory 256
  2. Review log files for errors: sudo cat /var/log/redis/redis-server.log
  3. Verify permissions on Redis directories: ls -la /var/lib/redis
  4. Check if port 6379 is already in use: sudo netstat -tulpn | grep 6379

Issue 2: WordPress Not Connecting to Redis

Symptoms: Plugin shows "Not Connected" status.

Solutions:

  1. Verify WP_CACHE_KEY_SALT is correctly set in wp-config.php
  2. Check PHP Redis extension is installed: php -m | grep redis
  3. Ensure Redis is running: sudo systemctl status redis-server
  4. Test Redis connection: redis-cli ping
  5. Reload PHP-FPM: sudo systemctl reload php8.3-fpm

Issue 3: Poor Cache Performance

Symptoms: Low hit ratio or minimal performance improvement.

Solutions:

  1. Increase maxmemory setting if memory usage is near limit
  2. Review and optimize excluded cache groups
  3. Check for conflicting caching plugins
  4. Verify cache key salt is unique for each site
  5. Monitor for cache evictions: redis-cli info stats | grep evicted

Issue 4: High Memory Usage

Symptoms: Redis consuming excessive memory.

Solutions:

  1. Check current memory usage: redis-cli info memory
  2. Review maxmemory setting in redis.conf
  3. Verify eviction policy is set to allkeys-lru
  4. Manually flush old data if needed: redis-cli flushdb
  5. Consider increasing server RAM or adjusting maxmemory

Best Practices and Recommendations

Performance Optimization Tips

  • Memory Allocation: Set maxmemory to 25-50% of available RAM for dedicated WordPress servers
  • Eviction Policy: Use allkeys-lru for optimal cache management
  • Cache Key Salt: Always use unique salt for each site to prevent cache conflicts
  • Monitoring: Regularly check hit/miss ratios and adjust cache settings accordingly
  • WooCommerce: Always exclude session data from Redis caching
  • Persistent Caching: Enable Redis persistence if you need cache survival across reboots
  • Security: Bind Redis to localhost unless you need external access
  • Updates: Keep Redis, PHP extension, and plugins up to date

Multi-Site Configuration

When running multiple WordPress sites on the same server with Redis:

  1. Each site must have a unique WP_CACHE_KEY_SALT value
  2. All sites can share the same Redis instance
  3. Monitor total memory usage across all sites
  4. Consider using multiple Redis instances for high-traffic sites
  5. Implement proper cache key prefixing to avoid conflicts

Security Considerations

  • Redis runs on localhost by default - keep it that way unless necessary
  • If exposing Redis externally, use strong passwords and firewall rules
  • Regularly update Redis to patch security vulnerabilities
  • Limit Redis user permissions and file access
  • Monitor Redis logs for suspicious activity

Quick Reference Summary

Complete Installation Process

1. Installation

sudo apt update
sudo apt install redis-server php8.3-redis

2. Fix Memory Warning

sudo nano /etc/sysctl.d/11-redis.conf
# Add: vm.overcommit_memory = 1
sudo reboot

3. Configure Redis

sudo nano /etc/redis/redis.conf
# Set: maxmemory 256mb
# Set: maxmemory-policy allkeys-lru
sudo systemctl restart redis-server

4. Configure WordPress

sudo nano /var/www/example.com/public_html/wp-config.php
# Add: define('WP_CACHE_KEY_SALT', 'example.com');
# Add: define('WP_REDIS_IGNORED_GROUPS', 'wc_session');
sudo systemctl reload php8.3-fpm

5. Install Plugin

Install "Redis Object Cache" plugin via WordPress admin and enable object cache.

Essential Commands Quick Reference

Purpose Command
Check Redis Status sudo systemctl status redis-server
Restart Redis sudo systemctl restart redis-server
View Redis Logs sudo cat /var/log/redis/redis-server.log
Test Connection redis-cli ping
Check Memory Usage redis-cli info memory
View Statistics redis-cli info stats
Flush All Cache redis-cli flushall
Count Keys redis-cli dbsize
Monitor Activity redis-cli monitor
Reload PHP-FPM sudo systemctl reload php8.3-fpm

Conclusion

Redis object caching is a powerful tool for enhancing WordPress and WooCommerce performance. By following this comprehensive guide, you have successfully:

  • Installed and configured Redis server on your Ubuntu system
  • Resolved common installation issues and warnings
  • Optimized Redis for WordPress object caching
  • Configured WordPress to utilize Redis caching
  • Implemented WooCommerce-specific considerations
  • Set up monitoring and maintenance procedures
Expected Results:
  • 50-80% reduction in database queries
  • Faster page load times (typically 2-5x improvement)
  • Improved server scalability and resource utilization
  • Better user experience with reduced loading times
  • Enhanced site performance under high traffic loads
Remember: Cache performance varies based on your specific site configuration, content types, traffic patterns, and server resources. Regular monitoring and optimization are essential for maintaining optimal performance.