Valkey is a high-performance, open-source key-value data store that emerged as a Redis fork, offering excellent compatibility while maintaining independence from Redis’ licensing changes. In this guide, we’ll walk through deploying Valkey on a Ramnode VPS, a popular choice for developers seeking reliable and affordable cloud hosting.
Why Choose Valkey and Ramnode?
Valkey provides the same lightning-fast performance and Redis compatibility you’re familiar with, but with the assurance of remaining truly open-source under the BSD license. It’s perfect for caching, session storage, real-time analytics, and message queuing.
Ramnode offers high-performance SSD VPS instances with excellent network connectivity and competitive pricing, making it an ideal platform for hosting database services like Valkey.
Prerequisites
Before we begin, ensure you have:
- A Ramnode VPS instance (Ubuntu 22.04 LTS recommended)
- SSH access to your server
- Basic familiarity with Linux command line
- Sudo privileges on your VPS
Step 1: Initial Server Setup
First, connect to your Ramnode VPS via SSH:
ssh root@your-server-ip
Update your system packages:
apt update && apt upgrade -y
Create a non-root user for better security:
adduser valkey
usermod -aG sudo valkey
su - valkey
Step 2: Installing Dependencies
Install the necessary build tools and dependencies:
sudo apt install -y build-essential tcl wget curl git
Step 3: Installing Valkey
Method 1: Building from Source (Recommended)
Clone the Valkey repository:
cd /opt
sudo git clone https://github.com/valkey-io/valkey.git
sudo chown -R valkey:valkey valkey
cd valkey
Compile Valkey:
make
sudo make install
Method 2: Using Package Manager (Alternative)
For newer Ubuntu versions, you might find Valkey in package repositories:
sudo apt install valkey-server
Step 4: Configuring Valkey
Create the Valkey configuration directory:
sudo mkdir -p /etc/valkey
sudo mkdir -p /var/lib/valkey
sudo mkdir -p /var/log/valkey
Create a configuration file:
sudo nano /etc/valkey/valkey.conf
Add the following basic configuration:
# Basic Configuration
bind 127.0.0.1
port 6379
timeout 300
tcp-keepalive 60
# Security
requirepass your_strong_password_here
protected-mode yes
# Persistence
save 900 1
save 300 10
save 60 10000
# Directories
dir /var/lib/valkey
logfile /var/log/valkey/valkey.log
pidfile /var/run/valkey.pid
# Memory Management
maxmemory 256mb
maxmemory-policy allkeys-lru
# Networking
tcp-backlog 511
databases 16
Set appropriate permissions:
sudo chown valkey:valkey /etc/valkey/valkey.conf
sudo chown valkey:valkey /var/lib/valkey
sudo chown valkey:valkey /var/log/valkey
sudo chmod 640 /etc/valkey/valkey.conf
Step 5: Creating a Systemd Service
Create a systemd service file:
sudo nano /etc/systemd/system/valkey.service
Add the following content:
[Unit]
Description=Valkey In-Memory Data Store
After=network.target
[Service]
User=valkey
Group=valkey
ExecStart=/usr/local/bin/valkey-server /etc/valkey/valkey.conf
ExecStop=/usr/local/bin/valkey-cli shutdown
Restart=always
RestartSec=3
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
Reload systemd and enable the service:
sudo systemctl daemon-reload
sudo systemctl enable valkey
sudo systemctl start valkey
Step 6: Verifying the Installation
Check if Valkey is running:
sudo systemctl status valkey
Test the connection:
valkey-cli ping
You should see PONG
as the response.
Test with authentication:
valkey-cli -a your_strong_password_here ping
Step 7: Security Hardening
Firewall Configuration
Install and configure UFW:
sudo apt install ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw allow from your_application_server_ip to any port 6379
sudo ufw enable
Additional Security Measures
- Disable dangerous commands by adding to your valkey.conf:
rename-command FLUSHDB ""
rename-command FLUSHALL ""
rename-command CONFIG "CONFIG_x7f3k9m"
- Use strong passwords and consider key-based authentication for SSH.
- Regular backups – set up automated backups of your Valkey data.
Step 8: Performance Optimization
Memory Overcommit
Add to /etc/sysctl.conf
:
vm.overcommit_memory = 1
Apply the changes:
sudo sysctl -p
Disable Transparent Huge Pages
Add to /etc/rc.local
:
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag
Step 9: Monitoring and Maintenance
Basic Monitoring
Create a simple monitoring script:
nano ~/valkey-monitor.sh
#!/bin/bash
echo "Valkey Status: $(sudo systemctl is-active valkey)"
echo "Memory Usage: $(valkey-cli -a your_password info memory | grep used_memory_human)"
echo "Connected Clients: $(valkey-cli -a your_password info clients | grep connected_clients)"
echo "Total Commands: $(valkey-cli -a your_password info stats | grep total_commands_processed)"
Make it executable:
chmod +x ~/valkey-monitor.sh
Log Rotation
Create a logrotate configuration:
sudo nano /etc/logrotate.d/valkey
/var/log/valkey/*.log {
weekly
missingok
rotate 12
compress
notifempty
create 640 valkey valkey
postrotate
systemctl reload valkey
endscript
}
Connecting from Applications
Here’s how to connect to your Valkey instance from different programming languages:
Python (using redis-py)
import redis
r = redis.Redis(
host='your-ramnode-ip',
port=6379,
password='your_strong_password_here',
decode_responses=True
)
r.set('test', 'Hello Valkey!')
print(r.get('test'))
Node.js (using ioredis)
const Redis = require('ioredis');
const redis = new Redis({
host: 'your-ramnode-ip',
port: 6379,
password: 'your_strong_password_here'
});
redis.set('test', 'Hello Valkey!');
redis.get('test').then(result => console.log(result));
Troubleshooting Common Issues
Issue: Valkey won’t start
- Check logs:
sudo journalctl -u valkey -f
- Verify configuration:
valkey-server --test-config /etc/valkey/valkey.conf
Issue: Connection refused
- Check if Valkey is running:
sudo systemctl status valkey
- Verify firewall rules:
sudo ufw status
- Check bind configuration in valkey.conf
Issue: Out of memory
- Monitor memory usage:
valkey-cli info memory
- Adjust maxmemory settings in configuration
- Consider upgrading your Ramnode VPS plan
Backup and Recovery
Set up automated backups:
#!/bin/bash
# backup-valkey.sh
BACKUP_DIR="/home/valkey/backups"
DATE=$(date +%Y%m%d_%H%M%S)
mkdir -p $BACKUP_DIR
valkey-cli -a your_password --rdb $BACKUP_DIR/valkey_backup_$DATE.rdb
find $BACKUP_DIR -name "*.rdb" -mtime +7 -delete
Add to crontab for daily backups:
crontab -e
# Add this line for daily backups at 2 AM
0 2 * * * /home/valkey/backup-valkey.sh
Conclusion
You now have a fully functional Valkey instance running on your Ramnode VPS! This setup provides excellent performance for caching, session storage, and real-time applications. The configuration we’ve implemented includes security best practices, performance optimizations, and monitoring capabilities.
Remember to regularly update Valkey, monitor your server resources, and maintain proper backups. As your application grows, you can always scale up your Ramnode VPS or implement clustering for high availability.
For production environments, consider implementing additional monitoring tools like Prometheus and Grafana, setting up SSL/TLS encryption, and creating comprehensive backup and disaster recovery procedures.
Next Steps:
- Set up SSL/TLS encryption for secure connections
- Implement clustering for high availability
- Configure advanced monitoring and alerting
- Optimize for your specific use case (caching vs. persistence)
Happy coding with Valkey on Ramnode!