Using Moleculer’s Circuit Breaker for Resilient Microservice Calls
Learn how to configure and monitor Moleculer’s built‑in Circuit Breaker, the minimal architecture that protects your services from cascading failures, and when to rethink the design.
18 Aug 2026, 06:51 UTC

Problem & Takeaway
When microservices call each other, a sudden spike in failures can flood the entire system. Moleculer’s Circuit Breaker (CB) stops this by opening a circuit after a configurable failure rate, immediately rejecting further calls until the target recovers. The key takeaway: enable CB globally or per service, tune thresholds to your traffic, and expose state changes to a monitoring system. This protects downstream services and surfaces failure patterns early.
Requirements
- Node.js 20+ and Moleculer v0.14+.
- Consistent broker configuration across cluster nodes.
- Monitoring stack that can consume Prometheus metrics or custom events.
Minimal Design
The smallest viable CB setup consists of:
- Broker configuration with
circuitBreakerenabled. - Action that calls an external service.
- Optional
fallbackaction to return a graceful result when the circuit is open.
Example broker configuration (global CB):
module.exports = {
broker: new ServiceBroker({
nodeID: process.env.NODE_ID || 'node-1',
transporter: 'NATS://localhost:4222',
circuitBreaker: {
timeout: 3000, // ms
errorThresholdPercentage: 50,
volumeThreshold: 5,
resetTimeout: 10000
}
})
};
Service definition with fallback:
module.exports = {
name: 'order',
actions: {
async place(ctx) {
// Normal action logic
return ctx.call('payment.process', ctx.params);
},
async placeFallback(ctx) {
// Graceful degradation
return { status: 'unavailable', message: 'Payment service temporarily down' };
}
},
circuitBreaker: {
fallback: 'order.placeFallback'
}
};
Trust/Data Boundaries
Each broker node maintains its own CB state. In a cluster, this means:
- Calls from node A to service B may be blocked while node C still sees the circuit closed.
- Inconsistent states can lead to uneven load distribution.
To mitigate, keep CB thresholds conservative and consider a shared state via a distributed cache if strict consistency is required.
Operational Checks
Set up automated checks:
- Event listeners: Subscribe to
circuit.openandcircuit.closeevents to trigger alerts.broker.on('circuit.open', (service, state) => { console.warn(`Circuit opened for ${service}`); }); - Metrics endpoint: Moleculer exposes Prometheus metrics at
http://localhost:3000/metrics. Querymoleculer_circuit_state{service="payment"}to see current state.curl http://localhost:3000/metrics | grep moleculer_circuit_state
Failure Modes
- False positives: Short timeouts on high‑latency services can open the circuit unnecessarily.
- Fallback latency: Poorly implemented fallbacks can add latency or return stale data.
- Inconsistent cluster state: Without shared CB state, some nodes may keep calling a failed service.
When to Redesign
Consider a redesign if:
- CB opens more than 20% of the time in a 5‑minute window.
- Fallback responses exceed 5 % of total traffic.
- Cluster nodes report divergent CB states for the same target.
- You need to coordinate circuit state across services (use a shared state store).
Concrete Diagnostic Workflow
- Deploy the test service with CB enabled and
volumeThresholdset to 5. - Trigger 5 consecutive failures by throwing an error in the target service.
- Verify that the broker logs show
Circuit opened for paymentand that subsequent calls hit the fallback immediately. - Wait
resetTimeout(10 s) and confirm that a limited number of test calls are allowed (half‑open state). - On a successful call, ensure the log shows
Circuit closed for paymentand normal traffic resumes.
Practical Check
After deployment, run:
curl http://localhost:3000/metrics | grep moleculer_circuit_state | grep payment
Expect output similar to:
moleculer_circuit_state{service="payment",state="closed"} 1
If the state is open or half‑open, investigate recent logs or adjust thresholds.
Conclusion
Enabling Moleculer’s Circuit Breaker is a lightweight, per‑service safeguard against cascading failures. With proper thresholds, fallback logic, and monitoring, you can keep your microservice ecosystem responsive. Keep an eye on failure patterns and be ready to adjust or redesign when the CB starts masking real issues or when cluster consistency becomes a problem.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.