A circuit breaker can be used to protect an application against unreliable
services or unexpected load. A newly created EventCountCircuitBreaker object is
initially in state closed meaning that no problem has been detected. When the
application encounters specific events (like errors or service timeouts), it tells the
circuit breaker to increment an internal counter. If the number of events reported in a
specific time interval exceeds a configurable threshold, the circuit breaker changes
into state open. This means that there is a problem with the associated sub
system; the application should no longer call it, but give it some time to settle down.
The circuit breaker can be configured to switch back to closed state after a
certain time frame if the number of events received goes below a threshold.
When a EventCountCircuitBreaker object is constructed the following parameters
can be provided:
This class supports the following typical use cases:
Protecting against load peaks
Imagine you have a server which can handle a certain number of requests per minute.
Suddenly, the number of requests increases significantly - maybe because a connected
partner system is going mad or due to a denial of service attack. A
EventCountCircuitBreaker can be configured to stop the application from
processing requests when a sudden peak load is detected and to start request processing
again when things calm down. The following code fragment shows a typical example of
such a scenario. Here the EventCountCircuitBreaker allows up to 1000 requests
per minute before it interferes. When the load goes down again to 800 requests per
second it switches back to state closed:
EventCountCircuitBreaker breaker = new EventCountCircuitBreaker(1000, 1, TimeUnit.MINUTE, 800);
...
public void handleRequest(Request request) {
if (breaker.incrementAndCheckState()) {
// actually handle this request
} else {
// do something else, e.g. send an error code
}
}
Deal with an unreliable service
In this scenario, an application uses an external service which may fail from time to time. If there are too many errors, the service is considered down and should not be called for a while. This can be achieved using the following pattern - in this concrete example we accept up to 5 errors in 2 minutes; if this limit is reached, the service is given a rest time of 10 minutes:
EventCountCircuitBreaker breaker = new EventCountCircuitBreaker(5, 2, TimeUnit.MINUTE, 5, 10, TimeUnit.MINUTE);
...
public void handleRequest(Request request) {
if (breaker.checkState()) {
try {
service.doSomething();
} catch (ServiceException ex) {
breaker.incrementAndCheckState();
}
} else {
// return an error code, use an alternative service, etc.
}
}
In addition to automatic state transitions, the state of a circuit breaker can be
changed manually using the methods open() and close(). It is also
possible to register PropertyChangeListener objects that get notified whenever
a state transition occurs. This is useful, for instance to directly react on a freshly
detected error condition.
Implementation notes:
| Modifier and Type | Class and Description |
|---|---|
| private abstract static class | EventCountCircuitBreaker.
Internally used class for executing check logic based on the current state of the circuit breaker. |
| private static class | EventCountCircuitBreaker.
An internally used data class holding information about the checks performed by this class. |
| private static class | EventCountCircuitBreaker.
A specialized |
| private static class | EventCountCircuitBreaker.
A specialized |
| Modifier and Type | Field and Description |
|---|---|
| private final AtomicReference | checkIntervalData
Stores information about the current check interval. |
| private final long | closingInterval
The time interval for closing the circuit breaker. |
| private final int | closingThreshold
The threshold for closing the circuit breaker. |
| private final long | openingInterval
The time interval for opening the circuit breaker. |
| private final int | openingThreshold
The threshold for opening the circuit breaker. |
| private static final Map | STRATEGY_MAP
A map for accessing the strategy objects for the different states. |
| Access | Constructor and Description |
|---|---|
| public | EventCountCircuitBreaker(final int
the threshold for changing the status of the circuit breaker; if
the number of events received in a check interval is greater than this value, the
circuit breaker is opened; if it is lower than this value, it is closed again threshold, final long the check interval for opening or closing the circuit breaker checkInterval, final TimeUnit the checkUnit)TimeUnit defining the check intervalCreates a new instance of |
| public | EventCountCircuitBreaker(final int
the threshold for opening the circuit breaker; if this
number of events is received in the time span determined by the check interval, the
circuit breaker is opened openingThreshold, final long the check interval for opening or closing the circuit breaker checkInterval, final TimeUnit the checkUnit, final int TimeUnit defining the check intervalthe threshold for closing the circuit breaker; if the
number of events received in the time span determined by the check interval goes
below this threshold, the circuit breaker is closed again closingThreshold)Creates a new instance of |
| public | EventCountCircuitBreaker(final int
the threshold for opening the circuit breaker; if this
number of events is received in the time span determined by the opening interval,
the circuit breaker is opened openingThreshold, final long the interval for opening the circuit breaker openingInterval, final TimeUnit the openingUnit, final int TimeUnit defining the opening intervalthe threshold for closing the circuit breaker; if the
number of events received in the time span determined by the closing interval goes
below this threshold, the circuit breaker is closed again closingThreshold, final long the interval for closing the circuit breaker closingInterval, final TimeUnit the closingUnit)TimeUnit defining the closing intervalCreates a new instance of |
| Modifier and Type | Method and Description |
|---|---|
| private void | changeStateAndStartNewCheckInterval(final AbstractCircuitBreaker.
the new state to be set newStateChanges the state of this circuit breaker and also initializes a new
|
| public boolean | checkState()
Implements abstract org. Implements org. Checks the state of this circuit breaker and changes it if necessary. |
| public void | close()
Overrides org. Implements org. Closes this circuit breaker. |
| private static Map | |
| public long | Returns: the opening check intervalGets the interval (in nanoseconds) for checking for the closing threshold. |
| public int | Returns: the closing thresholdGets the threshold value for closing the circuit breaker. |
| public long | Returns: the opening check intervalGets the interval (in nanoseconds) for checking for the opening threshold. |
| public int | Returns: the opening thresholdGets the threshold value for opening the circuit breaker. |
| public boolean | Returns: true if the circuit breaker is now closed; false otherwiseIncrements the monitored value by 1 and performs a check of the current state of this circuit breaker. |
| public boolean | incrementAndCheckState(final Integer increment)
Implements abstract org. Implements org. Increments the monitored value and performs a check of the current state of this circuit breaker. |
| pack-priv long | |
| private EventCountCircuitBreaker. | Returns: the updatedCheckIntervalData objectthe increment for the internal counter increment, final EventCountCircuitBreaker.the current check data object currentData,the current state of the circuit breaker currentState,the current time time)Calculates the next |
| public void | open()
Overrides org. Implements org. Opens this circuit breaker. |
| private boolean | Returns: a flag whether the circuit breaker is now closedthe increment for the internal counter increment)Actually checks the state of this circuit breaker and executes a state transition if necessary. |
| private static EventCountCircuitBreaker. | Returns: the correspondingAbstractStateStrategythe state stateReturns the |
| private boolean | Returns: a flag whether the update was successfulthe current check data object currentData,the replacing check data object nextDataUpdates the |
| checkIntervalData | back to summary |
|---|---|
| private final AtomicReference<EventCountCircuitBreaker. Stores information about the current check interval. | |
| closingInterval | back to summary |
|---|---|
| private final long closingInterval The time interval for closing the circuit breaker. | |
| closingThreshold | back to summary |
|---|---|
| private final int closingThreshold The threshold for closing the circuit breaker. | |
| openingInterval | back to summary |
|---|---|
| private final long openingInterval The time interval for opening the circuit breaker. | |
| openingThreshold | back to summary |
|---|---|
| private final int openingThreshold The threshold for opening the circuit breaker. | |
| STRATEGY_MAP | back to summary |
|---|---|
| private static final Map<AbstractCircuitBreaker. A map for accessing the strategy objects for the different states. | |
| EventCountCircuitBreaker | back to summary |
|---|---|
| public EventCountCircuitBreaker(final int threshold, final long checkInterval, final TimeUnit checkUnit) Creates a new instance of
| |
| EventCountCircuitBreaker | back to summary |
|---|---|
| public EventCountCircuitBreaker(final int openingThreshold, final long checkInterval, final TimeUnit checkUnit, final int closingThreshold) Creates a new instance of
| |
| EventCountCircuitBreaker | back to summary |
|---|---|
| public EventCountCircuitBreaker(final int openingThreshold, final long openingInterval, final TimeUnit openingUnit, final int closingThreshold, final long closingInterval, final TimeUnit closingUnit) Creates a new instance of
| |
| changeStateAndStartNewCheckInterval | back to summary |
|---|---|
| private void changeStateAndStartNewCheckInterval(final AbstractCircuitBreaker. Changes the state of this circuit breaker and also initializes a new
| |
| checkState | back to summary |
|---|---|
| public boolean checkState() Implements abstract org. Implements org. Doc from org. Checks the state of this circuit breaker and changes it if necessary. The return value indicates whether the circuit breaker is now in state closed; a value of true typically means that the current operation can continue. This implementation checks the internal event counter against the threshold values and the check intervals. This may cause a state change of this circuit breaker.
| |
| close | back to summary |
|---|---|
| public void close() Overrides org. Implements org. Doc from org. Closes this circuit breaker. Its state is changed to closed. If this circuit breaker is already closed, this method has no effect. A new check interval is started. If too many events are received in this interval, the circuit breaker changes again to state open. If this circuit breaker is already closed, this method has no effect, except that a new check interval is started.
| |
| createStrategyMap | back to summary |
|---|---|
| private static Map Creates the map with strategy objects. It allows access for a strategy for a given state.
| |
| getClosingInterval | back to summary |
|---|---|
| public long getClosingInterval() Gets the interval (in nanoseconds) for checking for the closing threshold.
| |
| getClosingThreshold | back to summary |
|---|---|
| public int getClosingThreshold() Gets the threshold value for closing the circuit breaker. If the number of events received in the time span determined by the closing interval goes below this threshold, the circuit breaker is closed again.
| |
| getOpeningInterval | back to summary |
|---|---|
| public long getOpeningInterval() Gets the interval (in nanoseconds) for checking for the opening threshold.
| |
| getOpeningThreshold | back to summary |
|---|---|
| public int getOpeningThreshold() Gets the threshold value for opening the circuit breaker. If this number of events is received in the time span determined by the opening interval, the circuit breaker is opened.
| |
| incrementAndCheckState | back to summary |
|---|---|
| public boolean incrementAndCheckState() Increments the monitored value by 1 and performs a check of the current state of this
circuit breaker. This method works like
| |
| incrementAndCheckState | back to summary |
|---|---|
| public boolean incrementAndCheckState(final Integer increment) Implements abstract org. Implements org. Doc from org. Increments the monitored value and performs a check of the current state of this
circuit breaker. This method works like
| |
| nanoTime | back to summary |
|---|---|
| pack-priv long nanoTime() Returns the current time in nanoseconds. This method is used to obtain the current time. This is needed to calculate the check intervals correctly.
| |
| nextCheckIntervalData | back to summary |
|---|---|
| private EventCountCircuitBreaker. Calculates the next
| |
| open | back to summary |
|---|---|
| public void open() Overrides org. Implements org. Doc from org. Opens this circuit breaker. Its state is changed to open. Depending on a concrete implementation, it may close itself again if the monitored subsystem becomes available. If this circuit breaker is already open, this method has no effect. This circuit breaker may close itself again if the number of events received during a check interval goes below the closing threshold. If this circuit breaker is already open, this method has no effect, except that a new check interval is started.
| |
| performStateCheck | back to summary |
|---|---|
| private boolean performStateCheck(final int increment) Actually checks the state of this circuit breaker and executes a state transition if necessary.
| |
| stateStrategy | back to summary |
|---|---|
| private static EventCountCircuitBreaker. Returns the
| |
| updateCheckIntervalData | back to summary |
|---|---|
| private boolean updateCheckIntervalData(final EventCountCircuitBreaker. Updates the
| |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| protected abstract long | Returns: the check interval to be appliedthe breaker)CircuitBreakerObtains the check interval to applied for the represented state from the given
|
| public boolean | Returns: a flag whether the end of the current check interval is reachedthe breaker, final EventCountCircuitBreaker.CircuitBreakerthe current state object currentData,the current time now)Tests whether the end of the current check interval is reached. |
| public abstract boolean | Returns: a flag whether a state transition should be performedthe breaker, EventCountCircuitBreaker.CircuitBreakerthe current currentData,CheckIntervalData objectthe updated nextDataCheckIntervalData objectTests whether the specified |
| AbstractStateStrategy | back to summary |
|---|---|
| private AbstractStateStrategy() | |
| fetchCheckInterval | back to summary |
|---|---|
| protected abstract long fetchCheckInterval(EventCountCircuitBreaker breaker) Obtains the check interval to applied for the represented state from the given
| |
| isCheckIntervalFinished | back to summary |
|---|---|
| public boolean isCheckIntervalFinished(final EventCountCircuitBreaker breaker, final EventCountCircuitBreaker. Tests whether the end of the current check interval is reached.
| |
| isStateTransition | back to summary |
|---|---|
| public abstract boolean isStateTransition(EventCountCircuitBreaker breaker, EventCountCircuitBreaker. Tests whether the specified
| |
| Modifier and Type | Field and Description |
|---|---|
| private final long | checkIntervalStart
The start time of the current check interval. |
| private final int | eventCount
The counter for events. |
| Access | Constructor and Description |
|---|---|
| pack-priv | CheckIntervalData(final int
the current count value count, final long the start time of the check interval intervalStart)Creates a new instance of |
| Modifier and Type | Method and Description |
|---|---|
| public long | Returns: the check interval start timeGets the start time of the current check interval. |
| public int | |
| public EventCountCircuitBreaker. | Returns: the updated instancethe delta delta)Returns a new instance of |
| checkIntervalStart | back to summary |
|---|---|
| private final long checkIntervalStart The start time of the current check interval. | |
| eventCount | back to summary |
|---|---|
| private final int eventCount The counter for events. | |
| CheckIntervalData | back to summary |
|---|---|
| pack-priv CheckIntervalData(final int count, final long intervalStart) Creates a new instance of
| |
| getCheckIntervalStart | back to summary |
|---|---|
| public long getCheckIntervalStart() Gets the start time of the current check interval.
| |
| getEventCount | back to summary |
|---|---|
| public int getEventCount() Gets the event counter.
| |
| increment | back to summary |
|---|---|
| public EventCountCircuitBreaker. Returns a new instance of
| |
AbstractStateStrategy implementation for the state closed.
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| protected long | fetchCheckInterval(final EventCountCircuitBreaker breaker)
Implements abstract org. Obtains the check interval to applied for the represented state from the given
|
| public boolean | isStateTransition(final EventCountCircuitBreaker breaker, final EventCountCircuitBreaker.
Implements abstract org. Tests whether the specified |
| StateStrategyClosed | back to summary |
|---|---|
| private StateStrategyClosed() | |
| fetchCheckInterval | back to summary |
|---|---|
| protected long fetchCheckInterval(final EventCountCircuitBreaker breaker) Implements abstract org. Obtains the check interval to applied for the represented state from the given
| |
| isStateTransition | back to summary |
|---|---|
| public boolean isStateTransition(final EventCountCircuitBreaker breaker, final EventCountCircuitBreaker. Implements abstract org. Doc from org. Tests whether the specified
| |
AbstractStateStrategy implementation for the state open.
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| protected long | fetchCheckInterval(final EventCountCircuitBreaker breaker)
Implements abstract org. Obtains the check interval to applied for the represented state from the given
|
| public boolean | isStateTransition(final EventCountCircuitBreaker breaker, final EventCountCircuitBreaker.
Implements abstract org. Tests whether the specified |
| StateStrategyOpen | back to summary |
|---|---|
| private StateStrategyOpen() | |
| fetchCheckInterval | back to summary |
|---|---|
| protected long fetchCheckInterval(final EventCountCircuitBreaker breaker) Implements abstract org. Obtains the check interval to applied for the represented state from the given
| |
| isStateTransition | back to summary |
|---|---|
| public boolean isStateTransition(final EventCountCircuitBreaker breaker, final EventCountCircuitBreaker. Implements abstract org. Doc from org. Tests whether the specified
| |