mule4-circuit-breaker
Code
When a backend slows down under load, retry turns every timeout into three calls and takes down what was already at its limit. mule4-circuit-breaker is the plugin from the article Circuit Breaker in MuleSoft: When Retry Makes Things Worse: when a backend keeps failing, it opens the circuit and stops calling what is broken. The decision comes from the failure rate in a sliding window.
The failure, made visible
In the scene, Checkout calls Payment twice. In the first take, with retry alone, 20 requests become 60 calls until Payment goes down. In the second, with the plugin’s scope, the circuit goes through CLOSED, OPEN, HALF_OPEN, OPEN, HALF_OPEN and CLOSED. The diagram shows what moves the circuit from one state to another.
What it does
- A
circuit-breaker:executescope that wraps the call; a blocked call raisesCIRCUIT-BREAKER:OPEN. - CLOSED, OPEN and HALF_OPEN states, with a configurable window, failure rate and reset time.
- Separates infrastructure failures from business errors, by error type.
- The state lives in the runtime’s persistent Object Store, visible across CloudHub 2.0 replicas.
Limit: there is no lock across replicas (the last write wins), the first write of a new key can diverge and the persistent Object Store adds latency to every call, and reading the error type goes through privileged.exception, a Mule package that may change between runtime versions.
Getting started
Needs Java 17 and Maven 3.9.x. Build the plugin from the tag; no Anypoint account is needed.
git clone --branch v2.0.0 https://github.com/brunosouzas/mule4-circuit-breaker.git
mvn -f mule4-circuit-breaker/pom.xml -DskipTests install
<circuit-breaker:execute circuitBreakerKey="my-service" failureErrorTypes="HTTP:TIMEOUT,HTTP:CONNECTIVITY">
<http:request config-ref="my-service-config" path="/orders" method="GET" />
</circuit-breaker:execute>