-
Notifications
You must be signed in to change notification settings - Fork 1
Add graceful Redis failure handling with aggressive timeouts #10
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
bmcdorman
wants to merge
3
commits into
master
Choose a base branch
from
graceful-redis-failure
base: master
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
3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,36 +4,65 @@ import Redis, { RedisOptions } from 'ioredis'; | |
| import Selector from './model/Selector'; | ||
|
|
||
| class RedisCache implements Cache { | ||
| private static DEFAULT_TTL = 60 * 60 * 24 * 7; | ||
| // 1 hour TTL to limit stale data window if cache invalidation fails during Redis issues | ||
| private static DEFAULT_TTL = 60 * 60; | ||
|
|
||
| private redis_: Redis; | ||
|
|
||
| constructor(options: RedisOptions) { | ||
| this.redis_ = new Redis(options); | ||
| this.redis_ = new Redis({ | ||
| ...options, | ||
| connectTimeout: 500, // 500ms to connect | ||
| commandTimeout: 200, // 200ms per command | ||
| // retryStrategy controls retries for initial connection and reconnection attempts | ||
| // maxRetriesPerRequest controls retries for individual commands (GET, SET, etc) | ||
| retryStrategy: (times) => { | ||
| // Stop reconnection attempts after 2 tries | ||
| if (times > 2) return null; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this check needed if maxRetriesPerRequest is 0? |
||
| return 50; // Wait only 50ms between reconnection attempts | ||
| }, | ||
| maxRetriesPerRequest: 0, // Don't retry commands - fail immediately | ||
| enableOfflineQueue: false, // Don't queue commands when disconnected | ||
| }); | ||
|
|
||
| this.redis_.on('error', (err) => { | ||
| console.error('Redis error (app will continue without cache):', err.message); | ||
| }); | ||
| } | ||
|
|
||
| private static key_ = ({ collection, id }: Selector): string => { | ||
| return `${collection}/${id}`; | ||
| }; | ||
|
|
||
| async get(selector: Selector): Promise<object | null> { | ||
| const data = await this.redis_.get(RedisCache.key_(selector)); | ||
| if (!data) return null; | ||
|
|
||
| return JSON.parse(data); | ||
| try { | ||
| const data = await this.redis_.get(RedisCache.key_(selector)); | ||
| if (!data) return null; | ||
| return JSON.parse(data); | ||
| } catch (err) { | ||
| console.error('Redis GET failed, continuing without cache:', err); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| async set(selector: Selector, value: object | null): Promise<void> { | ||
| if (!value) { | ||
| // Cache-aside pattern: Only populate cache on reads, not writes | ||
| // This prevents stale data if cache write fails but DB write succeeds | ||
| // Instead, we just invalidate the cache entry on writes | ||
| try { | ||
| await this.redis_.del(RedisCache.key_(selector)); | ||
| return; | ||
| } catch (err) { | ||
| console.error('Redis cache invalidation failed, continuing without cache:', err); | ||
| // Best effort - if invalidation fails, TTL will eventually clear stale data | ||
| } | ||
|
|
||
| await this.redis_.setex(RedisCache.key_(selector), RedisCache.DEFAULT_TTL, JSON.stringify(value)); | ||
| } | ||
|
|
||
| async remove(selector: Selector): Promise<void> { | ||
| await this.redis_.del(RedisCache.key_(selector)); | ||
| try { | ||
| await this.redis_.del(RedisCache.key_(selector)); | ||
| } catch (err) { | ||
| console.error('Redis DEL failed, continuing without cache:', err); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
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
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.
Why is this check needed if maxRetriesPerRequest is 0?