Reliability
Definition

Reliability Patterns

Fault Tolerance Strategies

Implementation
Retry with Exponential Backoff
public <T> T retryWithBackoff(Supplier<T> operation, int maxRetries) {
int retries = 0;
while (true) {
try {
return operation.get();
} catch (Exception e) {
if (++retries >= maxRetries) throw e;
long waitTime = (long) Math.pow(2, retries) * 1000;
long jitter = (long) (Math.random() * 1000);
Thread.sleep(waitTime + jitter);
}
}
}
Circuit Breaker
public class CircuitBreaker {
private State state = State.CLOSED;
private int failureCount = 0;
private long lastFailureTime;
private final int threshold = 5;
private final long timeout = 30000;
public <T> T execute(Supplier<T> operation, Supplier<T> fallback) {
if (state == State.OPEN) {
if (System.currentTimeMillis() - lastFailureTime > timeout) {
state = State.HALF_OPEN;
} else {
return fallback.get();
}
}
try {
T result = operation.get();
reset();
return result;
} catch (Exception e) {
recordFailure();
return fallback.get();
}
}
private void recordFailure() {
failureCount++;
lastFailureTime = System.currentTimeMillis();
if (failureCount >= threshold) {
state = State.OPEN;
}
}
private void reset() {
failureCount = 0;
state = State.CLOSED;
}
}
Testing for Reliability

Tips & Tricks
