- 
                Notifications
    You must be signed in to change notification settings 
- Fork 0
Redis failover detection on app launch #361
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
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
  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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import RedisModule from "ioredis"; | ||
| import { ValidLoggers } from "./types.js"; | ||
|  | ||
| type RedisMode = "read-write" | "read-only" | "down"; | ||
|  | ||
| async function checkRedisMode(url: string): Promise<RedisMode> { | ||
| let testModule: RedisModule.default | null = null; | ||
| try { | ||
| testModule = new RedisModule.default(url); | ||
|  | ||
| // Test connectivity | ||
| await testModule.ping(); | ||
|  | ||
| // Test write capability | ||
| const testKey = `health-check:${Date.now()}`; | ||
| try { | ||
| await testModule.set(testKey, "test", "EX", 1); | ||
| return "read-write"; | ||
| } catch (writeError) { | ||
| return "read-only"; | ||
| } | ||
| } catch (error) { | ||
| return "down"; | ||
| } finally { | ||
| if (testModule) { | ||
| await testModule.quit().catch(() => {}); | ||
| } | ||
| } | ||
| } | ||
|  | ||
| export async function createRedisModule( | ||
| primaryUrl: string, | ||
| fallbackUrl: string, | ||
| logger: ValidLoggers, | ||
| ) { | ||
| const primaryMode = await checkRedisMode(primaryUrl); | ||
| if (Math.random() < 0.01) { | ||
| // Upstash will complain if we never hit the instance | ||
| // Fire a hit every so often on the fallback | ||
| // Don't block on it | ||
| // Given we create a client once ever hour, we should hit the node at least once every 4 days at least | ||
| checkRedisMode(fallbackUrl); | ||
| } | ||
| if (primaryMode === "read-write") { | ||
| logger.info("Using primary Redis in read-write mode"); | ||
| return new RedisModule.default(primaryUrl); | ||
| } | ||
|  | ||
| const fallbackMode = await checkRedisMode(fallbackUrl); | ||
|  | ||
| if (fallbackMode === "read-write") { | ||
| logger.warn( | ||
| `Primary Redis is ${primaryMode}, using fallback in read-write mode`, | ||
| ); | ||
| return new RedisModule.default(fallbackUrl); | ||
| } | ||
|  | ||
| if (primaryMode === "read-only") { | ||
| logger.warn( | ||
| "Both Redis instances are read-only. Using primary in read-only mode", | ||
| ); | ||
| return new RedisModule.default(primaryUrl); | ||
| } | ||
|  | ||
| if (fallbackMode === "read-only") { | ||
| logger.error( | ||
| "Primary Redis is down and Fallback Redis is down, using primary anyway", | ||
| ); | ||
| return new RedisModule.default(primaryUrl); | ||
| } | ||
|  | ||
| logger.error( | ||
| "Both primary and fallback Redis instances are down. Creating client on primary anyway.", | ||
| ); | ||
| return new RedisModule.default(primaryUrl); | ||
| } | ||
  
    
      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
    
  
  
    
              
      
      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.
Fix incorrect log message.
The log message states "Fallback Redis is down" but this branch is entered when
fallbackMode === "read-only", not "down". This will mislead engineers during incidents when they're trying to understand the Redis failover state.Apply this diff:
if (fallbackMode === "read-only") { logger.error( - "Primary Redis is down and Fallback Redis is down, using primary anyway", + "Primary Redis is down and fallback is read-only; failing with primary to surface write failures immediately", ); return new RedisModule.default(primaryUrl); }📝 Committable suggestion
🤖 Prompt for AI Agents