Contents
Using Redis for Object Caching in WordPress
Object caching is one of the most effective ways to improve WordPress performance at scale. By persisting expensive database query results in memory, you reduce page load times, database load, and overall server resource consumption. Redis, an open-source, in-memory data store, is a popular and highly reliable object cache backend.
What Is Redis
- In-Memory Key-Value Store: Redis stores data in RAM for sub-millisecond read/write operations.
- Data Structures: Supports strings, lists, hashes, sets, sorted sets, bitmaps, hyperloglogs, and streams.
- Persistence: Offers snapshotting (RDB) and append-only file (AOF) persistence.
- High Availability: Built-in replication, automatic failover (Redis Sentinel), and clustering.
Official site: redis.io
Why Object Caching
WordPress relies heavily on MySQL. Common operations such as retrieving posts, options, and user meta generate repetitive queries. Object caching:
- Reduces database round trips.
- Speeds up
wp_options
,wp_posts
,wp_users
queries. - Improves page generation time.
- Lowers CPU and I/O on your database server.
WordPress Object Cache API
WordPress provides the WP_Object_Cache
class and functions like wp_cache_get()
, wp_cache_set()
, wp_cache_delete()
. By default, this is a non-persistent cache (per-request). A persistent backend like Redis requires a drop-in plugin.
Setting Up Redis
1. Install Redis Server
- On Ubuntu/Debian:
sudo apt update sudo apt install redis-server
- On CentOS/RHEL (with EPEL):
sudo yum install epel-release sudo yum install redis
Verify:redis-cli ping
→ PONG
2. Configure Persistence Security
- Edit
/etc/redis/redis.conf
: - Set
requirepass your-strong-password
- Enable or tune
save 900 1
,appendonly yes
- Bind to localhost or your private network:
bind 127.0.0.1
Restart service:sudo systemctl restart redis-server
Integrating with WordPress
1. Install Redis Object Cache Plugin
Popular choices:
- redis-cache by Till Krüss
- WP Redis
Install via WP Admin or WP-CLI:wp plugin install redis-cache --activate
2. Configure wp-config.php
define(WP_REDIS_HOST, 127.0.0.1)
define(WP_REDIS_PORT, 6379)
define(WP_REDIS_PASSWORD, your-strong-password)
define(WP_REDIS_DATABASE, 0) // optional
3. Enable and Verify
- In WP Admin → Tools → Redis:
- Click Enable Object Cache
- Verify
PING: PONG
, hit/miss stats. - Or via WP-CLI:
wp redis enable
wp redis info
Performance Monitoring
Redis CLI
INFO
– overview (memory, uptime, hits, misses).MONITOR
– real-time command stream.CLIENT LIST
– connected clients.
Metrics to Track
Metric | Description |
---|---|
used_memory |
RAM consumed by Redis. |
keyspace_hits/misses |
Cache effectiveness ratio. |
evicted_keys |
Keys purged due to memory limits. |
Advanced Usage Best Practices
1. Namespacing Multiple Sites
- Set a unique
WP_CACHE_KEY_SALT
per site inwp-config.php
. - For multisite, use database indices or separate salt per blog.
2. TTL (Time-to-Live)
By default, object caches persist until eviction. You can set TTL per-group:
wp_cache_set(my_key, data, group, 3600)
3. Cache Invalidation
- Use
wp_cache_delete()
when updating posts/programmatically modifying data. - Ensure plugins/themes properly clear cache on CRUD operations.
4. Memory Management
- Configure
maxmemory
andmaxmemory-policy
(allkeys-lru
recommended). - Monitor
evicted_keys
to avoid unexpected cache evictions.
Security Considerations
- Require a strong Redis password (
requirepass
). - Bind Redis to
127.0.0.1
or private network. Avoid public exposure. - Use firewall rules (e.g., UFW, iptables) to restrict access.
Comparing Redis to Other Caching Solutions
Feature | Redis | Memcached | APCu (Local) |
---|---|---|---|
Data Structures | Rich (hash, list, set…) | Strings, slab allocation | PHP variables only |
Persistence | Yes (RDB/AOF) | No | No |
Clustering | Yes | No | N/A |
Troubleshooting
- “PONG” not returned Check service status, host/port, password.
- High
misses
rate Review TTLs, ensure frequent keys are set correctly. - Evicted keys Increase
maxmemory
or tune eviction policy. - Plugin errors Enable WP_DEBUG and review
error_log
.
Conclusion
Redis object caching can dramatically reduce WordPress page load times and database load. With its rich feature set—persistence, clustering, advanced data structures—Redis outperforms many alternatives. Following best practices for installation, configuration, and security will ensure a rock-solid caching layer that scales as your traffic grows.
Further reading:
|
Acepto donaciones de BAT's mediante el navegador Brave 🙂 |