Skip to main content

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

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:

ParameterDescription
bindIP address Redis listens to
portDefault is 6379
requirepassAuthentication password
maxmemoryLimit memory usage
maxmemory-policyEviction 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​

IssueFix
Could not connect to RedisEnsure service is running (sudo systemctl status redis-server)
Port 6379 blockedAllow via firewall or use SSH tunnel
Data lost after restartEnable AOF persistence
Redis won’t startCheck /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​

EnvironmentRecommended InstallNotes
VPS (Production)apt install redis-serverSecure with password + UFW
Local (Development)Docker or WSL2Easier to reset & test

Redis is lightweight, fast, and perfect for caching, session management, and message brokering.


πŸ“… Last Updated: November 2025 πŸ§‘β€πŸ’» Author: Huy Pham