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.
- Dramatically reduced database load
- Faster page load times
- Improved server scalability
- Better handling of high traffic volumes
- Enhanced user experience
Redis Object Caching Workflow
Prerequisites
- 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
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
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 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
7Reboot the Server
To apply the new configuration, reboot your server:
sudo reboot
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
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"):
# maxmemory <bytes>
maxmemory 256mb
13Configure Cache Eviction Policy
Below the maxmemory setting, find and configure the eviction policy:
# maxmemory-policy noeviction
maxmemory-policy allkeys-lru
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
Step 4: WordPress Configuration
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');
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
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_hashwoocommerce_items_in_cartwp_woocommerce_session_*woocommerce_recently_viewedstore_notice[notice_id]_wc_session_*
Step 6: Installing Redis WordPress Plugin
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
- Log in to your WordPress admin dashboard
- Navigate to Plugins → Add New
- Search for "Redis Object Cache"
- Install and activate the plugin by Till Krüss
- Go to Settings → Redis
- Click "Enable Object Cache"
Configuring W3 Total Cache for Redis
If using W3 Total Cache, Redis support is built-in:
- Go to Performance → General Settings
- Enable "Object Cache" and select "Redis" as the method
- Configure Redis server settings (usually localhost:6379)
- Save settings and clear all caches
Step 7: Testing and Verification
20Verify Redis Connection
Check if Redis is accepting connections:
redis-cli ping
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
- 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
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
Solutions:
- Check configuration file syntax:
redis-cli --test-memory 256 - Review log files for errors:
sudo cat /var/log/redis/redis-server.log - Verify permissions on Redis directories:
ls -la /var/lib/redis - Check if port 6379 is already in use:
sudo netstat -tulpn | grep 6379
Issue 2: WordPress Not Connecting to Redis
Solutions:
- Verify WP_CACHE_KEY_SALT is correctly set in wp-config.php
- Check PHP Redis extension is installed:
php -m | grep redis - Ensure Redis is running:
sudo systemctl status redis-server - Test Redis connection:
redis-cli ping - Reload PHP-FPM:
sudo systemctl reload php8.3-fpm
Issue 3: Poor Cache Performance
Solutions:
- Increase maxmemory setting if memory usage is near limit
- Review and optimize excluded cache groups
- Check for conflicting caching plugins
- Verify cache key salt is unique for each site
- Monitor for cache evictions:
redis-cli info stats | grep evicted
Issue 4: High Memory Usage
Solutions:
- Check current memory usage:
redis-cli info memory - Review maxmemory setting in redis.conf
- Verify eviction policy is set to allkeys-lru
- Manually flush old data if needed:
redis-cli flushdb - 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:
- Each site must have a unique WP_CACHE_KEY_SALT value
- All sites can share the same Redis instance
- Monitor total memory usage across all sites
- Consider using multiple Redis instances for high-traffic sites
- 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
- 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