Skip to content

Commit 073d7bc

Browse files
authored
Merge pull request #116 from JerryIdoko/feature/redis-scaling-171-85
Pull Request: Setup Redis for Distributed Rate Limiting & Caching (#171, #85)
2 parents 98fa97e + e137128 commit 073d7bc

7 files changed

Lines changed: 327 additions & 42 deletions

File tree

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ POOL_ADDRESS="CC..." # Replace with your Pool Contract ID
1414
INDEXER_POLL_INTERVAL=5000 # Polling interval in ms
1515
WS_PORT=3001 # WebSocket server port
1616

17+
# Redis Configuration
18+
REDIS_URL="redis://localhost:6379"
19+
1720
# Security Configuration
1821
JWT_SECRET="YOUR_SUPER_SECRET_KEY_CHANGE_ME"
1922
ADMIN_PASSWORD="admin_secure_password_123"

config/redis.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const Redis = require('ioredis');
2+
3+
// Load environment variables if needed for standalone scripts
4+
if (!process.env.REDIS_URL) {
5+
require('dotenv').config();
6+
}
7+
8+
const REDIS_URL = process.env.REDIS_URL || 'redis://localhost:6379';
9+
10+
/**
11+
* Initializes and exports a centralized Redis client connection.
12+
* Used for distributed rate limiting and caching across multiple API instances.
13+
*/
14+
const redis = new Redis(REDIS_URL, {
15+
maxRetriesPerRequest: null,
16+
retryStrategy: (times) => {
17+
const delay = Math.min(times * 50, 2000);
18+
return delay;
19+
}
20+
});
21+
22+
redis.on('connect', () => {
23+
console.log('✅ Connected to Redis successfully');
24+
});
25+
26+
redis.on('error', (err) => {
27+
console.error('❌ Redis Connection Error:', err.message);
28+
});
29+
30+
module.exports = redis;

0 commit comments

Comments
 (0)