Repository navigation
Expand file tree
/
Copy pathredis-token-bucket-example.js
More file actions
284 lines (225 loc) · 8.51 KB
/
Copy pathredis-token-bucket-example.js
File metadata and controls
284 lines (225 loc) · 8.51 KB
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
/**
* Redis Token Bucket Example
*
* Demonstrates distributed rate limiting using Redis backend.
* This allows rate limiting to work across multiple server instances.
*
* Prerequisites:
* 1. Install Redis: https://redis.io/download
* 2. Install ioredis: npm install ioredis
* 3. Start Redis server: redis-server
*/
const Redis = require('ioredis');
const RedisTokenBucket = require('../../src/algorithms/javascript/redis-token-bucket');
// Example 1: Basic Usage
console.log('='.repeat(70));
console.log('Example 1: Basic Redis Token Bucket Usage');
console.log('='.repeat(70));
async function basicExample() {
// Connect to Redis
const redis = new Redis({
host: 'localhost',
port: 6379,
retryStrategy: (times) => {
const delay = Math.min(times * 50, 2000);
return delay;
}
});
// Create rate limiter for a specific user
const limiter = new RedisTokenBucket(
redis,
'rate_limit:user:alice',
10, // 10 tokens capacity
1 // 1 token per second refill
);
console.log('\nSimulating API requests for user Alice:\n');
// Make some requests
for (let i = 1; i <= 12; i++) {
const allowed = await limiter.allowRequest();
const state = await limiter.getState();
console.log(`Request ${i}: ${allowed ? '✅ ALLOWED' : '❌ REJECTED'} | ` +
`Tokens: ${state.availableTokens}`);
if (!allowed) {
const waitTime = await limiter.getTimeUntilNextToken();
console.log(` ⏰ Wait ${Math.ceil(waitTime)}ms for next token`);
}
}
await redis.quit();
}
// Example 2: Multi-Server Scenario
console.log('\n' + '='.repeat(70));
console.log('Example 2: Multi-Server Distributed Rate Limiting');
console.log('='.repeat(70));
async function multiServerExample() {
// Simulate two server instances
const redis1 = new Redis({ host: 'localhost', port: 6379 });
const redis2 = new Redis({ host: 'localhost', port: 6379 });
// Both servers use the same key = shared state
const server1Limiter = new RedisTokenBucket(redis1, 'api:shared', 20, 2);
const server2Limiter = new RedisTokenBucket(redis2, 'api:shared', 20, 2);
console.log('\nSimulating requests across 2 server instances:\n');
// Requests come to different servers but share the same limit
console.log('Server 1: Processing 10 requests');
for (let i = 0; i < 10; i++) {
await server1Limiter.allowRequest();
}
const state1 = await server1Limiter.getState();
console.log(`Server 1 view: ${state1.availableTokens} tokens remaining\n`);
console.log('Server 2: Processing 10 requests');
for (let i = 0; i < 10; i++) {
await server2Limiter.allowRequest();
}
const state2 = await server2Limiter.getState();
console.log(`Server 2 view: ${state2.availableTokens} tokens remaining\n`);
// Next request should be rejected on either server
const allowed1 = await server1Limiter.allowRequest();
const allowed2 = await server2Limiter.allowRequest();
console.log(`Server 1 next request: ${allowed1 ? '✅ ALLOWED' : '❌ REJECTED'}`);
console.log(`Server 2 next request: ${allowed2 ? '✅ ALLOWED' : '❌ REJECTED'}`);
await redis1.quit();
await redis2.quit();
}
// Example 3: Per-User Rate Limiting
console.log('\n' + '='.repeat(70));
console.log('Example 3: Per-User Rate Limiting');
console.log('='.repeat(70));
async function perUserExample() {
const redis = new Redis({ host: 'localhost', port: 6379 });
class APIServer {
constructor(redis) {
this.redis = redis;
this.limiters = new Map();
}
getUserLimiter(userId, tier = 'free') {
const key = `api:${tier}:${userId}`;
if (!this.limiters.has(key)) {
// Different tiers have different limits
const limits = {
free: { capacity: 10, refillRate: 1 },
pro: { capacity: 100, refillRate: 10 },
enterprise: { capacity: 1000, refillRate: 100 }
};
const config = limits[tier];
this.limiters.set(key, new RedisTokenBucket(
this.redis,
key,
config.capacity,
config.refillRate
));
}
return this.limiters.get(key);
}
async handleRequest(userId, tier) {
const limiter = this.getUserLimiter(userId, tier);
const allowed = await limiter.allowRequest();
const state = await limiter.getState();
return {
allowed,
remaining: state.availableTokens,
tier
};
}
}
const server = new APIServer(redis);
console.log('\nSimulating requests from different user tiers:\n');
// Free tier user
const freeResult = await server.handleRequest('user_1', 'free');
console.log(`Free user: ${freeResult.allowed ? '✅' : '❌'} | Remaining: ${freeResult.remaining}`);
// Pro tier user
const proResult = await server.handleRequest('user_2', 'pro');
console.log(`Pro user: ${proResult.allowed ? '✅' : '❌'} | Remaining: ${proResult.remaining}`);
// Enterprise tier user
const entResult = await server.handleRequest('user_3', 'enterprise');
console.log(`Enterprise: ${entResult.allowed ? '✅' : '❌'} | Remaining: ${entResult.remaining}`);
await redis.quit();
}
// Example 4: Cost-Based Operations
console.log('\n' + '='.repeat(70));
console.log('Example 4: Cost-Based Rate Limiting');
console.log('='.repeat(70));
async function costBasedExample() {
const redis = new Redis({ host: 'localhost', port: 6379 });
const limiter = new RedisTokenBucket(redis, 'api:operations', 100, 10);
const operations = [
{ name: 'Read', cost: 1 },
{ name: 'Write', cost: 5 },
{ name: 'Delete', cost: 10 },
{ name: 'Bulk Import', cost: 50 }
];
console.log('\nProcessing operations with different costs:\n');
for (const op of operations) {
const allowed = await limiter.allowRequest(op.cost);
const state = await limiter.getState();
console.log(`${op.name} (cost: ${op.cost}): ${allowed ? '✅ ALLOWED' : '❌ REJECTED'} | ` +
`Tokens: ${state.availableTokens}`);
}
await redis.quit();
}
// Example 5: Health Check and Failover
console.log('\n' + '='.repeat(70));
console.log('Example 5: Health Check and Error Handling');
console.log('='.repeat(70));
async function healthCheckExample() {
const redis = new Redis({
host: 'localhost',
port: 6379,
retryStrategy: () => null // Don't retry for this demo
});
const limiter = new RedisTokenBucket(redis, 'health:check', 10, 1);
// Check Redis health
const healthy = await limiter.isHealthy();
console.log(`\nRedis Status: ${healthy ? '✅ Connected' : '❌ Disconnected'}`);
if (healthy) {
console.log('Processing requests normally...');
const allowed = await limiter.allowRequest();
console.log(`Request: ${allowed ? '✅ ALLOWED' : '❌ REJECTED'}`);
}
// Simulate Redis disconnection
await redis.disconnect();
console.log('\n⚠️ Simulating Redis failure...\n');
const stillHealthy = await limiter.isHealthy();
console.log(`Redis Status: ${stillHealthy ? '✅ Connected' : '❌ Disconnected'}`);
// Limiter fails open (allows requests) on errors
const allowedOnError = await limiter.allowRequest();
console.log(`Request during failure: ${allowedOnError ? '✅ ALLOWED (fail-open)' : '❌ REJECTED'}`);
console.log('\n💡 Tip: Fail-open strategy prevents complete outage when Redis is down');
}
// Example 6: Cleanup and Management
console.log('\n' + '='.repeat(70));
console.log('Example 6: Bucket Management');
console.log('='.repeat(70));
async function managementExample() {
const redis = new Redis({ host: 'localhost', port: 6379 });
const limiter = new RedisTokenBucket(redis, 'managed:bucket', 10, 1);
// Use some tokens
await limiter.allowRequest(7);
console.log('\nAfter consuming 7 tokens:');
console.log(await limiter.getState());
// Reset bucket
await limiter.reset();
console.log('\nAfter reset:');
console.log(await limiter.getState());
// Delete bucket
await limiter.delete();
console.log('\nBucket deleted from Redis ✅');
await redis.quit();
}
// Run all examples
(async () => {
try {
await basicExample();
await multiServerExample();
await perUserExample();
await costBasedExample();
await healthCheckExample();
await managementExample();
console.log('\n' + '='.repeat(70));
console.log('✅ All examples completed successfully!');
console.log('='.repeat(70));
} catch (error) {
console.error('\n❌ Error:', error.message);
console.error('\n💡 Make sure Redis is running: redis-server');
console.error('💡 Install ioredis: npm install ioredis');
process.exit(1);
}
})();