Resilience Patterns Tutorials
These tutorials guide us through building fault-tolerant applications with higher-kinded-j's resilience patterns.
The One Line, Six Layers anchor uses Either short-circuiting to handle a single failure cleanly. This journey covers the patterns we reach for once one failure is not the whole story: retries, timeouts, downstream-protection, multi-step compensations. Reference material lives under Resilience Patterns in the Effect chapter.
Prerequisites
Before starting, we should be comfortable with:
VTaskbasics (creating, running, composing)- The Effect Path API (
VTaskPath,VStreamPath) - Basic error handling (
handleError,recover)
Tutorial Track
Tutorial 1: Circuit Breaker (~10 minutes)
Learn to protect services from cascading failures with the circuit breaker pattern.
Exercises:
- Create a circuit breaker with custom configuration
- Protect a VTask with the circuit breaker
- Observe the circuit opening after threshold failures
- Handle
CircuitOpenException - Use
protectWithFallbackfor graceful degradation - Monitor circuit breaker metrics
File: Tutorial01_CircuitBreaker.java
Tutorial 2: Saga (~10 minutes)
Learn to coordinate multi-step operations with automatic compensation on failure.
Exercises:
- Create a simple saga with compensation
- Chain saga steps with dependent data
- Verify compensation on failure
- Use
SagaBuilderfor complex workflows - Handle saga errors with
runSafe()
File: Tutorial02_Saga.java
Tutorial 3: Retry, Bulkhead & Combined Resilience (~10 minutes)
Learn VTask-native retry, concurrency limiting, and combining multiple patterns.
Exercises:
- Use
Retry.retryTaskwith aRetryPolicy - Monitor retries with
RetryEvent - Create a
Bulkheadto limit concurrency - Compose patterns with
ResilienceBuilder - Use convenience methods from
Resilience
File: Tutorial03_RetryBulkheadResilience.java
Tutorial 4: Path API Resilience (~10 minutes)
Learn to use resilience patterns through the fluent Path API.
Exercises:
VTaskPath.withRetry()and.retry()convenienceVTaskPath.catching(),.asMaybe(),.asTry()typed error wrappingVTaskPath.withCircuitBreaker()in a fluent chainVStreamPath.recover()and.onError()stream error handlingVStreamPath.mapTask()with per-element retryVTaskContextLayer 2 resilience:.retry(),.withCircuitBreaker()
File: Tutorial04_PathResilience.java
Running Tutorials
# Run tutorial exercises (expected to fail until completed)
./gradlew :hkj-examples:tutorialTest
# Run solutions to verify they work
./gradlew :hkj-examples:test
Solutions
Each tutorial has a corresponding solution file under hkj-examples/src/test/java/org/higherkindedj/tutorial/solutions/resilience/. Try to complete the exercises on your own before checking the solutions.
| Tutorial | Solution File |
|---|---|
Tutorial01_CircuitBreaker.java | solutions/resilience/Tutorial01_CircuitBreaker_Solution.java |
Tutorial02_Saga.java | solutions/resilience/Tutorial02_Saga_Solution.java |
Tutorial03_RetryBulkheadResilience.java | solutions/resilience/Tutorial03_RetryBulkheadResilience_Solution.java |
Tutorial04_PathResilience.java | solutions/resilience/Tutorial04_PathResilience_Solution.java |