Spring / Spring Retry Interview Questions
How does Spring Retry implement the circuit breaker pattern?
Spring Retry includes a CircuitBreakerRetryPolicy that implements the circuit breaker pattern on top of its standard retry infrastructure. The circuit acts as a gate that opens when failure rates exceed a threshold, temporarily blocking retries to prevent further load on a failing downstream system.
Circuit states:
- Closed: Normal operation. Requests flow through and failures are counted.
- Open: The failure threshold has been reached. All calls fail immediately without attempting the operation.
- Half-Open: After a reset timeout, one trial request is allowed through to test if recovery has occurred.
Configuration via RetryTemplate:
CircuitBreakerRetryPolicy policy = new CircuitBreakerRetryPolicy( new SimpleRetryPolicy(3)); policy.setOpenTimeout(5000); // open after 5s of failures policy.setResetTimeout(20000); // try again after 20s RetryTemplate template = new RetryTemplate(); template.setRetryPolicy(policy);
Spring Retry's circuit breaker is stateful — it requires a stateful RetryTemplate (using RetryState keys per operation) so that circuit state persists across multiple independent calls to the same operation. This is different from stateless retry where each call sequence is independent.
More Related questions...