-
Notifications
You must be signed in to change notification settings - Fork 65
perf(redis): resilience, Redis-backed rate limiter, and health metrics (#278) #296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Abhushan187
wants to merge
2
commits into
geturbackend:main
Choose a base branch
from
Abhushan187:perf/redis-resilience-rate-limiter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| const express = require('express'); | ||
| const { getHealth } = require('../controllers/health.controller'); | ||
| const { getHealth, getRedisHealth } = require('../controllers/health.controller'); | ||
|
|
||
| const router = express.Router(); | ||
|
|
||
| router.get('/', getHealth); | ||
| router.get('/redis', getRedisHealth); | ||
|
|
||
| module.exports = router; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| const autocannon = require('autocannon'); | ||
| const { redis } = require('@urbackend/common'); | ||
|
|
||
| const TARGET_URL = process.env.TARGET_URL || 'http://localhost:1235'; | ||
| const API_KEY = process.env.TEST_API_KEY || 'test-api-key'; | ||
|
|
||
| async function runRateLimitLoadTest() { | ||
| console.log('🚀 Starting rate limit load test...\n'); | ||
|
|
||
| // Warm up: clear any existing rate limit keys | ||
| const keys = await redis.keys('rl:project:*'); | ||
| if (keys.length) await redis.del(...keys); | ||
| console.log(`Cleared ${keys.length} existing rate limit keys\n`); | ||
|
|
||
| const instance = autocannon({ | ||
| url: `${TARGET_URL}/api/data`, | ||
| connections: 100, | ||
| duration: 30, | ||
| headers: { 'x-api-key': API_KEY }, | ||
| requests: [{ method: 'GET' }], | ||
| }); | ||
|
|
||
| autocannon.track(instance, { renderProgressBar: true }); | ||
|
|
||
| const result = await instance; | ||
| console.log('\n📊 Results:'); | ||
| console.log(` 2xx: ${result['2xx']}`); | ||
| console.log(` 429: ${result['429']}`); | ||
| console.log(` Mean latency: ${result.latency.mean}ms`); | ||
| console.log(` P99 latency: ${result.latency.p99}ms`); | ||
| console.log(` Errors: ${result.errors}`); | ||
|
|
||
| // Verify Redis keys were created | ||
| const rlKeys = await redis.keys('rl:project:*'); | ||
| console.log(`\n🔑 Redis rate limit keys created: ${rlKeys.length}`); | ||
| } | ||
|
|
||
| if (require.main === module) { | ||
| runRateLimitLoadTest().catch((err) => { | ||
| console.error('Load test failed:', err); | ||
| process.exit(1); | ||
| }); | ||
| } | ||
|
|
||
| module.exports = { runRateLimitLoadTest }; |
34 changes: 34 additions & 0 deletions
34
apps/public-api/tests/resilience/redis-failure.resilience.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| const request = require('supertest'); | ||
| const app = require('../../src/app'); | ||
| const { redis } = require('@urbackend/common'); | ||
|
|
||
| describe('Redis resilience', () => { | ||
| afterEach(async () => { | ||
| if (redis.status === 'ready') await redis.flushdb(); | ||
| }); | ||
|
Abhushan187 marked this conversation as resolved.
|
||
|
|
||
| test('health endpoint returns 503 when Redis is unreachable', async () => { | ||
| // Simulate Redis failure by disconnecting temporarily | ||
| await redis.disconnect(); | ||
|
|
||
| const res = await request(app).get('/api/health/redis'); | ||
| expect(res.status).toBe(503); | ||
| expect(res.body.redis.connected).toBe(false); | ||
|
|
||
| // Reconnect for subsequent tests | ||
| await redis.connect(); | ||
| }); | ||
|
Abhushan187 marked this conversation as resolved.
|
||
|
|
||
| test('public API endpoints degrade gracefully when Redis is down', async () => { | ||
| await redis.disconnect(); | ||
|
|
||
| const res = await request(app) | ||
| .get('/api/data') | ||
| .set('x-api-key', 'test-key'); | ||
|
|
||
| // Should not crash; may return 401 or 500 depending on auth flow | ||
| expect([200, 401, 403, 500]).toContain(res.status); | ||
|
|
||
|
Abhushan187 marked this conversation as resolved.
|
||
| await redis.connect(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: geturbackend/urBackend
Length of output: 152
🌐 Web query:
rate-limit-redis 3.1.0 vs 4.0.2 breaking changes compatibility ioredis 5.x💡 Result:
The rate-limit-redis package maintains compatibility with ioredis 5.x throughout the v3 and v4 release cycles [1][2]. Transitioning from v3.1.0 to v4.0.2 involves several breaking changes and architectural shifts [3][4]. Breaking Changes (v3.1.0 to v4.0.2) 1. Node.js Support: Version 4.0.0 and later dropped support for Node.js v14 [3][4]. Users must be on Node.js 16 or higher [5][2]. 2. Dependency Updates: Version 4.0.0 introduced support for express-rate-limit v7 [3][4]. This often necessitates updating the express-rate-limit middleware alongside the store [6]. 3. Packaging Changes: Version 4.0.0 switched to using pkgroll for bundling, which may affect how the library is resolved or tree-shaken in some build environments [3][4]. Compatibility with ioredis 5.x ioredis 5.x remains fully compatible with both v3.1.0 and v4.x.x [1][2]. The integration pattern for ioredis relies on the sendCommand option, which has remained consistent [1][2]. For both versions, you should configure the store as follows [1][2]: new RedisStore({ sendCommand: (...args) => client.call(...args), }) Note that while the configuration pattern is stable, later versions (4.x and 5.x) have improved internal script handling and bug fixes, such as the resolution of race conditions affecting hit counters and TTLs [7]. Always ensure that each rate limiter instance uses its own new RedisStore instance to prevent counter collisions [6].
Citations:
🏁 Script executed:
Repository: geturbackend/urBackend
Length of output: 152
🌐 Web query:
rate-limit-redis 3.1.0 security vulnerability advisory💡 Result:
There are no known security vulnerability advisories specifically for the
rate-limit-redispackage version 3.1.0 [1]. It is possible that you may be conflating this package with a vulnerability in its core dependency,express-rate-limit. Specifically, a high-severity vulnerability (CVE-2026-30827) was recently identified inexpress-rate-limit(versions 8.0.0 through 8.2.1), which causes IPv4-mapped IPv6 addresses to share a single rate-limit bucket on dual-stack servers [2][3]. This issue has been patched in newer versions ofexpress-rate-limit[3][4]. If you are usingrate-limit-redis, ensure that your dependencies are up to date. The latest releases ofrate-limit-redis(as of mid-2026) requireexpress-rate-limitversion 8.5.0 or higher [1]. Always verify security advisories against the specific package name, as vulnerability scanning tools may sometimes flag dependencies or misattribute issues [5][6].Citations:
🌐 Web query:
rate-limit-redis 4.0.2 security vulnerability advisory💡 Result:
There are no known security vulnerabilities specifically targeting the rate-limit-redis package itself [1]. However, the rate-limit-redis package is a store for the express-rate-limit middleware, and it is important to note that a vulnerability has been identified in the parent middleware, express-rate-limit, which may affect applications using it [2][3][4]. The vulnerability, CVE-2026-30827, affects express-rate-limit versions 8.0.0 through 8.2.1 [3][4]. It involves an issue where IPv4-mapped IPv6 addresses are handled incorrectly on dual-stack network servers, potentially allowing a single client to exhaust the shared rate limit bucket and trigger a denial-of-service for all IPv4 clients [3][4]. If your application uses express-rate-limit within this vulnerable range, it is recommended to upgrade express-rate-limit to version 8.0.2, 8.1.1, 8.2.2, or 8.3.0 and higher [3][4]. You should ensure that your project's dependencies are updated accordingly [3]. For ongoing monitoring, you can check for updates to your project's dependencies using tools like npm audit or security scanning platforms [2][1].
Citations:
Clarify
rate-limit-redisdowngrade risk and prioritize the real security fixrate-limit-redis@3.1.0; upstream indicatesrate-limit-redisstays compatible withioredis@5.xacross the v3/v4 line (when using the standardsendCommand/client.call(...)integration).express-rate-limit@8.2.1(CVE-2026-30827; affects8.0.0–8.2.1), so upgradingexpress-rate-limitto a patched release is required regardless of therate-limit-redisversion.rate-limit-redis@3.1.0,express-rate-limit@8.2.1, andioredis@5.10.0(peer-dependency/integration expectations).🤖 Prompt for AI Agents