Resilience Patterns Tutorials
These tutorials guide you through building fault-tolerant applications with higher-kinded-j's resilience patterns.
Prerequisites
Before starting, you 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 (~12 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 (~12 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 (~12 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 in the solutions/ subdirectory. Try to complete the exercises on your own before checking the solutions.