Redis
This guide explains how to install, configure, and run Redis both on a VPS (Linux server) and on a local laptop (Windows/macOS/Linux) for development or production use.
π§© 1. What is Redis?β
Redis is an in-memory data store used as a database, cache, or message broker. Itβs extremely fast and lightweight β ideal for caching, sessions, queues, and real-time data processing.
βοΈ 2. Installation on VPS (Ubuntu/Debian)β
Step 1: Update system packagesβ
sudo apt update && sudo apt upgrade -y
Step 2: Install Redisβ
sudo apt install redis-server -y
Step 3: Enable and start Redis serviceβ
sudo systemctl enable redis-server
sudo systemctl start redis-server
Step 4: Verify Redis is runningβ
sudo systemctl status redis-server
Expected output:
Active: active (running)
Step 5: Test connectionβ
redis-cli ping
Expected output:
PONG
π 3. Secure Redis (for VPS)β
By default, Redis listens on 127.0.0.1 (localhost). To allow remote connections securely:
Edit Redis config:β
sudo nano /etc/redis/redis.conf
Find and modify:
bind 127.0.0.1 ::1
Change to:
bind 0.0.0.0
Then set a password:
requirepass your_strong_password
Restart Redis:
sudo systemctl restart redis-server
Allow Redis port via UFW:β
sudo ufw allow 6379/tcp
β οΈ Warning: Never expose Redis to the public internet without a password or firewall rules.
π§± 4. Installation on Local Laptopβ
πͺ Windows (via WSL2 or Docker)β
Option 1: Install via WSL (Ubuntu)β
sudo apt update && sudo apt install redis-server -y
sudo service redis-server start
redis-cli ping
Option 2: Install via Dockerβ
docker run --name redis -p 6379:6379 -d redis
Check:
docker exec -it redis redis-cli ping
Output:
PONG
Option 3: Native Windows Build (less recommended)β
You can download Redis for Windows from: π https://github.com/microsoftarchive/redis/releases
Run with:
redis-server.exe redis.windows.conf
π macOSβ
Using Homebrewβ
brew install redis
brew services start redis
redis-cli ping
Manual startβ
redis-server /usr/local/etc/redis.conf
β‘ 5. Redis Configuration Basicsβ
Main configuration file:
/etc/redis/redis.conf
Common parameters:
| Parameter | Description |
|---|---|
bind | IP address Redis listens to |
port | Default is 6379 |
requirepass | Authentication password |
maxmemory | Limit memory usage |
maxmemory-policy | Eviction policy (e.g., allkeys-lru) |
Reload configuration after changes:
sudo systemctl restart redis-server
π§ 6. Connecting from Applicationsβ
Example (Node.js):
import Redis from "ioredis";
const redis = new Redis({
host: "127.0.0.1",
port: 6379,
password: "your_strong_password",
});
await redis.set("key", "Hello Redis");
const value = await redis.get("key");
console.log(value);
Example (Python):
import redis
r = redis.Redis(host='localhost', port=6379, password='your_strong_password')
print(r.ping())
π§© 7. Persistence & Data Safetyβ
Redis uses two persistence methods:
- RDB (Snapshotting) β saves data periodically to disk (
dump.rdb). - AOF (Append Only File) β logs every write operation (
appendonly.aof).
Check config:
sudo nano /etc/redis/redis.conf
Make sure:
save 900 1
save 300 10
save 60 10000
appendonly yes
Restart Redis after changes:
sudo systemctl restart redis-server
π§° 8. Common Troubleshootingβ
| Issue | Fix |
|---|---|
Could not connect to Redis | Ensure service is running (sudo systemctl status redis-server) |
| Port 6379 blocked | Allow via firewall or use SSH tunnel |
| Data lost after restart | Enable AOF persistence |
| Redis wonβt start | Check /var/log/redis/redis-server.log |
π 9. Useful Commandsβ
redis-cli info
redis-cli keys '*'
redis-cli flushall
redis-cli monitor
Stop Redis:
sudo systemctl stop redis-server
Restart Redis:
sudo systemctl restart redis-server
π― 10. Summaryβ
| Environment | Recommended Install | Notes |
|---|---|---|
| VPS (Production) | apt install redis-server | Secure with password + UFW |
| Local (Development) | Docker or WSL2 | Easier to reset & test |
Redis is lightweight, fast, and perfect for caching, session management, and message brokering.
π Last Updated: November 2025 π§βπ» Author: Huy Pham