Skip to content

Vertx Circuit Breaker

DodaTech 1 min read

In this tutorial, you'll learn about Fix Vert.x Circuit Breaker Not Opening. We cover key concepts, practical examples, and best practices.

The Problem

Circuit breaker stays closed despite repeated failures, or never recovers.

Quick Fix

Configure circuit breaker properly

Wrong:

CircuitBreaker breaker = CircuitBreaker.create("my-circuit", vertx);
// No configuration

Output:

Default settings (5 failures, 5s reset)

Right:

CircuitBreaker breaker = CircuitBreaker.create("my-circuit", vertx,
    new CircuitBreakerOptions()
        .setMaxFailures(3)
        .setTimeout(2000)
        .setResetTimeout(10000)
);

Output:

Custom configuration

Use executeWithFallback

Wrong:

breaker.execute(future -> {
    callService().onComplete(future);
}); // No fallback

Output:

Exception on failure

Right:

breaker.executeWithFallback(
    future -> callService().onComplete(future),
    throwable -> Future.succeededFuture("fallback")
);

Output:

Fallback returned on failure

Handle circuit state changes

Wrong:

// No state listener
breaker.openHandler(v -> log.warn("Circuit opened"));
breaker.closeHandler(v -> log.info("Circuit closed"));

Output:

Callback registration

Right:

breaker.openHandler(v -> log.warn("Circuit opened"));
breaker.closeHandler(v -> log.info("Circuit closed"));

Output:

Listeners registered

Prevention

  • Set maxFailures, timeout, and resetTimeout
  • Use executeWithFallback for graceful degradation
  • Add state change listeners for monitoring

Common Mistakes with circuit breaker

  1. Using foldl instead of foldl' causing stack overflow on large lists
  2. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  3. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable

These mistakes appear frequently in real-world VERTX code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### How does the circuit breaker recover?

After resetTimeout, the circuit enters half-open state and tries one request. If it succeeds, the circuit closes.

This quick fix is part of the DodaTech Spring & JVM ecosystem series. Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro