Bird
0
0

Given this pseudocode snippet, what will be the output if the circuit breaker is CLOSED and the service call fails twice?

medium📝 Analysis Q4 of 15
Microservices - Resilience Patterns
Given this pseudocode snippet, what will be the output if the circuit breaker is CLOSED and the service call fails twice?
breaker = CircuitBreaker(state='CLOSED', failureCount=0, failureThreshold=3)
for i in range(2):
  try:
    call_service()
  except:
    breaker.failureCount += 1
if breaker.failureCount >= breaker.failureThreshold:
  breaker.state = 'OPEN'
print(breaker.state)
ACLOSED
BHALF-OPEN
COPEN
DFAILED
Step-by-Step Solution
Solution:
  1. Step 1: Track failure count after two failures

    Failure count increments twice, so failureCount = 2.
  2. Step 2: Compare failureCount with failureThreshold

    Threshold is 3, current failureCount is 2, which is less than threshold, so state remains CLOSED.
  3. Final Answer:

    CLOSED -> Option A
  4. Quick Check:

    Failures below threshold keep state CLOSED [OK]
Quick Trick: State changes only when failures reach threshold [OK]
Common Mistakes:
MISTAKES
  • Assuming state changes after any failure
  • Confusing failureCount with threshold
  • Thinking state changes to HALF-OPEN here

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Microservices Quizzes