What is Redis?
Redis is an in-memory data store used primarily as a cache. Instead of querying the database for every request, Redis stores frequently accessed data in RAM — which is orders of magnitude faster.
Speed Comparison
| Operation | MySQL | Redis |
|---|---|---|
| Simple query | 1-10ms | 0.1ms |
| Complex query | 50-500ms | 0.1ms |
| Session lookup | 5-20ms | 0.1ms |
Common Use Cases
1. Database Query Cache
Cache expensive database results:
Request → Check Redis → Hit? Return cached → Miss? Query DB → Store in Redis → Return2. Session Storage
Store user sessions in Redis instead of files/database:
- Faster session reads
- Shared across multiple servers
- Automatic expiration
3. Full-Page Cache
Cache entire rendered pages:
- Bypass PHP/application entirely
- Sub-millisecond response times
- Ideal for content that doesn't change often
4. Rate Limiting
Track request counts efficiently:
Key: rate:192.168.1.1
Value: 47 (request count)
TTL: 60 secondsRedis with WordPress
Object Cache
WordPress makes hundreds of database queries per page. Redis object cache reduces this dramatically.
Setup:
- Install Redis on your server
- Install the "Redis Object Cache" plugin
- Add to
wp-config.php:
define('WP_REDIS_HOST', '127.0.0.1');
define('WP_REDIS_PORT', 6379);Expected Results
- Database queries: 200+ → 5-10 per page
- Page generation: 500ms → 50ms
- Server load: 80% reduction
Redis Commands Cheat Sheet
# Connect
redis-cli
# Set/Get values
SET mykey "Hello"
GET mykey
# Set with expiration (60 seconds)
SETEX mykey 60 "Hello"
# Check TTL
TTL mykey
# Delete key
DEL mykey
# List all keys (use with caution in production)
KEYS pattern*
# Server info
INFO memory
INFO stats
# Flush all data
FLUSHALLBest Practices
- Set TTL on all keys — prevent memory overflow
- Use key prefixes —
wp:cache:,session:,rate: - Monitor memory — set maxmemory policy
- Use Redis Sentinel for high availability
- Secure with password and bind to localhost
Conclusion
Redis is the single most impactful performance upgrade for dynamic websites. If your site makes database queries on every request, Redis caching can reduce response times by 90%+ with minimal setup effort.