File size: 3,549 Bytes
0a9d112
 
 
 
 
 
 
 
 
579a6d1
0a9d112
579a6d1
0a9d112
579a6d1
0a9d112
579a6d1
 
 
 
 
0a9d112
579a6d1
 
 
 
0a9d112
579a6d1
0a9d112
579a6d1
 
 
 
 
 
 
 
 
0a9d112
 
 
 
 
 
 
 
579a6d1
0a9d112
 
 
 
 
 
579a6d1
0a9d112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
579a6d1
0a9d112
579a6d1
 
0a9d112
579a6d1
 
0a9d112
 
 
579a6d1
 
0a9d112
579a6d1
0a9d112
 
579a6d1
0a9d112
579a6d1
0a9d112
 
 
579a6d1
 
0a9d112
579a6d1
0a9d112
 
579a6d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a9d112
579a6d1
 
 
 
0a9d112
579a6d1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# Redis Setup for OTP Service

## Overview
The OTP service uses Redis for temporary storage of verification codes. Redis provides:
- Fast in-memory storage
- Automatic expiry (TTL)
- Production-ready scalability
- No cleanup jobs needed

## HuggingFace Spaces - Use Redis Cloud

HuggingFace Spaces doesn't allow running Redis server in containers. Use **Redis Cloud** (free tier):

### Setup Redis Cloud (Free 30MB)

1. **Sign up:** https://redis.com/try-free/
2. **Create database:**
   - Select "Free" plan (30MB - enough for ~30,000 OTPs)
   - Choose region closest to your HF Space
   - Copy the connection URL

3. **Add to HF Space:**
   - Go to your Space Settings → Variables
   - Add secret: `REDIS_URL`
   - Value: `redis://default:YOUR_PASSWORD@YOUR_HOST:PORT`

Example:
```
REDIS_URL=redis://default:abc123xyz@redis-12345.c1.us-east-1.ec2.cloud.redislabs.com:12345
```

That's it! ✅ The app will automatically connect to Redis Cloud.

## Local Development

### Option 1: Use Redis Cloud (Recommended)
Same as production - use Redis Cloud free tier.

### Option 2: Install Redis Locally

**Ubuntu/Debian:**
```bash
sudo apt update
sudo apt install redis-server
sudo systemctl start redis
export REDIS_URL=redis://localhost:6379/0
```

**macOS:**
```bash
brew install redis
brew services start redis
export REDIS_URL=redis://localhost:6379/0
```

**Windows:**
```bash
# Use WSL2 or Docker
wsl --install
# Then follow Ubuntu instructions
```

### Option 3: No Redis (Development Only)
The app automatically falls back to in-memory storage if Redis is unavailable.

**Warning:** In-memory storage:
- ❌ Lost on restart
- ❌ Doesn't work with multiple servers
- ✅ Fine for local testing

## Configuration

Set Redis URL via environment variable:
```bash
export REDIS_URL=redis://localhost:6379/0
```

Or in `.env`:
```
REDIS_URL=redis://localhost:6379/0
```

## Verify Redis is Working

1. **Check health endpoint:**
```bash
curl http://localhost:7860/health
```

Look for:
```json
{
  "components": {
    "redis": {
      "status": "connected",
      "storage_type": "redis"
    }
  }
}
```

2. **Check logs:**
```
✅ OTP Service using Redis storage (redis://localhost:6379/0)
```

If you see:
```
⚠️ Redis connection failed
⚠️ Falling back to in-memory storage
```
Then Redis is not running or not accessible.

## Redis Cloud Configuration

Redis Cloud free tier provides:
- **Max Memory:** 30MB (enough for ~30,000 OTPs)
- **Eviction Policy:** allkeys-lru (removes oldest keys when full)
- **SSL/TLS:** Encrypted connections
- **High Availability:** 99.99% uptime SLA

## Troubleshooting

### Connection refused
Check your `REDIS_URL` environment variable:
```bash
echo $REDIS_URL
```

Test connection:
```bash
redis-cli -u $REDIS_URL ping
# Should return: PONG
```

### Authentication failed
Ensure your Redis Cloud password is correct in the URL:
```
redis://default:YOUR_PASSWORD@host:port
```

### Memory full
Free tier has 30MB. If full:
- OTPs auto-expire (5-30 minutes)
- Oldest keys are evicted automatically (LRU policy)
- Upgrade to paid tier if needed

## Why 30MB is Enough

**OTP Storage:**
- Each OTP: ~500 bytes (code, metadata, timestamps)
- 30MB = ~60,000 OTPs
- OTPs expire in 5-30 minutes
- Even with 1000 users/hour, you'll use <1MB

**Redis Cloud Free Tier is perfect for OTPs!**## Production Scaling

If you outgrow 30MB:
1. **Redis Cloud Pro:** $5/month for 250MB
2. **AWS ElastiCache:** Pay-as-you-go
3. **Self-hosted:** On your own infrastructure

For most apps, free tier is sufficient! ✅