Circuit Breaker - SDLC

It’s a software designs best practice to avoid cascading failures when an application cannot process the request or fails for some reason.
Let’s say service A calls Service B. When service B has issues, can calling service like A will get 5xx errors and this can have cascading effects on the service which invoked A.
To avoid this issue, it’s good to have a circuit break pattern.
When a service fails continuously for 3 times then we have open the “Circuit Breaker”,
• Service B will no longer accept requests from service A.
• Service B handles the request by queueing them to process later once service B is recovered.
• It can fail over the request to some other service.
• It can half open after certain amount of time to see if the service recovered.
Simple Python Program Implementing Circuit Breaker:
The program uses random.random function to generate numbers between 0.0 to 1.0.
If the generated number is lesser than 0.5 for 3 times, its calls another function as part of circuit breaker (queue_service).
In this service, we are adding a sleep logic for 10 sec and try processing the request.
import random
import time
from datetime import datetime
def working_service():
if random.random() < 0.5:
print(f"Request Completed - {datetime.now()}")
else:
raise Exception("Request Could Not Be Completed.")
def queue_service():
print(f"Circuit Breaker Triggered. Request Queued And It Will Be Processed Later - {datetime.now()}")
# Recovery time
time.sleep(10)
fail_count = 0
for i in range(10):
try:
working_service()
except Exception as e:
fail_count = fail_count + 1
if fail_count >=3:
queue_service()
fail_count = 0
else:
print(f"Request Failed. Circuit Breaker Not Triggered - {datetime.now()}")
Comments
Post a Comment