Prerequisites & VPS Selection
Development
- • 512MB RAM
- • Learning & testing
Small Prod
- • 1-2GB RAM
- • Caching, sessions
Medium Prod
- • 4-8GB RAM
- • Larger datasets
Large Prod
- • 16GB+ RAM
- • High-traffic apps
What You'll Need
- Ubuntu 22.04/24.04 LTS or Debian 11/12
- Root or sudo access
- SSH access configured
- Basic Linux command line familiarity
Install Redis
Install Redis from the official repository for the latest stable version:
sudo apt update
sudo apt upgrade -ysudo apt install -y curl gnupg lsb-releasecurl -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.listsudo apt update
sudo apt install -y redissudo systemctl start redis-server
sudo systemctl enable redis-serverredis-cli ping✅ Success: You should see "PONG" as the response, indicating Redis is running correctly.
Configuration
Configure Redis in /etc/redis/redis.conf:
sudo nano /etc/redis/redis.confKey Configuration Options
| Directive | Default | Description |
|---|---|---|
| bind | 127.0.0.1 | IP address(es) to listen on |
| port | 6379 | TCP port for connections |
| daemonize | yes | Run as background daemon |
| maxmemory | 0 | Maximum memory usage limit |
| maxmemory-policy | noeviction | Eviction policy when maxmemory reached |
| timeout | 0 | Client idle timeout (0 = disabled) |
Memory Management
Set memory limits to prevent Redis from consuming all system RAM:
# Set maximum memory (adjust based on your VPS)
maxmemory 256mb
# Eviction policy when maxmemory is reached
# Options: volatile-lru, allkeys-lru, volatile-random,
# allkeys-random, volatile-ttl, noeviction
maxmemory-policy allkeys-lruEviction Policy Comparison
| Policy | Description |
|---|---|
| volatile-lru | Evict least recently used keys with expiration set |
| allkeys-lru | Evict least recently used keys (recommended for caching) |
| volatile-ttl | Evict keys with shortest TTL first |
| noeviction | Return errors when memory limit reached |
Security Hardening
Secure your Redis installation:
Password Authentication
openssl rand -base64 32requirepass YourStrongPassword123!@#redis-cli
AUTH YourStrongPassword123!@#Network Security
# Only accept local connections (most secure)
bind 127.0.0.1 ::1
# Accept connections from specific private IP
bind 127.0.0.1 10.0.0.5# Allow Redis from specific IP only
sudo ufw allow from 10.0.0.10 to any port 6379
# Or allow from subnet
sudo ufw allow from 10.0.0.0/24 to any port 6379# From client machine
ssh -L 6379:127.0.0.1:6379 user@your-ramnode-vpsTLS/SSL Encryption (Redis 6.0+)
sudo mkdir -p /etc/redis/tls
cd /etc/redis/tls
sudo openssl genrsa -out redis.key 4096
sudo openssl req -new -key redis.key -out redis.csr -subj "/CN=redis"
sudo openssl x509 -req -days 365 -in redis.csr -signkey redis.key -out redis.crttls-port 6380
tls-cert-file /etc/redis/tls/redis.crt
tls-key-file /etc/redis/tls/redis.key
tls-auth-clients noDisable Dangerous Commands
# Rename dangerous commands
rename-command FLUSHDB ""
rename-command FLUSHALL ""
rename-command DEBUG ""
rename-command CONFIG "ADMIN_CONFIG_b4c2d8f1"Data Persistence
Redis offers two persistence mechanisms to protect against data loss:
RDB Snapshots
Creates point-in-time snapshots at specified intervals:
# Save snapshot rules (seconds changes)
save 900 1 # Save if 1 key changed in 900 seconds
save 300 10 # Save if 10 keys changed in 300 seconds
save 60 10000 # Save if 10000 keys changed in 60 seconds
# Snapshot filename and directory
dbfilename dump.rdb
dir /var/lib/redisAppend-Only File (AOF)
Logs every write operation for better durability:
appendonly yes
appendfilename "appendonly.aof"
# Fsync policy options:
# - always: Most durable, slowest
# - everysec: Good balance (recommended)
# - no: Fastest, least durable
appendfsync everysecHybrid Persistence (Recommended)
Redis 4.0+ combines RDB and AOF benefits:
aof-use-rdb-preamble yes💡 This creates an AOF file that starts with an RDB snapshot followed by AOF commands for optimal recovery.
Basic Redis Commands
redis-cli
# Or with authentication:
redis-cli -a YourPasswordEssential Commands
| Command | Description |
|---|---|
| SET key value | Store a string value |
| GET key | Retrieve a string value |
| DEL key | Delete a key |
| EXPIRE key seconds | Set expiration time |
| TTL key | Get remaining time to live |
| KEYS pattern | Find keys matching pattern |
| INFO | Server information and statistics |
| DBSIZE | Number of keys in database |
| MONITOR | Real-time command monitoring |
Working with Data Types
SET user:1:name "John Doe"
GET user:1:name
INCR page:viewsHSET user:1 name "John" email "john@example.com"
HGET user:1 name
HGETALL user:1LPUSH queue:tasks "task1"
RPUSH queue:tasks "task2"
LPOP queue:tasks
LRANGE queue:tasks 0 -1SADD tags:post:1 "redis" "tutorial" "vps"
SMEMBERS tags:post:1
SISMEMBER tags:post:1 "redis"Common Use Cases
Session Storage
Store user sessions with automatic expiration:
# Store session with 30-minute expiration
SETEX session:abc123 1800 '{"user_id":1,"role":"admin"}'
# Retrieve session
GET session:abc123
# Extend session
EXPIRE session:abc123 1800Application Caching
Cache database queries or API responses:
# Cache with 5-minute TTL
SETEX cache:user:1:profile 300 '{"name":"John","email":"john@example.com"}'
# Check if cached
EXISTS cache:user:1:profile
# Invalidate cache
DEL cache:user:1:profileRate Limiting
Implement API rate limiting:
# Increment request counter (resets every minute)
INCR ratelimit:api:192.168.1.1
EXPIRE ratelimit:api:192.168.1.1 60
# Check current count
GET ratelimit:api:192.168.1.1Monitoring & Health Checks
# Basic connectivity check
redis-cli ping
# Detailed server information
redis-cli INFO
# Memory usage
redis-cli INFO memory
# Connected clients
redis-cli INFO clients
# Keyspace statistics
redis-cli INFO keyspaceKey Performance Metrics
| Metric | What to Watch |
|---|---|
| used_memory | Should stay below maxmemory setting |
| connected_clients | Sudden spikes may indicate issues |
| blocked_clients | Should typically be 0 |
| instantaneous_ops_per_sec | Operations throughput |
| hit_rate | Cache effectiveness (keyspace_hits / total) |
| rdb_last_bgsave_status | Should be "ok" |
Backup Strategy
#!/bin/bash
# /opt/scripts/redis-backup.sh
BACKUP_DIR=/var/backups/redis
DATE=$(date +%Y%m%d_%H%M%S)
# Trigger RDB save
redis-cli -a "YourPassword" BGSAVE
# Wait for save to complete
sleep 5
# Copy RDB file
cp /var/lib/redis/dump.rdb $BACKUP_DIR/dump_$DATE.rdb
# Keep only last 7 days
find $BACKUP_DIR -name "dump_*.rdb" -mtime +7 -delete# Make executable
chmod +x /opt/scripts/redis-backup.sh
# Daily backup at 2 AM
0 2 * * * /opt/scripts/redis-backup.shTroubleshooting
Common Issues & Solutions
| Issue | Solution |
|---|---|
| Connection refused | Check if Redis is running: systemctl status redis-server |
| NOAUTH required | Provide password with -a flag or AUTH command |
| OOM (Out of Memory) | Increase maxmemory or adjust eviction policy |
| High latency | Check slow commands with SLOWLOG GET 10 |
| Cannot connect remotely | Check bind settings and firewall rules |
| RDB save failing | Check disk space and permissions on data directory |
# View Redis logs
sudo tail -f /var/log/redis/redis-server.log
# Check for warnings
sudo grep -i warn /var/log/redis/redis-server.log
# Check service status
sudo systemctl status redis-serverRedis Deployed Successfully!
Your Redis installation is ready. Redis provides exceptional performance for caching, session management, and real-time data processing.
Next Steps:
- ✓ Regularly monitor your instance
- ✓ Maintain automated backups
- ✓ Keep Redis updated for security patches
- ✓ Consider Redis Sentinel for high availability
- ✓ Explore Redis Cluster for horizontal scaling
Additional Resources
Ready to Deploy Redis?
Get started with a RamNode VPS and deploy Redis in minutes. Our high-performance infrastructure is perfect for in-memory data workloads.
View VPS Plans →