Circuit Breaker For Anypoint MQ
In this tutorial, we will demonstrate how can we implement the circuit breaker for Anypoint MQ
Circuit breaker pattern
The basic idea behind the circuit breaker is very simple. You wrap a protected function call in a circuit breaker object, which monitors for failures. Once the failures reach a certain threshold, the circuit breaker trips, and all further calls to the circuit breaker return with an error, without the protected call being made at all.
Circuit Breaker States
- Open:- The Subscriber source doesn’t attempt to retrieve messages and skips the message silently until the configured trip timeout occurred.
- Closed:- The starting state where the Subscriber source retrieves messages normally from MQ based on its configuration
- Half Open:- After trip timeout elapses, the Subscriber source goes to a Half-Open state. In the next poll for messages, the Subscriber source retrieves a single message from the service and uses that message to check if the system has recovered before going back to the normal Closed state.
To demonstrate the circuit breaker we will create a new mule application and subscribe the message from a queue and make a call to the rest endpoint, the objective of the circuit breaker is when the rest API is not available for a longer duration Anypoint subscriber will stop consuming the message from Anypoint message queue for a particular time and give time to external service to become available again
How It Works In Mule
The circuit breaker capability is bound to the error handling mechanism provided by Mule.. You can bind any error to a circuit failure. For example, you can bind HTTP:TIMEOUT, MULE:RETRY_EXHAUSTED, or even a custom error from your app, such as APP:CUSTOM_ERROR
If a Mule flow finishes its execution with an error, the Subscriber checks if the error is for the defined ErrorTypes then it counts consecutive occurrences for that error until errorsThreshold is reached. When errorsThreshold is reached, the circuit trips and stops polling for new messages the duration specified by tripTimeout
To test this scenario first we will create a MQ if not exist in Anypoint platform
Create a new mule application and drag and drop Anypoint MQ subscriber operation
Create configuration for Anypoint MQ, provide the URL, and client id and client_secret to connect with Anypoint MQ( you have to create a new client_apps below destination tab on MQ Home page)
Configure Circuit Breaker
Go to Global Elements-> Create->Component configuration->click on Circuit breaker
Provide the below parameters
- On Error Type: Error type which will decide the circuit breaker to trip, it can be any error you can provide multiple error type as comma-separated values.
- Error Threshold: This is the threshold(no of consecutive error) after which the circuit will open
- Trip timeout: This time decide for how long the circuit will keep open
- Trip timeout Unit: Unit for trip timeout
Configure the connector to poll the message from the Anypoint MQ
Configure the circuit breaker created globally(you can also create a circuit breaker configuration inline with the MQ subscriber as shown in option-2, but it is recommended to create globally so that you can use this with other subscribers
Alternate option to define the circuit breaker as inline
Configure the HTTP Requester to make a rest API call and wrap the HTTP Requester under until successful to retry the HTTP Request in case the service is not available after all the Retry is exhausted Retry exhausted, after 3 consecutive MULE:RETRY:EXHAUSTED circuit breaker will trip off
Deploy the application on local and send the message from MQ and see the logs
You will see after 3 consecutive error circuit is trip off(open) for 5 minutes and after 5 Minutes the circuit will half closed/open to check if the API is available or not, if the API is available it will be close again to consume all the message else it will be kept open and wait for the complete recovery
Sample application: circuit breaker sample application