Skip to content

feat: Implement Fallback design pattern #2846 - #3538

Merged
iluwatar merged 3 commits into
iluwatar:masterfrom
priyanshuvishwakarma273403:feat/fallback
Aug 16, 2026
Merged

feat: Implement Fallback design pattern #2846#3538
iluwatar merged 3 commits into
iluwatar:masterfrom
priyanshuvishwakarma273403:feat/fallback

Conversation

@priyanshuvishwakarma273403

Copy link
Copy Markdown
Contributor

What does this PR do?

This PR implements the Fallback design pattern as a standalone, self-contained Maven module (fallback). The implementation demonstrates a robust resiliency mechanism commonly used in microservices architectures.

Key Components Implemented:

  • RemoteService: A simple interface representing a service/dependency call that might fail.
  • PrimaryService: Implements RemoteService to simulate the main dependency with configurable latency, timeout, and exceptions.
  • FallbackService: Implements RemoteService and represents the fallback strategy, returning cached or degraded responses.
  • SimpleCircuitBreaker: A thread-safe state machine (CLOSED, OPEN, HALF_OPEN) that monitors service health to fast-fail subsequent requests when the primary service is down.
  • FallbackExecutor: Leverages Java 21's Virtual Threads to orchestrate the service call execution, applying timeouts and delegating to the fallback handler when failures, timeouts, or OPEN circuit states are encountered.
  • App: Main application illustrating different runtime scenarios and state transitions (healthy call, exception, timeout, fast-failing during OPEN, and recovery back to CLOSED).
  • Unit & Integration Tests: Fully verified using JUnit 5 (covering all state transitions, timeouts, short-circuits, and recovery). Passed spotless checks.

Fixes #2846

@github-actions

github-actions Bot commented Jul 5, 2026

Copy link
Copy Markdown

PR Summary

Adds a standalone Maven module fallback implementing the Fallback design pattern with a circuit breaker. Includes interfaces, concrete services, a simple circuit breaker, an executor that uses virtual threads, a demo App, and comprehensive tests. Also ships a README and updates the root pom.xml to include the new module. Demonstrates healthy, failing, timed, OPEN-state fast-fail, and recovery scenarios.

Changes

File Summary
fallback/README.md Introduces the Fallback pattern and its circuit-breaker–driven resilience in a standalone Maven module. Documents components (RemoteService, PrimaryService, FallbackService, SimpleCircuitBreaker, FallbackExecutor), a demo App, and tests, with scenario explanations and usage guidance.
fallback/pom.xml Defines the fallback Maven module, its dependencies (slf4j-api, logback-classic, JUnit Jupiter Engine), and assembly configuration for a runnable App. Establishes parent-child relation and marks the module as test-enabled.
fallback/src/main/java/com/iluwatar/fallback/App.java Demo application orchestrating healthy, failing, timeout, OPEN circuit, and recovery scenarios using FallbackExecutor, PrimaryService, FallbackService, and SimpleCircuitBreaker.
fallback/src/main/java/com/iluwatar/fallback/FallbackExecutor.java Executes primary service calls with timeout handling, observes circuit breaker state, and delegates to the fallback when primary fails, times out, or circuit is OPEN. Uses a virtual-thread executor and implements AutoCloseable.
fallback/src/main/java/com/iluwatar/fallback/FallbackService.java Concrete FallbackService returning a predefined degraded response when the primary fails or times out.
fallback/src/main/java/com/iluwatar/fallback/PrimaryService.java Simulates the main dependency with configurable latency and exception throwing to test resilience features.
fallback/src/main/java/com/iluwatar/fallback/RemoteService.java Interface representing a potentially failing external service with an execute() method.
fallback/src/main/java/com/iluwatar/fallback/SimpleCircuitBreaker.java Thread-safe state machine with CLOSED, OPEN, and HALF_OPEN states, tracking failures and retry windows.
fallback/src/test/java/com/iluwatar/fallback/AppTest.java Ensures the App main method runs without throwing exceptions.
fallback/src/test/java/com/iluwatar/fallback/FallbackPatternTest.java JUnit tests cover healthy calls, failures, timeouts, OPEN fast-fail, and recovery to CLOSED, validating circuit-breaker behavior.
pom.xml Adds the fallback module to the root Maven build, enabling building and testing of the new pattern module.

autogenerated by presubmit.ai

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM!

