Implement the circuit breaker pattern in Java with Resilience4j and in Go with gobreaker, including thresholds and fallbacks.
Agentic Workflow: Read -> Reason -> Act (circuit-breaker-pattern)
You are Circuit Breaker Pattern (api/general) — a sub-agent that Reads, Reasons, and Acts via allowed-tools.
1. Read — api context for circuit-breaker-pattern
- Domain: Implement the circuit breaker pattern in Java with Resilience4j and in Go with gobreaker, including thresholds and fallbacks.
- resilience4j: Add circuit breakers to Java apps with Resilience4j and configure thresholds —
mvn dependency:get -Dartifact=io.github.resilience4j:resilience4j-circuitbreaker - gobreaker: Use sony/gobreaker circuit breaker in Go services with custom thresholds and fallbacks —
go get github.com/sony/gobreaker - Check
knowledgeandprerequisites: mvn
2. Reason — think for circuit-breaker-pattern
- For
resilience4j: Add circuit breakers to Java apps with Resilience4j and configure thresholds — decide which checks to run - For
gobreaker: Use sony/gobreaker circuit breaker in Go services with custom thresholds and fallbacks — decide which checks to run - Evaluate trust/policy:
kdesk trust+kdesk doctorpatterns for your inputs
3. Act — execute with circuit-breaker-pattern tools
- Tools:
Glob,Grep,Read,Mvn,Bash(see frontmattertools/allowed-tools) - Use
safe_pathfor any write; record evidence (paths, checksums) - Fingerprint:
circuit-breaker-pattern:8a0ae5db
Circuit Breaker Pattern
Protect APIs from cascading failures with circuit breakers.
When to Use
- A downstream dependency is failing or slow
- Preventing timeouts from piling up on a flaky service
- Fast-failing with fallbacks instead of hanging
States
Closed -> (failure ratio exceeded) -> Open -> (timeout) -> Half-open -> (success) -> Closed
Java with Resilience4j
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-circuitbreaker</artifactId>
<version>2.2.0</version>
</dependency>
CircuitBreakerConfig config = CircuitBreakerConfig.custom()
.failureRateThreshold(60)
.waitDurationInOpenState(Duration.ofSeconds(10))
.slidingWindowSize(20)
.build();
CircuitBreaker cb = CircuitBreaker.of("payments", config);
String result = cb.executeSupplier(() -> paymentsClient.charge(order));
Go with gobreaker
st := gobreaker.Settings{
Name: "payments",
MaxRequests: 5,
Interval: 60 * time.Second,
Timeout: 10 * time.Second,
ReadyToTrip: func(counts gobreaker.Counts) bool {
return counts.ConsecutiveFailures > 5
},
}
cb := gobreaker.NewCircuitBreaker(st)
resp, err := cb.Execute(func() (interface{}, error) {
return charge(order)
})
Testing
# Open the circuit by failing repeatedly, then confirm fast-fail
curl -s -o /dev/null -w "%{http_code}\n" http://localhost:8080/api/payments
# Load to trigger thresholds
hey -n 200 -c 20 http://localhost:8080/api/payments
Best Practices
- Tune thresholds from observed p99 and error budgets
- Provide fallbacks for degraded mode
- Expose breaker state as metrics
- Set a probe timeout to recover automatically
- Do not place breakers between a client and its own gateway
- Test the open, half-open, and closed transitions explicitly
Capabilities
resilience4j
Add circuit breakers to Java apps with Resilience4j and configure thresholds
Parameters:
artifact(string): Resilience4j artifact coordinateversion(string): Resilience4j version such as 2.2.0
Commands:
mvn dependency:get -Dartifact=io.github.resilience4j:resilience4j-circuitbreaker:2.2.0mvn dependency:get -Dartifact=io.github.resilience4j:resilience4j-spring-boot3:2.2.0mvn testmvn spring-boot:run
Examples:
- mvn dependency:get -Dartifact=io.github.resilience4j:resilience4j-circuitbreaker:2.2.0
- mvn test -Dtest=CircuitBreakerTest
- mvn spring-boot:run
gobreaker
Use sony/gobreaker circuit breaker in Go services with custom thresholds and fallbacks
Parameters:
failure_ratio(string): Failure ratio that opens the circuit, e.g. 0.6timeout(string): Timeout before probing closed again, e.g. 10s
Commands:
go get github.com/sony/gobreakergo run ./cmd/servergo test ./...go vet ./...
Examples:
- go get github.com/sony/gobreaker
- go test -run TestBreaker -v ./...
- go run ./cmd/server