File size: 6,045 Bytes
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# Redis Cloud Setup Guide for SwiftOps

## Step-by-Step Setup (5 minutes)

### Step 1: Create Redis Cloud Account

1. **Go to Redis Cloud:**
   - Visit: https://redis.com/try-free/
   - Or: https://app.redislabs.com/#/sign-up

2. **Sign Up:**
   - Click "Get Started Free"
   - Use your email or sign in with Google/GitHub
   - No credit card required! βœ…

3. **Verify Email:**
   - Check your email for verification link
   - Click to verify your account

---

### Step 2: Create Your First Database

1. **After login, you'll see the dashboard**
   - Click "Create database" or "New database"

2. **Select Plan:**
   - Choose **"Free"** plan
   - Shows: 30MB storage, 30 connections
   - Perfect for OTPs! βœ…

3. **Configure Database:**
   ```
   Database Name: swiftops-otp
   Cloud Provider: AWS (or any)
   Region: Choose closest to your HuggingFace Space
           (US East is usually good)
   
   Port: 12345 (auto-assigned)
   Password: (auto-generated or set your own)
   ```

4. **Click "Activate database"**
   - Wait 30-60 seconds for provisioning

---

### Step 3: Get Your Connection Details

After database is created, you'll see the Configuration page.

**Click the "Connect" button** (next to "Connect using Redis CLI, Client, or Insight")

A popup will show your connection details:

```
Redis URL:
redis://default:YOUR_PASSWORD@redis-15438-c212.ap-south-1.ec2.cloud.redislabs.com:15438
```

**OR you'll see separate fields:**
```
Host: redis-15438-c212.ap-south-1.ec2.cloud.redislabs.com
Port: 15438
Password: abc123xyz456
Username: default
```

**If you see the full Redis URL - just copy it!** βœ…

**If you see separate fields, build the URL:**
```
redis://default:PASSWORD@HOST:PORT
```

**Example from your screenshot:**
```
redis://default:YOUR_PASSWORD@redis-15438-c212.ap-south-1.ec2.cloud.redislabs.com:15438
```

**Copy this URL!** You'll need it for HuggingFace.

---

### Step 4: Add to HuggingFace Space

1. **Go to your HuggingFace Space:**
   - https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE

2. **Click "Settings" tab**

3. **Scroll to "Variables and secrets"**

4. **Click "New secret":**
   ```
   Name: REDIS_URL
   Value: redis://default:YOUR_PASSWORD@YOUR_ENDPOINT:YOUR_PORT
   ```

5. **Click "Save"**

6. **Restart your Space:**
   - Click "Factory reboot" or just push new code
   - Space will automatically use Redis Cloud

---

### Step 5: Verify It's Working

1. **Check Space logs:**
   ```
   βœ… OTP Service using Redis storage (redis://default:***@redis-12345...)
   ```

2. **Check health endpoint:**
   ```bash
   curl https://YOUR_SPACE.hf.space/health
   ```
   
   Should show:
   ```json
   {
     "components": {
       "redis": {
         "status": "connected",
         "storage_type": "redis"
       }
     }
   }
   ```

3. **Test OTP:**
   - Try sending an OTP via your app
   - Should work instantly!

---

## Quick Reference

### Your Redis Cloud Dashboard
- URL: https://app.redislabs.com/
- View: Database metrics, memory usage, connections

### Connection String Format
```
redis://[username]:[password]@[host]:[port]/[db]

Default username: default
DB number: 0 (default, can omit)
```

### Example URLs

**Basic:**
```
redis://default:mypassword@redis-12345.c1.us-east-1.ec2.cloud.redislabs.com:12345
```

**With SSL (if enabled):**
```
rediss://default:mypassword@redis-12345.c1.us-east-1.ec2.cloud.redislabs.com:12345
```

---

## Troubleshooting

### "Connection refused"
- βœ… Check password is correct
- βœ… Check endpoint and port
- βœ… Ensure database is "Active" in Redis Cloud dashboard

### "Authentication failed"
- βœ… Password must be URL-encoded if it contains special characters
- βœ… Username is usually "default"

### "Memory full"
- βœ… Check usage in Redis Cloud dashboard
- βœ… OTPs auto-expire (5-30 minutes)
- βœ… 30MB = ~60,000 OTPs (should never fill up)

### Test connection locally:
```bash
# Install redis-cli
brew install redis  # macOS
sudo apt install redis-tools  # Ubuntu

# Test connection
redis-cli -u "redis://default:PASSWORD@HOST:PORT" ping
# Should return: PONG
```

---

## Security Best Practices

1. **Never commit REDIS_URL to git**
   - Always use environment variables
   - Already in `.gitignore` as `.env`

2. **Use HuggingFace Secrets**
   - Secrets are encrypted
   - Not visible in logs
   - Only accessible to your Space

3. **Rotate password periodically**
   - Redis Cloud β†’ Database β†’ Configuration β†’ Reset password
   - Update HuggingFace secret

4. **Monitor access**
   - Redis Cloud dashboard shows connections
   - Check for unusual activity

---

## Cost & Limits

### Free Tier (Forever Free)
- **Storage:** 30MB
- **Connections:** 30 concurrent
- **Bandwidth:** Unlimited
- **Uptime:** 99.99% SLA
- **Support:** Community

### When to Upgrade?
- If you exceed 30MB (unlikely for OTPs)
- If you need >30 concurrent connections
- If you want dedicated support

**For OTP service: Free tier is perfect!** βœ…

---

## Alternative: Local Redis for Development

If you want to test locally without Redis Cloud:

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

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

**Docker:**
```bash
docker run -d -p 6379:6379 redis:alpine
export REDIS_URL=redis://localhost:6379/0
```

---

## Summary Checklist

- [ ] Created Redis Cloud account
- [ ] Created free database (30MB)
- [ ] Copied connection URL
- [ ] Added `REDIS_URL` secret to HuggingFace Space
- [ ] Restarted Space
- [ ] Verified Redis connection in logs
- [ ] Tested OTP functionality

**Done!** Your OTP service is now using Redis Cloud. πŸŽ‰

---

## Need Help?

- **Redis Cloud Docs:** https://docs.redis.com/latest/rc/
- **Redis Cloud Support:** https://redis.com/company/support/
- **HuggingFace Docs:** https://huggingface.co/docs/hub/spaces-overview

---

**Total Setup Time:** ~5 minutes  
**Cost:** $0 (free forever)  
**Maintenance:** None (fully managed)