# 🔄 Redis Migration Guide

## Overview
Redis is now part of the monitoring stack and running locally in Docker.

---

## ✅ What Was Done

1. **Added Redis to monitoring stack**
   - Image: `redis:7-alpine`
   - Container name: `re_redis`
   - Port: `6379` (mapped to host)
   - Password: Uses `REDIS_PASSWORD` from environment (default: `Redis@123`)
   - Data persistence: Volume `re_redis_data`

2. **Updated Redis Exporter**
   - Now connects to local Redis container
   - Automatically starts after Redis is healthy

---

## 🔧 Update Backend Configuration

### Step 1: Update `.env` file

Open `Re_Backend/.env` and change:

```bash
# OLD (External Redis)
REDIS_HOST=160.187.166.17

# NEW (Local Docker Redis)
REDIS_HOST=localhost
```

Or if you want to use Docker network (recommended for production):
```bash
REDIS_HOST=re_redis  # Use container name if backend is also in Docker
```

### Step 2: Restart Backend

```powershell
# Stop backend (Ctrl+C in terminal)
# Then restart
npm run dev
```

---

## 📊 Verify Everything Works

### 1. Check Redis is Running
```powershell
docker ps --filter "name=redis"
```

Should show:
```
re_redis            Up (healthy)
re_redis_exporter   Up
```

### 2. Test Redis Connection
```powershell
# Test from host
redis-cli -h localhost -p 6379 -a Redis@123 ping
# Should return: PONG
```

### 3. Check Backend Logs
```
[info]: [Redis] ✅ Connected successfully
[info]: [TAT Queue] ✅ Queue initialized
[info]: [Pause Resume Queue] ✅ Queue initialized
```

### 4. Refresh Grafana Dashboard
- Go to: http://localhost:3001/d/re-workflow-overview
- **Redis Status** should show "Up" (green)
- **Redis Connections** should show a number
- **Redis Memory** should show bytes used
- **Queue metrics** should work as before

---

## 🎯 Benefits of Local Redis

✅ **Simpler Setup** - Everything in one place  
✅ **Faster Performance** - Local network, no external latency  
✅ **Data Persistence** - Redis data saved in Docker volume  
✅ **Easy Monitoring** - Redis Exporter automatically connected  
✅ **Environment Isolation** - No conflicts with external Redis  

---

## 🔄 Docker Commands

### Start all monitoring services (including Redis)
```powershell
cd Re_Backend/monitoring
docker-compose -f docker-compose.monitoring.yml up -d
```

### Stop all services
```powershell
docker-compose -f docker-compose.monitoring.yml down
```

### View Redis logs
```powershell
docker logs re_redis
```

### Redis CLI access
```powershell
docker exec -it re_redis redis-cli -a Redis@123
```

### Check Redis data
```powershell
# Inside redis-cli
INFO
DBSIZE
KEYS *
```

---

## 🗄️ Data Persistence

Redis data is persisted in Docker volume:
```
Volume: re_redis_data
Location: Docker managed volume
```

To backup Redis data:
```powershell
docker exec re_redis redis-cli -a Redis@123 SAVE
docker cp re_redis:/data/dump.rdb ./redis-backup.rdb
```

---

## ⚠️ If You Want to Keep External Redis

If you prefer to keep using the external Redis server, simply:

1. Update `docker-compose.monitoring.yml`:
```yaml
redis-exporter:
  environment:
    - REDIS_ADDR=redis://160.187.166.17:6379
  command:
    - '--redis.addr=160.187.166.17:6379'
    - '--redis.password=Redis@123'
```

2. Don't change `.env` in backend

3. Remove the `redis` service from docker-compose if you don't need it locally

---

## 🎉 Summary

Your setup now includes:
- ✅ Redis running locally in Docker
- ✅ Redis Exporter connected and working
- ✅ Backend ready to connect (just update `REDIS_HOST=localhost` in `.env`)
- ✅ All monitoring metrics available in Grafana

**Next step**: Update `Re_Backend/.env` with `REDIS_HOST=localhost` and restart your backend!

