Skip to content

feat: add redis fallback mechanism #21043#21044

Merged
crazywoola merged 2 commits intolanggenius:mainfrom
NeatGuyCoding:more_resilience_redis
Jul 10, 2025
Merged

feat: add redis fallback mechanism #21043#21044
crazywoola merged 2 commits intolanggenius:mainfrom
NeatGuyCoding:more_resilience_redis

Conversation

@NeatGuyCoding
Copy link
Copy Markdown
Contributor

@NeatGuyCoding NeatGuyCoding commented Jun 16, 2025

Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue and you have been assigned to it
  3. Use the correct syntax to link this PR: Fixes #21043.

Summary

This PR introduces a Redis fallback mechanism to improve the resilience of the Dify application when Redis operations fail. The implementation includes:

  • New redis_fallback decorator: A decorator function that gracefully handles Redis operation exceptions and returns configurable default values when Redis is unavailable
  • Enhanced error handling: Applied the fallback mechanism to critical account service methods that depend on Redis for rate limiting functionality
  • Logging integration: Added proper logging to track Redis operation failures for monitoring and debugging purposes

Key Changes:

  • Added redis_fallback decorator in ext_redis.py to handle RedisError exceptions
  • Applied the decorator to rate limiting methods in account_service.py:
    • add_login_error_rate_limit()
    • is_login_error_rate_limit()
    • reset_login_error_rate_limit()
    • add_forgot_password_error_rate_limit()
  • Included comprehensive unit tests to ensure the fallback mechanism works correctly

This enhancement ensures that authentication and rate limiting features continue to function (with degraded capability) even when Redis is temporarily unavailable, preventing complete service disruption.

Fix #21043

Screenshots

Before After
Redis failures would cause unhandled exceptions and potential service disruption Redis failures are gracefully handled with fallback behavior and proper logging

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. 💪 enhancement New feature or request 📚 documentation Improvements or additions to documentation labels Jun 16, 2025
@crazywoola crazywoola requested review from Copilot and zhangx1n June 18, 2025 05:58
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a Redis fallback capability to prevent service disruptions when Redis is unavailable by introducing a decorator and applying it to rate‐limiting methods, along with tests for the decorator.

  • Introduces redis_fallback decorator in ext_redis.py for handling RedisError with configurable defaults and logging
  • Applies the decorator to key rate‐limiting methods in account_service.py
  • Adds unit tests for the fallback decorator behavior in test_redis.py

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
api/extensions/ext_redis.py Adds redis_fallback decorator with logging of failures
api/services/account_service.py Decorates rate‐limit methods with Redis fallback logic
api/tests/unit_tests/extensions/test_redis.py Introduces tests to validate the decorator functionality
Comments suppressed due to low confidence (1)

api/services/account_service.py:498

  • Add unit tests for this decorated method to confirm that it returns None and logs appropriately when Redis operations throw an error.
    @redis_fallback(default_return=None)

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Jul 10, 2025
@crazywoola crazywoola merged commit 6f8c7a6 into langgenius:main Jul 10, 2025
6 checks passed
@NeatGuyCoding NeatGuyCoding deleted the more_resilience_redis branch July 10, 2025 07:31
@dosubot dosubot bot mentioned this pull request Jul 11, 2025
5 tasks
Scorpion1221 added a commit to yybht155/dify that referenced this pull request Jul 16, 2025
* commit '390e4cc0bf203a979e13694ed99ec9fda6b42cc8': (143 commits)
  chore(version): bump to 1.6.0 (langgenius#22136)
  chore: fix schema editor can not hover item (langgenius#22155)
  fix: add the default value to the dark icon (langgenius#22149)
  feat: support ping method for MCP server (langgenius#22144)
  fix: mcp server card button display (langgenius#22141)
  feat: add support for dark icons in provider and tool entities (langgenius#22081)
  update worklow events logs. (langgenius#19871)
  chore: translate i18n files (langgenius#22132)
  feat: the frontend part of mcp (langgenius#22131)
  feat: add MCP support (langgenius#20716)
  Fix: Resolve issue with json_output (langgenius#22053)
  Chore: rm useless import and vars (langgenius#22108)
  feat: add redis fallback mechanism langgenius#21043 (langgenius#21044)
  optimize: batch embedding and qdrant write_consistency_factor parameter (langgenius#21776)
  chore: Update theme vars (langgenius#22113)
  feat(question-classifier): add drag-and-drop sorting for topics list (langgenius#22066)
  test: add comprehensive unit tests for encrypter module (langgenius#22102)
  fix: allow update plugin install settings (langgenius#22111)
  fix(Drawer): add overflow hidden to ensure copy button is always clickable (langgenius#21992) (langgenius#22103)
  Update clean_document_task.py (langgenius#22090)
  ...

# Conflicts:
#	api/core/helper/code_executor/template_transformer.py
#	api/pyproject.toml
#	api/uv.lock
tutkun pushed a commit to tutkun/dify that referenced this pull request Aug 15, 2025
Co-authored-by: tech <cto@sb>
Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

📚 documentation Improvements or additions to documentation 💪 enhancement New feature or request lgtm This PR has been approved by a maintainer size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

More resilient services even if redis failed

4 participants