Set up Redis 7 as a centralized caching layer and session store for microservices, with service discovery integration and clustering for high availability. Configure distributed session management patterns and implement Redis clustering for horizontal scalability.
Prerequisites
- Root or sudo access
- Basic understanding of microservices architecture
- Redis 7 compatible system
- Network connectivity between services
What this solves
Modern microservices architectures require centralized caching and session management to maintain performance and state consistency across distributed services. Redis serves as an ideal solution for microservices caching patterns, providing low-latency data access, distributed session storage, and pub/sub messaging capabilities. This tutorial shows you how to integrate Redis 7 with your microservices infrastructure for caching, session management, and service coordination.
Step-by-step installation
Update system packages
Start by updating your package manager to ensure you get the latest Redis packages and dependencies.
sudo apt update && sudo apt upgrade -y
sudo apt install -y wget curl gnupg2 lsb-release
Install Redis 7 from official repository
Install Redis 7 from the official repository to get the latest features including improved clustering and ACL support for microservices security.
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
sudo apt update
sudo apt install -y redis-server redis-tools
Configure Redis for microservices
Configure Redis with optimized settings for microservices workloads including memory policies, persistence, and networking.
# Network configuration for microservices
bind 0.0.0.0
port 6379
protected-mode yes
requirepass microservices_redis_2024
Memory optimization for caching
maxmemory 2gb
maxmemory-policy allkeys-lru
Persistence for session data
save 900 1
save 300 10
save 60 10000
Enable AOF for session durability
appendonly yes
appendfsync everysec
Logging
loglevel notice
logfile /var/log/redis/redis-server.log
Performance tuning
tcp-keepalive 300
timeout 300
tcp-backlog 511
Security
renamed-commands
rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command DEBUG ""
rename-command CONFIG "CONFIG_a8f2d1e9"
Create Redis service discovery configuration
Set up Redis with service discovery patterns for microservices to dynamically locate Redis instances and handle failover scenarios.
# Service discovery configuration
Include this in main redis.conf
Service registration
replica-announce-ip 203.0.113.10
replica-announce-port 6379
Health check settings
repl-ping-replica-period 10
repl-timeout 60
Cluster bus port for service mesh
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
cluster-require-full-coverage no
Configure Redis clustering for scalability
Set up Redis cluster configuration to enable horizontal scaling and high availability for microservices workloads.
sudo mkdir -p /var/lib/redis/cluster
sudo mkdir -p /etc/redis/cluster
sudo chown -R redis:redis /var/lib/redis/cluster
sudo chown -R redis:redis /etc/redis/cluster
# Redis cluster configuration template
port 7000
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-node-timeout 15000
appendonly yes
appendfilename "appendonly-7000.aof"
dir /var/lib/redis/cluster/
logfile /var/log/redis/redis-cluster-7000.log
Authentication
requirepass cluster_pass_2024
masterauth cluster_pass_2024
Memory and performance
maxmemory 1gb
maxmemory-policy allkeys-lru
tcp-keepalive 60
Persistence
save 900 1
save 300 10
save 60 10000
Set up session management configuration
Configure Redis for distributed session management with proper TTL and serialization settings for microservices session sharing.
# Session management optimizations
Add to main redis.conf
Session-specific settings
notify-keyspace-events Ex
Session cleanup
lazy-expire-disabled no
active-rehashing yes
Connection pooling for microservices
tcp-keepalive 300
timeout 0
Memory efficiency for sessions
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
Create microservices connection configuration
Set up connection parameters and patterns that microservices will use to connect to Redis for caching and session management.
# Microservices Redis client configuration
Use this as a template for application configs
[redis]
host = 203.0.113.10
port = 6379
password = microservices_redis_2024
db = 0
Connection pool settings
max_connections = 50
connection_pool_timeout = 20
socket_connect_timeout = 5
socket_timeout = 5
health_check_interval = 30
Session configuration
[sessions]
db = 1
ttl = 3600
prefix = "session:"
serializer = "json"
Cache configuration
[cache]
db = 2
default_ttl = 300
prefix = "cache:"
compression = true
Pub/Sub for service coordination
[pubsub]
db = 3
channel_prefix = "microservices:"
Enable and start Redis services
Start Redis with the microservices configuration and enable it to start automatically on system boot.
sudo systemctl daemon-reload
sudo systemctl enable redis-server
sudo systemctl start redis-server
sudo systemctl status redis-server
Create Redis monitoring and health check scripts
Set up monitoring scripts that microservices can use to check Redis health and perform service discovery operations.
#!/bin/bash
Redis health check for microservices
REDIS_HOST="203.0.113.10"
REDIS_PORT="6379"
REDIS_PASS="microservices_redis_2024"
Test connection and basic operations
redis-cli -h $REDIS_HOST -p $REDIS_PORT -a $REDIS_PASS ping
if [ $? -ne 0 ]; then
echo "Redis connection failed"
exit 1
fi
Test memory usage
MEMORY_USED=$(redis-cli -h $REDIS_HOST -p $REDIS_PORT -a $REDIS_PASS info memory | grep used_memory_human | cut -d: -f2 | tr -d '\r')
echo "Memory used: $MEMORY_USED"
Test key operations
redis-cli -h $REDIS_HOST -p $REDIS_PORT -a $REDIS_PASS set health_check "$(date)" EX 10 > /dev/null
redis-cli -h $REDIS_HOST -p $REDIS_PORT -a $REDIS_PASS get health_check > /dev/null
if [ $? -eq 0 ]; then
echo "Redis health check passed"
exit 0
else
echo "Redis health check failed"
exit 1
fi
sudo chmod +x /usr/local/bin/redis-health-check.sh
Configure Redis Sentinel for high availability
Set up Redis Sentinel to provide automatic failover and service discovery for microservices in production environments.
# Redis Sentinel configuration for microservices HA
port 26379
sentinel announce-ip 203.0.113.10
sentinel announce-port 26379
Monitor Redis master
sentinel monitor microservices-master 203.0.113.10 6379 2
sentinel auth-pass microservices-master microservices_redis_2024
sentinel down-after-milliseconds microservices-master 5000
sentinel parallel-syncs microservices-master 1
sentinel failover-timeout microservices-master 10000
Sentinel authentication
requirepass sentinel_pass_2024
Logging
logfile /var/log/redis/redis-sentinel.log
Security
protected-mode yes
bind 0.0.0.0
Set up microservices caching patterns
Create example configurations for common microservices caching patterns including cache-aside, write-through, and session storage.
# Microservices caching pattern configurations
Cache-aside pattern settings
[cache-aside]
default_ttl = 300
max_retries = 3
backoff_factor = 2
Write-through cache settings
[write-through]
sync_writes = true
write_timeout = 1000
consistency_level = "strong"
Write-behind cache settings
[write-behind]
batch_size = 100
flush_interval = 5000
max_queue_size = 10000
Session cache settings
[session-cache]
session_ttl = 3600
sliding_expiration = true
secure_serialization = true
session_key_prefix = "sess:"
API response cache
[api-cache]
response_ttl = 180
vary_by_headers = ["Authorization", "User-Agent"]
compression_enabled = true
compression_threshold = 1024
Configure service discovery integration
Set up Consul integration
Configure Redis to register with Consul for automatic service discovery by microservices. This enables dynamic Redis endpoint resolution.
{
"service": {
"name": "redis",
"id": "redis-primary",
"port": 6379,
"address": "203.0.113.10",
"tags": ["cache", "session-store", "microservices"],
"check": {
"script": "/usr/local/bin/redis-health-check.sh",
"interval": "10s",
"timeout": "3s"
},
"meta": {
"version": "7.4",
"environment": "production",
"cluster_role": "master"
}
}
}
Create service discovery client library
Set up a reusable configuration pattern that microservices can use to discover and connect to Redis through service discovery.
#!/usr/bin/env python3
Redis service discovery client for microservices
import redis
import consul
import json
import time
from typing import Optional, Dict, Any
class RedisDiscoveryClient:
def __init__(self, consul_host='localhost', consul_port=8500):
self.consul = consul.Consul(host=consul_host, port=consul_port)
self.redis_client = None
self.current_endpoint = None
def discover_redis(self, service_name='redis') -> Optional[Dict[str, Any]]:
"""Discover Redis service from Consul"""
try:
services = self.consul.health.service(service_name, passing=True)[1]
if services:
service = services[0]['Service']
return {
'host': service['Address'],
'port': service['Port'],
'tags': service['Tags']
}
except Exception as e:
print(f"Service discovery failed: {e}")
return None
def get_redis_client(self, db=0) -> redis.Redis:
"""Get Redis client with automatic failover"""
endpoint = self.discover_redis()
if not endpoint or endpoint != self.current_endpoint:
if endpoint:
self.redis_client = redis.Redis(
host=endpoint['host'],
port=endpoint['port'],
db=db,
password='microservices_redis_2024',
socket_timeout=5,
socket_connect_timeout=5,
health_check_interval=30
)
self.current_endpoint = endpoint
else:
raise Exception("No healthy Redis service found")
return self.redis_client
Example usage
if __name__ == "__main__":
client = RedisDiscoveryClient()
redis_conn = client.get_redis_client(db=0)
redis_conn.set("test", "microservices-integration")
print(redis_conn.get("test"))
Implement distributed session management
Configure session serialization and storage
Set up Redis-based session management with proper serialization, TTL management, and cross-service session sharing capabilities.
#!/usr/bin/env python3
Distributed session manager for microservices
import redis
import json
import uuid
import time
from datetime import datetime, timedelta
from typing import Optional, Dict, Any
class MicroservicesSessionManager:
def __init__(self, redis_client: redis.Redis):
self.redis = redis_client
self.session_prefix = "session:"
self.default_ttl = 3600 # 1 hour
def create_session(self, user_id: str, session_data: Dict[str, Any] = None) -> str:
"""Create a new distributed session"""
session_id = str(uuid.uuid4())
session_key = f"{self.session_prefix}{session_id}"
session_object = {
'session_id': session_id,
'user_id': user_id,
'created_at': datetime.utcnow().isoformat(),
'last_accessed': datetime.utcnow().isoformat(),
'data': session_data or {}
}
# Store with expiration
self.redis.setex(
session_key,
self.default_ttl,
json.dumps(session_object)
)
# Create user session index
user_sessions_key = f"user_sessions:{user_id}"
self.redis.sadd(user_sessions_key, session_id)
self.redis.expire(user_sessions_key, self.default_ttl)
return session_id
def get_session(self, session_id: str) -> Optional[Dict[str, Any]]:
"""Retrieve session data"""
session_key = f"{self.session_prefix}{session_id}"
session_data = self.redis.get(session_key)
if session_data:
session_object = json.loads(session_data)
# Update last accessed time
session_object['last_accessed'] = datetime.utcnow().isoformat()
self.redis.setex(session_key, self.default_ttl, json.dumps(session_object))
return session_object
return None
def update_session(self, session_id: str, data: Dict[str, Any]) -> bool:
"""Update session data"""
session = self.get_session(session_id)
if session:
session['data'].update(data)
session['last_accessed'] = datetime.utcnow().isoformat()
session_key = f"{self.session_prefix}{session_id}"
self.redis.setex(session_key, self.default_ttl, json.dumps(session))
return True
return False
def delete_session(self, session_id: str) -> bool:
"""Delete a session"""
session_key = f"{self.session_prefix}{session_id}"
return bool(self.redis.delete(session_key))
def get_user_sessions(self, user_id: str) -> list:
"""Get all sessions for a user"""
user_sessions_key = f"user_sessions:{user_id}"
session_ids = self.redis.smembers(user_sessions_key)
return [sid.decode() for sid in session_ids]
Example usage for microservices
if __name__ == "__main__":
redis_client = redis.Redis(
host='203.0.113.10',
port=6379,
db=1,
password='microservices_redis_2024'
)
session_manager = MicroservicesSessionManager(redis_client)
# Create session
session_id = session_manager.create_session(
user_id="user123",
session_data={"role": "admin", "permissions": ["read", "write"]}
)
print(f"Created session: {session_id}")
Set up microservices cache coordination
Create cache invalidation and coordination patterns that allow microservices to maintain cache consistency across the distributed system.
#!/usr/bin/env python3
Cache coordination for microservices
import redis
import json
import threading
import time
from typing import Callable, Dict, Any
class CacheCoordinator:
def __init__(self, redis_client: redis.Redis):
self.redis = redis_client
self.pubsub = self.redis.pubsub()
self.cache_prefix = "cache:"
self.invalidation_channel = "microservices:cache:invalidate"
self.update_channel = "microservices:cache:update"
self.listeners = []
def cache_set(self, key: str, value: Any, ttl: int = 300, tags: list = None):
"""Set cache with tags for coordinated invalidation"""
cache_key = f"{self.cache_prefix}{key}"
cache_object = {
'value': value,
'timestamp': time.time(),
'tags': tags or [],
'ttl': ttl
}
# Set the cache value
self.redis.setex(cache_key, ttl, json.dumps(cache_object))
# Index by tags for coordinated invalidation
if tags:
for tag in tags:
tag_key = f"cache_tags:{tag}"
self.redis.sadd(tag_key, key)
self.redis.expire(tag_key, ttl + 300) # Slightly longer TTL
# Notify other microservices
self.redis.publish(self.update_channel, json.dumps({
'action': 'set',
'key': key,
'tags': tags,
'timestamp': time.time()
}))
def cache_get(self, key: str) -> Any:
"""Get cache value"""
cache_key = f"{self.cache_prefix}{key}"
cached_data = self.redis.get(cache_key)
if cached_data:
cache_object = json.loads(cached_data)
return cache_object['value']
return None
def invalidate_by_tag(self, tag: str):
"""Invalidate all cache entries with a specific tag"""
tag_key = f"cache_tags:{tag}"
cache_keys = self.redis.smembers(tag_key)
if cache_keys:
# Delete all cache entries
keys_to_delete = [f"{self.cache_prefix}{key.decode()}" for key in cache_keys]
if keys_to_delete:
self.redis.delete(*keys_to_delete)
# Clean up tag index
self.redis.delete(tag_key)
# Notify other microservices
self.redis.publish(self.invalidation_channel, json.dumps({
'action': 'invalidate_tag',
'tag': tag,
'timestamp': time.time(),
'keys_count': len(cache_keys)
}))
def start_coordination_listener(self):
"""Start listening for cache coordination messages"""
self.pubsub.subscribe(self.invalidation_channel, self.update_channel)
def listen_loop():
for message in self.pubsub.listen():
if message['type'] == 'message':
try:
data = json.loads(message['data'])
for listener in self.listeners:
listener(message['channel'].decode(), data)
except Exception as e:
print(f"Cache coordination error: {e}")
thread = threading.Thread(target=listen_loop, daemon=True)
thread.start()
def add_listener(self, callback: Callable):
"""Add a listener for cache coordination events"""
self.listeners.append(callback)
Example usage
if __name__ == "__main__":
redis_client = redis.Redis(
host='203.0.113.10',
port=6379,
db=2,
password='microservices_redis_2024'
)
cache_coordinator = CacheCoordinator(redis_client)
# Set cache with tags
cache_coordinator.cache_set(
"user:123:profile",
{"name": "John Doe", "email": "john@example.com"},
ttl=600,
tags=["user:123", "profiles"]
)
# Start coordination listener
def on_cache_event(channel, data):
print(f"Cache event on {channel}: {data}")
cache_coordinator.add_listener(on_cache_event)
cache_coordinator.start_coordination_listener()
Verify your setup
# Test Redis connection
redis-cli -h 203.0.113.10 -p 6379 -a microservices_redis_2024 ping
Test basic operations
redis-cli -h 203.0.113.10 -p 6379 -a microservices_redis_2024 set test_key "microservices_test"
redis-cli -h 203.0.113.10 -p 6379 -a microservices_redis_2024 get test_key
Check Redis info and memory usage
redis-cli -h 203.0.113.10 -p 6379 -a microservices_redis_2024 info memory
redis-cli -h 203.0.113.10 -p 6379 -a microservices_redis_2024 info replication
Test pub/sub functionality
redis-cli -h 203.0.113.10 -p 6379 -a microservices_redis_2024 publish microservices:test "Hello from microservice"
Check cluster status (if clustering is enabled)
redis-cli -h 203.0.113.10 -p 6379 -a microservices_redis_2024 cluster info
Verify health check script
/usr/local/bin/redis-health-check.sh
Configure Redis clustering for scalability
Initialize Redis cluster
Set up a Redis cluster with multiple nodes to provide horizontal scaling and automatic sharding for microservices workloads.
# Create cluster node directories
sudo mkdir -p /var/lib/redis/cluster/{7000,7001,7002}
sudo chown -R redis:redis /var/lib/redis/cluster/
Create systemd service files for cluster nodes
sudo cp /lib/systemd/system/redis-server.service /lib/systemd/system/redis-cluster@.service
[Unit]
Description=Redis Cluster Node %i
After=network.target
[Service]
Type=notify
ExecStart=/usr/bin/redis-server /etc/redis/cluster/redis-%i.conf
ExecStop=/bin/kill -s QUIT $MAINPID
TimeoutStopSec=0
Restart=always
User=redis
Group=redis
RuntimeDirectory=redis-cluster
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
Create cluster node configurations
Generate individual configuration files for each Redis cluster node with appropriate ports and settings.
# Generate cluster node configs
for port in 7000 7001 7002; do
sudo cp /etc/redis/cluster/redis-cluster.conf /etc/redis/cluster/redis-$port.conf
sudo sed -i "s/7000/$port/g" /etc/redis/cluster/redis-$port.conf
sudo chown redis:redis /etc/redis/cluster/redis-$port.conf
done
Start and initialize the cluster
Start all cluster nodes and initialize the Redis cluster with automatic slot assignment for distributed caching.
# Start cluster nodes
for port in 7000 7001 7002; do
sudo systemctl enable redis-cluster@$port
sudo systemctl start redis-cluster@$port
sudo systemctl status redis-cluster@$port
done
Initialize the cluster
redis-cli --cluster create 203.0.113.10:7000 203.0.113.10:7001 203.0.113.10:7002 --cluster-replicas 0 -a cluster_pass_2024
Verify cluster status
redis-cli -h 203.0.113.10 -p 7000 -a cluster_pass_2024 cluster info
redis-cli -h 203.0.113.10 -p 7000 -a cluster_pass_2024 cluster nodes
Common issues
| Symptom | Cause | Fix |
|---|---|---|
| Connection refused to Redis | Firewall blocking port 6379 | sudo ufw allow 6379/tcp or configure firewall rules |
| Authentication failed errors | Wrong password or missing auth | Verify password in /etc/redis/redis.conf and client configuration |
| Memory errors and evictions | maxmemory limit reached | Increase maxmemory or optimize memory policy in Redis config |
| Session data not persisting | AOF or RDB not configured | Enable persistence with appendonly yes and save directives |
| Cluster nodes not joining | Network or authentication issues | Check cluster bus port 16379 and verify masterauth settings |
| Service discovery failing | Consul agent not running | Start Consul service and verify Redis health check script permissions |
| High latency on cache operations | Network or memory pressure | Optimize tcp-keepalive and check Redis info stats |
| Session conflicts between services | Same session prefix used | Use unique session prefixes per service or database separation |
Next steps
- Set up Redis monitoring with Prometheus and Grafana dashboards
- Configure Redis Sentinel with SSL/TLS encryption and authentication for high availability
- Implement Redis backup automation with RDB and AOF persistence
- Integrate Consul with Kubernetes service discovery and automatic configuration
- Configure NGINX reverse proxy for microservices load balancing
- Implement microservices API gateway with Kong and Redis rate limiting
Automated install script
Run this to automate the entire setup
#!/usr/bin/env bash
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
# Configuration variables
REDIS_PASSWORD="${1:-microservices_redis_$(date +%Y)}"
REDIS_MAX_MEMORY="${2:-2gb}"
BIND_ADDRESS="${3:-127.0.0.1}"
# Usage function
usage() {
echo "Usage: $0 [redis_password] [max_memory] [bind_address]"
echo "Example: $0 mypassword123 4gb 0.0.0.0"
exit 1
}
# Cleanup function
cleanup() {
echo -e "${RED}Installation failed. Cleaning up...${NC}"
systemctl stop redis-server 2>/dev/null || systemctl stop redis 2>/dev/null || true
exit 1
}
trap cleanup ERR
# Check if running as root or with sudo
if [[ $EUID -ne 0 ]]; then
echo -e "${RED}This script must be run as root or with sudo${NC}"
exit 1
fi
echo -e "${GREEN}Redis 7 Microservices Installation Script${NC}"
echo "=================================================="
# Auto-detect distribution
echo -e "${YELLOW}[1/8] Detecting system distribution...${NC}"
if [ -f /etc/os-release ]; then
. /etc/os-release
case "$ID" in
ubuntu|debian)
PKG_MGR="apt"
PKG_UPDATE="apt update"
PKG_INSTALL="apt install -y"
PKG_UPGRADE="apt upgrade -y"
REDIS_SERVICE="redis-server"
REDIS_CONF="/etc/redis/redis.conf"
REDIS_USER="redis"
;;
almalinux|rocky|centos|rhel|ol)
PKG_MGR="dnf"
PKG_UPDATE="dnf check-update || true"
PKG_INSTALL="dnf install -y"
PKG_UPGRADE="dnf upgrade -y"
REDIS_SERVICE="redis"
REDIS_CONF="/etc/redis/redis.conf"
REDIS_USER="redis"
;;
fedora)
PKG_MGR="dnf"
PKG_UPDATE="dnf check-update || true"
PKG_INSTALL="dnf install -y"
PKG_UPGRADE="dnf upgrade -y"
REDIS_SERVICE="redis"
REDIS_CONF="/etc/redis/redis.conf"
REDIS_USER="redis"
;;
amzn)
PKG_MGR="yum"
PKG_UPDATE="yum check-update || true"
PKG_INSTALL="yum install -y"
PKG_UPGRADE="yum upgrade -y"
REDIS_SERVICE="redis"
REDIS_CONF="/etc/redis/redis.conf"
REDIS_USER="redis"
;;
*)
echo -e "${RED}Unsupported distribution: $ID${NC}"
exit 1
;;
esac
echo -e "${GREEN}Detected: $PRETTY_NAME${NC}"
else
echo -e "${RED}Cannot detect distribution${NC}"
exit 1
fi
# Update system packages
echo -e "${YELLOW}[2/8] Updating system packages...${NC}"
$PKG_UPDATE
$PKG_UPGRADE
$PKG_INSTALL wget curl gnupg2
# Install distribution-specific packages
if [[ "$PKG_MGR" == "apt" ]]; then
$PKG_INSTALL lsb-release
elif [[ "$PKG_MGR" == "dnf" ]] || [[ "$PKG_MGR" == "yum" ]]; then
$PKG_INSTALL epel-release
fi
# Install Redis 7
echo -e "${YELLOW}[3/8] Installing Redis 7...${NC}"
if [[ "$PKG_MGR" == "apt" ]]; then
curl -fsSL https://packages.redis.io/gpg | gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" > /etc/apt/sources.list.d/redis.list
apt update
$PKG_INSTALL redis-server redis-tools
else
$PKG_INSTALL redis redis-tools
fi
# Create necessary directories
echo -e "${YELLOW}[4/8] Creating Redis directories...${NC}"
mkdir -p /var/lib/redis/cluster
mkdir -p /etc/redis/cluster
mkdir -p /var/log/redis
chown -R $REDIS_USER:$REDIS_USER /var/lib/redis
chown -R $REDIS_USER:$REDIS_USER /var/log/redis
chown -R $REDIS_USER:$REDIS_USER /etc/redis/cluster
chmod 750 /var/lib/redis /var/log/redis /etc/redis/cluster
# Configure main Redis instance
echo -e "${YELLOW}[5/8] Configuring Redis for microservices...${NC}"
cp $REDIS_CONF ${REDIS_CONF}.backup
cat > $REDIS_CONF << EOF
# Network configuration for microservices
bind $BIND_ADDRESS
port 6379
protected-mode yes
requirepass $REDIS_PASSWORD
# Memory optimization for caching
maxmemory $REDIS_MAX_MEMORY
maxmemory-policy allkeys-lru
# Persistence for session data
save 900 1
save 300 10
save 60 10000
# Enable AOF for session durability
appendonly yes
appendfsync everysec
# Logging
loglevel notice
logfile /var/log/redis/redis-server.log
# Performance tuning
tcp-keepalive 300
timeout 300
tcp-backlog 511
# Session management optimizations
notify-keyspace-events Ex
# Security - disable dangerous commands
rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command DEBUG ""
rename-command CONFIG "CONFIG_$(openssl rand -hex 8)"
# Directories
dir /var/lib/redis/
EOF
# Set proper permissions for config
chown $REDIS_USER:$REDIS_USER $REDIS_CONF
chmod 640 $REDIS_CONF
# Configure cluster template
echo -e "${YELLOW}[6/8] Setting up cluster configuration...${NC}"
cat > /etc/redis/cluster/redis-cluster-template.conf << EOF
port 7000
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-node-timeout 15000
appendonly yes
appendfilename "appendonly-7000.aof"
dir /var/lib/redis/cluster/
logfile /var/log/redis/redis-cluster-7000.log
# Authentication
requirepass $REDIS_PASSWORD
masterauth $REDIS_PASSWORD
# Memory and performance
maxmemory 1gb
maxmemory-policy allkeys-lru
tcp-keepalive 60
# Persistence
save 900 1
save 300 10
save 60 10000
bind $BIND_ADDRESS
protected-mode yes
EOF
chown $REDIS_USER:$REDIS_USER /etc/redis/cluster/redis-cluster-template.conf
chmod 640 /etc/redis/cluster/redis-cluster-template.conf
# Configure firewall
echo -e "${YELLOW}[7/8] Configuring firewall...${NC}"
if command -v ufw >/dev/null 2>&1; then
ufw allow 6379/tcp
ufw allow 16379/tcp
elif command -v firewall-cmd >/dev/null 2>&1; then
firewall-cmd --permanent --add-port=6379/tcp
firewall-cmd --permanent --add-port=16379/tcp
firewall-cmd --reload
fi
# Configure SELinux if present
if command -v setsebool >/dev/null 2>&1 && getenforce 2>/dev/null | grep -q "Enforcing"; then
setsebool -P redis_enable_notify on
fi
# Start and enable Redis service
systemctl enable $REDIS_SERVICE
systemctl start $REDIS_SERVICE
# Verification
echo -e "${YELLOW}[8/8] Verifying installation...${NC}"
sleep 3
if systemctl is-active --quiet $REDIS_SERVICE; then
echo -e "${GREEN}✓ Redis service is running${NC}"
else
echo -e "${RED}✗ Redis service is not running${NC}"
exit 1
fi
# Test Redis connection
if redis-cli -a "$REDIS_PASSWORD" ping 2>/dev/null | grep -q PONG; then
echo -e "${GREEN}✓ Redis authentication working${NC}"
else
echo -e "${RED}✗ Redis authentication failed${NC}"
exit 1
fi
# Display completion message
echo ""
echo -e "${GREEN}=================================================="
echo "Redis 7 Microservices Installation Complete!"
echo "==================================================${NC}"
echo ""
echo "Configuration Details:"
echo "- Redis Version: $(redis-server --version | head -n1)"
echo "- Bind Address: $BIND_ADDRESS"
echo "- Port: 6379"
echo "- Password: $REDIS_PASSWORD"
echo "- Max Memory: $REDIS_MAX_MEMORY"
echo "- Config File: $REDIS_CONF"
echo "- Log File: /var/log/redis/redis-server.log"
echo "- Data Directory: /var/lib/redis/"
echo ""
echo "Service Management:"
echo "- Start: systemctl start $REDIS_SERVICE"
echo "- Stop: systemctl stop $REDIS_SERVICE"
echo "- Status: systemctl status $REDIS_SERVICE"
echo ""
echo "Connection Test:"
echo "redis-cli -a '$REDIS_PASSWORD' ping"
echo ""
echo -e "${YELLOW}Note: Cluster template available at /etc/redis/cluster/redis-cluster-template.conf${NC}"
Review the script before running. Execute with: bash install.sh