Redis (Remote Dictionary Server) is an in-memory data structure store used as a cache, session store, real-time leaderboard, pub/sub message broker, and rate-limiting backend. Redis stores all data in RAM with optional disk persistence, making it orders of magnitude faster than relational databases for lookups: sub-millisecond response times at millions of operations per second. Common use cases include caching database query results to reduce MySQL/PostgreSQL load, storing PHP/Python session data, implementing distributed locks, rate limiting API endpoints, and real-time pub/sub messaging. RHEL 9’s AppStream includes Redis 7. This guide covers installing Redis 7 on RHEL 9, configuring authentication and persistence, tuning memory limits, and connecting from PHP and Python applications.
Prerequisites
- RHEL 9 with sudo/root access
Step 1 — Install Redis
dnf install -y redis
systemctl enable --now redis
redis-cli ping # Should return: PONG
Step 2 — Configure Authentication
# /etc/redis/redis.conf
# Add a strong requirepass (Redis password)
requirepass StrongRedisPassword456!
systemctl restart redis
redis-cli -a StrongRedisPassword456! ping
Step 3 — Configure Memory Limits and Eviction Policy
# /etc/redis/redis.conf
# Maximum memory for Redis (use for caching — evict old keys when limit reached)
maxmemory 512mb
# Eviction policy when maxmemory is reached:
# allkeys-lru: evict any key using LRU (best for pure caches)
# volatile-lru: evict only keys with TTL using LRU (for mixed cache+persistent use)
maxmemory-policy allkeys-lru
# Persist keys with expiry to disk (optional for cache-only setups)
# RDB snapshot — save to disk every 60s if at least 1000 keys changed
save 60 1000
# AOF persistence (recommended for durability)
appendonly yes
appendfsync everysec
systemctl restart redis
Step 4 — Tune Kernel for Redis Performance
# Redis requires vm.overcommit_memory = 1 for RDB snapshots without OOM errors
echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf
sysctl -p
# Disable Transparent Huge Pages (causes latency spikes)
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.local
chmod +x /etc/rc.d/rc.local
Step 5 — Basic Redis Operations
redis-cli -a StrongRedisPassword456!
# String (cache a value with 5-minute TTL)
SET session:user123 '{"id":123,"name":"Alice"}' EX 300
# Get a value
GET session:user123
# Hash (store structured data)
HSET user:123 name "Alice" email "[email protected]" created "2024-01-01"
HGETALL user:123
# List
RPUSH queue:jobs "job1" "job2"
LRANGE queue:jobs 0 -1
# Check memory usage
INFO memory | grep used_memory_human
Step 6 — Connect from PHP and Python
# PHP (using phpredis extension)
dnf install -y php-redis
# /var/www/app/cache.php
connect('127.0.0.1', 6379);
$redis->auth('StrongRedisPassword456!');
$redis->setex('my_key', 300, 'my_value');
echo $redis->get('my_key');
# Python
pip install redis
python3 -c "
import redis
r = redis.Redis(host='localhost', port=6379, password='StrongRedisPassword456!', decode_responses=True)
r.setex('py_key', 300, 'py_value')
print(r.get('py_key'))
"
Conclusion
Redis 7 on RHEL 9 provides a versatile in-memory data store that dramatically accelerates web applications through caching, session storage, and pub/sub messaging. Configuring authentication via requirepass, setting a memory limit with an LRU eviction policy, enabling AOF persistence, and tuning kernel parameters creates a production-ready Redis installation suitable for high-traffic environments.
Next steps: How to Configure Redis Cluster and Sentinel on RHEL 9, How to Install Memcached on RHEL 9, and How to Install MongoDB on RHEL 9.