examples/resilience-failsafe/README.md
Failsafe's retry, timeout, and fallback strategies can be used to make the cache operations resilient to intermittent failures.
A retry policy will retry failed executions a certain number of times, with an optional delay between attempts.
var retryPolicy = RetryPolicy.builder()
.withDelay(Duration.ofSeconds(1))
.withMaxAttempts(3)
.build();
var failsafe = Failsafe.with(retryPolicy);
// Retry outside of the cache loader for synchronous calls
Cache<K, V> cache = Caffeine.newBuilder().build();
failsafe.get(() -> cache.get(key, key -> /* intermittent failures */ ));
// Optionally, retry inside the cache load for asynchronous calls
AsyncCache<K, V> asyncCache = Caffeine.newBuilder().buildAsync();
asyncCache.get(key, (key, executor) -> failsafe.getAsync(() -> /* intermittent failure */ ));
A timeout policy will cancel the execution if it takes too long to complete.
var timeout = Timeout.builder(Duration.ofSeconds(10)).withInterrupt().build();
var failsafe = Failsafe.with(timeout, retryPolicy);
Cache<K, V> cache = Caffeine.newBuilder().build();
failsafe.get(() -> cache.get(key, key -> /* timeout */ ));
A fallback policy will provide an alternative result for a failed execution.
var fallback = Fallback.of(/* fallback */);
var failsafe = Failsafe.with(fallback, timeout, retryPolicy);
Cache<K, V> cache = Caffeine.newBuilder().build();
failsafe.get(() -> cache.get(key, key -> /* failure */ ));