Design Pattern for Microservices - Part 02

Chamal Weerasinghe
3 min readJun 7, 2021

--

Photo by Troy Bridges on Unsplash

In the previous article, we discussed the aggregation patterns used in a microservice architecture. Now the number of services is getting increasing now the team has to put effort on maintaining the services too.

There are situations where services go offline or to a state where that they cannot respond to the responses, it is getting. This can happen due to few scenarios first thing is the excessive load of requests that the service getting. When the requests are getting increasing they had to wait in the queue till they processed this takes latency and then eventually even causes downtime.

Another reason is when using a pattern like chained services a fault in one service can cause failure the other services waiting for the response, this is what called the “Cascade Failure”. To avoid these types of issues Microservice has a specific design pattern called Circuit Breaker Design Pattern.

Circuit Breaker Design Pattern

In-Circuit Breaker Design Pattern what does is monitors the requests flow through the services and using a circuit breaker. The circuit breaker allows setting a certain level of threshold which is completely customizable (Based on Traffic/Response time). And based on this threshold if the threshold is near to the limit or it is near to exceed the circuit breaker will divert the request into another service or if there is any failure again circuit breaker switches the request into a working service that can fulfill the request.

Circuit breakers' status can change into different states.

Closed - The request flow is normal and it does not show any cause of failure. so the circuit breaker remained closed.

Open - In the open state the requests are failing and in this state, it does not send any requests.

Half_Open - The circuit breaker cannot stay closed. to take the services back online in order to check if the failed service is recovered circuit breaker turns the state into Half-Open and send a request only to test that it is working and returning a response. if it succeeds it will switch that service to a closed state or otherwise back to an open state.

Change of Status in Circuit Breaker (Image from Martin Fowler)

Here’s an example from Nginx of Circuit breaker pattern used in image resizing service where the image is uploaded in uploader service and it checks the active services by a circuit breaker and diverts the request to a working service

Circuit Breaker (Image from Ngnix)

--

--