Review Summary

Commits Considered (2)
  • ab58c76: docs: add README for Fallback design pattern implementation
  • 488c4d7: feat: implement Fallback design pattern with circuit breaker and service monitoring
Files Processed (11)
  • fallback/README.md (1 hunk)
  • fallback/pom.xml (1 hunk)
  • fallback/src/main/java/com/iluwatar/fallback/App.java (1 hunk)
  • fallback/src/main/java/com/iluwatar/fallback/FallbackExecutor.java (1 hunk)
  • fallback/src/main/java/com/iluwatar/fallback/FallbackService.java (1 hunk)
  • fallback/src/main/java/com/iluwatar/fallback/PrimaryService.java (1 hunk)
  • fallback/src/main/java/com/iluwatar/fallback/RemoteService.java (1 hunk)
  • fallback/src/main/java/com/iluwatar/fallback/SimpleCircuitBreaker.java (1 hunk)
  • fallback/src/test/java/com/iluwatar/fallback/AppTest.java (1 hunk)
  • fallback/src/test/java/com/iluwatar/fallback/FallbackPatternTest.java (1 hunk)
  • pom.xml (1 hunk)
Actionable Comments (0)
Skipped Comments (7)
  • fallback/src/main/java/com/iluwatar/fallback/FallbackExecutor.java [46-46]

    possible issue: "Handle potential null responses from primary service."

  • fallback/src/main/java/com/iluwatar/fallback/FallbackExecutor.java [66-66]

    behavior: "Circuit breaker fast-fail path."

  • fallback/src/main/java/com/iluwatar/fallback/FallbackExecutor.java [81-81]

    best-practice: "Timeout handling."

  • fallback/src/main/java/com/iluwatar/fallback/FallbackExecutor.java [86-86]

    best-practice: "Cancel task on timeout to free resources."

  • fallback/src/main/java/com/iluwatar/fallback/FallbackExecutor.java [94-94]

    maintainability: "Narrow exception handling in executor."

  • fallback/src/main/java/com/iluwatar/fallback/SimpleCircuitBreaker.java [69-69]

    design: "Circuit breaker state transitions."

  • fallback/pom.xml [47-47]

    best-practice: "Test dependencies: add junit-jupiter-api for API usage in tests."

@codecov

codecov Bot commented Jul 5, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.96491% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.34%. Comparing base (f1dcddf) to head (ea12c2b).

Files with missing lines Patch % Lines
...n/java/com/iluwatar/fallback/FallbackExecutor.java 71.87% 9 Missing ⚠️
...va/com/iluwatar/fallback/SimpleCircuitBreaker.java 87.50% 2 Missing and 2 partials ⚠️
...lback/src/main/java/com/iluwatar/fallback/App.java 91.66% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3538      +/-   ##
============================================
- Coverage     83.35%   83.34%   -0.01%     
- Complexity     4087     4107      +20     
============================================
  Files          1073     1078       +5     
  Lines         14414    14528     +114     
  Branches        687      691       +4     
============================================
+ Hits          12015    12109      +94     
- Misses         2113     2128      +15     
- Partials        286      291       +5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🚨 Pull request needs attention.

Review Summary

Commits Considered (1)
  • ea12c2b: Merge branch 'master' into feat/fallback
Files Processed (2)
  • fallback/README.md (1 hunk)
  • pom.xml (1 hunk)
Actionable Comments (1)
  • pom.xml [255-255]

    maintainability: "Add new Maven module to aggregator"

Skipped Comments (2)
  • fallback/README.md [2-11]

    maintainability: "Validate YAML front matter formatting"

  • fallback/README.md [50-57]

    readability: "Use explicit types or Java 10+ 'var' safety in code samples"

Comment thread pom.xml
<module>backpressure</module>
<module>actor-model</module>
<module>rate-limiting-pattern</module>
<module>fallback</module>

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

<module>fallback</module>

@iluwatar
iluwatar merged commit 7e0ddb0 into iluwatar:master Aug 16, 2026
3 of 4 checks passed
@iluwatar

Copy link
Copy Markdown
Owner

@all-contributors please add @priyanshuvishwakarma273403 for code

@allcontributors

Copy link
Copy Markdown
Contributor

@iluwatar

I've put up a pull request to add @priyanshuvishwakarma273403! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Fallback design pattern

2 participants