Install and Configure Redis Cache on VPS to Speed Up Your Website
Redis is one of the fastest in-memory data stores available. You can use it as a cache to reduce database queries, store sessions, manage queues, or handle pub/sub messaging in any application. Installing Redis on your VPS is straightforward, and you'll see immediate performance improvements.
Install Redis
apt update
apt install redis-server -y
Configure Redis (/etc/redis/redis.conf)
Bind Redis to Localhost Only
bind 127.0.0.1 ::1
Never expose Redis directly to the internet — this is critical for security.
Set maxmemory and Eviction Policy
maxmemory 256mb
maxmemory-policy allkeys-lru
allkeys-lru stands for Least Recently Used — it removes the least recently used keys when memory is full. This is ideal for cache use cases.
Set a Password
requirepass YourStrongPasswordHere
Choose a Persistence Mode
# For pure cache workloads, disable persistence to save disk I/O
save ""
# If you need persistence, use AOF
appendonly yes
Restart and Enable
systemctl restart redis-server
systemctl enable redis-server
Test Redis
redis-cli -a YourStrongPasswordHere ping
# Response: PONG
redis-cli -a YourStrongPasswordHere
127.0.0.1:6379> SET testkey "hello"
127.0.0.1:6379> GET testkey
# Response: "hello"
Using Redis with WordPress
Install the Redis Object Cache plugin, then add these lines to wp-config.php:
define('WP_REDIS_HOST', '127.0.0.1');
define('WP_REDIS_PORT', 6379);
define('WP_REDIS_PASSWORD', 'YourStrongPasswordHere');
define('WP_REDIS_DATABASE', 0);
Using Redis as a PHP Session Store
# Install the PHP Redis extension
apt install php-redis -y
# Edit php.ini
session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379?auth=YourStrongPasswordHere"
Monitor Redis
# View stats
redis-cli -a password INFO stats
# View number of keys and memory usage
redis-cli -a password INFO keyspace
redis-cli -a password INFO memory
# Monitor commands in real-time (for debugging)
redis-cli -a password MONITOR
used_memory_human and maxmemory_human in INFO memory. If your hit rate is below 80%, it suggests your TTL is too short or your cache keys don't match your usage patterns.Summary
Redis takes less than 5 minutes to install, but the performance boost is significant — especially for websites with repeated database queries on every request.
Verify Redis is Working Correctly
# Check service status
systemctl status redis-server
# Test the connection
redis-cli -a YourPassword ping
# Must return PONG
# View cache hit rate
redis-cli -a YourPassword INFO stats | grep -E "keyspace_hits|keyspace_misses"
# hit_rate = hits / (hits + misses) * 100 — should be above 80%
# View memory usage
redis-cli -a YourPassword INFO memory | grep used_memory_human
Common Issues & Troubleshooting
Error: "NOAUTH Authentication required"
You've set a password in redis.conf but forgot to include the auth parameter in your command or application config:
# WordPress: Check that wp-config.php has all WP_REDIS_* constants
grep WP_REDIS /var/www/html/wp-config.php
# Send password with redis-cli
redis-cli -a YourStrongPassword ping
Error: "OOM command not allowed when used memory > 'maxmemory'"
Redis has hit its memory limit, and the eviction policy is not set or is set to noeviction:
# Check the current policy
redis-cli -a password CONFIG GET maxmemory-policy
# Change the policy without restarting
redis-cli -a password CONFIG SET maxmemory-policy allkeys-lru
# Increase maxmemory if you have spare RAM
redis-cli -a password CONFIG SET maxmemory 512mb
Redis Service Won't Start
# Check error logs
journalctl -u redis-server --no-pager -n 30
cat /var/log/redis/redis-server.log
# Common issue: /run/redis has incorrect permissions
mkdir -p /run/redis
chown redis:redis /run/redis
systemctl start redis-server
WordPress Not Using Redis Cache Despite Successful Connection
The object-cache.php drop-in file hasn't been installed:
# Using WP-CLI
wp redis enable --path=/var/www/html
# Or in WordPress admin: Settings → Redis → Enable Object Cache
Need to Flush All Cache
# Flush all keys (use with caution)
redis-cli -a password FLUSHALL
# Flush only database 0
redis-cli -a password -n 0 FLUSHDB
Frequently Asked Questions
How do Redis and Memcached differ? Which should I use?
Redis supports diverse data structures (strings, hashes, lists, sets, sorted sets), offers persistence options, and includes pub/sub messaging. Memcached is slightly faster for pure key-value caching but lacks these features. Today, Redis is the default choice for most use cases.
How much should I set maxmemory to?
A good rule of thumb is no more than 25–30% of your total RAM. For example, on a 4 GB VPS, set maxmemory to 1 GB, leaving plenty of RAM for the OS, database, and application.
Is Redis safe if bound to 0.0.0.0?
Absolutely not. Redis was not designed to be exposed directly to the internet. Always bind it to 127.0.0.1. If you need access from another server, use an SSH tunnel or a private network connection.