← 返回 reddit 的题目列表Implement a Circuit Breaker for Backend Servers
类型:online_judge
Problem: Implement a Circuit Breaker for Backend Servers
A load balancer distributes requests across multiple backend servers. Implement a circuit breaker used independently for each backend so that traffic is not continuously sent to unhealthy servers.
The breaker has three states:
CLOSED: requests are allowed normally.
OPEN: requests are rejected and no traffic is sent to that backend.
HALF_OPEN: after the cooldown period, a limited probe request is allowed; success recovers the backend, while failure opens the breaker again.
Design and implement:
logic for deciding whether a request may be sent to a backend;
state updates after request success or failure;
a consecutive-failure threshold;
an OPEN cooldown period;
probing and transitions in HALF_OPEN;
a thread-safety or concurrency strategy.
Explain time complexity, key state fields, and how this component integrates with backend selection in the load balancer.
Example behavior:
With a failure threshold of 3, three consecutive failures open a backend's breaker.
The load balancer must not select that backend before the cooldown expires.
After cooldown, one probe request is allowed.
A successful probe returns the breaker to CLOSED; a failed probe returns it to OPEN.