← Projects

mule4-circuit-breaker

Mule 4 (Java SDK) · MIT · Stable

On this page

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.

Animated scene with the same checkout in two takes. In the first, with retry only, 20 requests become 60 calls and the payment service goes down. In the second, with a circuit breaker, the circuit goes from CLOSED to OPEN after 5 failed calls, passes through HALF_OPEN, returns to OPEN, passes through HALF_OPEN again and closes in CLOSED; the other requests fail at once with CIRCUIT-BREAKER:OPEN, and a successful test call closes it.

Circuit breaker state diagram: it starts CLOSED; goes to OPEN when the failure rate in the window exceeds the threshold, with a minimum call count; goes to HALF_OPEN when the wait period (resetTimeout) elapses; returns to CLOSED after enough successful test calls and to OPEN on any failure during the test.

What it does

  • A circuit-breaker:execute scope that wraps the call; a blocked call raises CIRCUIT-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>

Where it appears

Code for this page