Redis Setup

Complete guide to installing, configuring, and optimizing Redis for MegaVault session storage, caching, and real-time features.

Redis Overview

Redis serves as MegaVault's primary database for session storage, caching, and real-time features, providing high performance and reliability.

Key Features

Redis capabilities for MegaVault

  • ✅ Session storage
  • ✅ Application caching
  • ✅ Real-time features
  • ✅ Background jobs

Performance

High-speed operations

  • ✅ In-memory storage
  • ✅ Microsecond latency
  • ✅ High throughput
  • ✅ Connection pooling

Reliability

Production-ready features

  • ✅ Data persistence
  • ✅ High availability
  • ✅ Backup & recovery
  • ✅ Monitoring tools
💡

Redis Version

MegaVault requires Redis 6.0 or later for optimal performance and security features.

Installation

Install Redis on various platforms and environments.

Ubuntu/Debian

Ubuntu Installation
# Update package list
sudo apt update

# Install Redis
sudo apt install redis-server

# Start and enable Redis
sudo systemctl start redis-server
sudo systemctl enable redis-server

# Verify installation
redis-cli ping

CentOS/RHEL

CentOS Installation
# Install EPEL repository
sudo yum install epel-release

# Install Redis
sudo yum install redis

# Start and enable Redis
sudo systemctl start redis
sudo systemctl enable redis

Docker Installation

docker-compose.yml
version: '3.8'
services:
  redis:
    image: redis:7-alpine
    container_name: megavault-redis
    restart: unless-stopped
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
      - ./redis.conf:/usr/local/etc/redis/redis.conf
    command: redis-server /usr/local/etc/redis/redis.conf
    environment:
      - REDIS_PASSWORD=${REDIS_PASSWORD}

volumes:
  redis_data:

Cloud Redis Services

  • Redis Cloud: Managed Redis by Redis Labs
  • AWS ElastiCache: Amazon's managed Redis service
  • Azure Cache for Redis: Microsoft's Redis offering
  • Google Cloud Memorystore: Google's managed Redis

Configuration

Configure Redis for optimal performance and security in production.

Basic Configuration

redis.conf
# Network and Security
bind 127.0.0.1
port 6379
protected-mode yes
requirepass your-secure-password

# Memory Management
maxmemory 2gb
maxmemory-policy allkeys-lru

# Persistence
save 900 1
save 300 10
save 60 10000
dir /var/lib/redis

# Logging
loglevel notice
logfile /var/log/redis/redis-server.log

# Performance
tcp-keepalive 300
timeout 0
databases 16

MegaVault Environment Variables

Redis Environment Configuration
# Basic Redis Connection
REDIS_URL=redis://localhost:6379/0

# With Password
REDIS_URL=redis://:password@localhost:6379/0

# SSL/TLS Connection
REDIS_URL=rediss://username:password@host:6380/0

# Connection Pool Settings
REDIS_MAX_CONNECTIONS=10
REDIS_MAX_RETRIES=3
REDIS_RETRY_DELAY=1000
REDIS_CONNECT_TIMEOUT=10000

Database Organization

  • Database 0: User sessions
  • Database 1: Application cache
  • Database 2: Background jobs
  • Database 3: Real-time features

Security Setup

Secure your Redis installation for production use.

Authentication

Password Authentication
# Set password in redis.conf
requirepass your-very-secure-password-here

# Or set via command line
redis-cli CONFIG SET requirepass "your-secure-password"

# Test authentication
redis-cli -a your-secure-password ping

Network Security

  • Bind Address: Only bind to localhost or private networks
  • Firewall: Restrict Redis port (6379) access
  • SSL/TLS: Use encrypted connections for remote access
  • VPN: Access Redis through VPN for additional security

Redis ACL (Access Control Lists)

Redis ACL Configuration
# Create user for MegaVault
ACL SETUSER megavault on >password ~* +@all

# Create read-only monitoring user
ACL SETUSER monitor on >monitor-password ~* +@read +info +ping

# Save ACL configuration
ACL SAVE

Performance Tuning

Optimize Redis performance for MegaVault workloads.

Memory Optimization

Memory Settings
# Set maximum memory usage
maxmemory 4gb

# Memory eviction policy
maxmemory-policy allkeys-lru

# Disable swap usage
vm.swappiness = 1

# Transparent huge pages
echo never > /sys/kernel/mm/transparent_hugepage/enabled

Connection Tuning

  • Connection Pooling: Use connection pools in application
  • Keep-Alive: Enable TCP keep-alive
  • Timeout Settings: Configure appropriate timeouts
  • Max Clients: Set based on expected concurrent users

Persistence Tuning

Persistence Configuration
# RDB snapshots
save 900 1      # Save if at least 1 key changed in 900 seconds
save 300 10     # Save if at least 10 keys changed in 300 seconds
save 60 10000   # Save if at least 10000 keys changed in 60 seconds

# AOF (Append Only File) - for better durability
appendonly yes
appendfsync everysec
no-appendfsync-on-rewrite no

Monitoring

Monitor Redis performance and health metrics.

Basic Monitoring Commands

Redis Monitoring
# Check Redis status
redis-cli ping

# Get server information
redis-cli info

# Monitor real-time commands
redis-cli monitor

# Check memory usage
redis-cli info memory

# Check connected clients
redis-cli info clients

# Check key statistics
redis-cli info keyspace

Key Metrics to Monitor

  • Memory Usage: used_memory, maxmemory
  • Operations: instantaneous_ops_per_sec
  • Connections: connected_clients, blocked_clients
  • Persistence: last_save_time, changes_since_last_save
  • Replication: master_repl_offset, slave_lag

Health Check Script

Redis Health Check
#!/bin/bash
# redis-health-check.sh

REDIS_CLI="redis-cli -a $REDIS_PASSWORD"

echo "=== Redis Health Check ==="

# Test connectivity
if $REDIS_CLI ping | grep -q PONG; then
    echo "✓ Redis is responding"
else
    echo "✗ Redis connection failed"
    exit 1
fi

# Check memory usage
MEMORY_USED=$($REDIS_CLI info memory | grep used_memory_human | cut -d: -f2)
echo "Memory used: $MEMORY_USED"

# Check connected clients
CLIENTS=$($REDIS_CLI info clients | grep connected_clients | cut -d: -f2)
echo "Connected clients: $CLIENTS"

echo "Health check completed"

Troubleshooting

Common Redis issues and their solutions.

Common Issues

Connection Refused

Error: Could not connect to Redis

Solutions:

  • Check if Redis service is running
  • Verify port and bind address
  • Check firewall settings
  • Validate password authentication

High Memory Usage

Issue: Redis consuming too much memory

Solutions:

  • Set maxmemory limit
  • Configure eviction policy
  • Clean up expired keys
  • Monitor key patterns
⚠️

Production Checklist

Before deploying Redis to production: enable authentication, configure persistence, set up monitoring, implement backups, and secure network access.