Circuit Breaker Pattern making application fault tolerant in the cloud

Circuit Breaker is a programming pattern applied in the cloud which helps applications to be fault tolerant. In today’s world, applications rely on several services, with few services being very important for the application to be running successfully. This makes it challenging as we would need to design systems that are capable of running when their dependencies stop functioning well.

When a system makes calls to an API there is always a possibility that the API might experience a downtime this is why our system should detect such incidents and provide an alternative. Providing an alternative is not an easy task for system engineers as they have to look for an alternative which provides similar functionality.

As an example, e-commerce websites use several services such as queues or a search API. If one of these services goes down, the e-commerce website will stop functioning. Circuit breaker helps us to identify when the primary systems fail and switch to an alternative.

The circuit breaker is a modern pattern which makes the application to use an alternative working service if the primary one fails.

The diagram below shows an overview of how this pattern is in place in an e-commerce website which uses AWS SQS to store jobs, but since it has implemented circuit breaker pattern if the SQS service fails the business logic will rely on a database.

The circuit breaker will be a script that runs every minute or second performing certain checks on the SQS queues. Once it detects SQS is not responding for a while (this duration depends on how long you would like to move the application to use the alternative system) it will update the Memcache or Redis so the application will start using the alternative service.

Example:

Circuit Breaker Code

if ($sqsFailed) {
  memcache_set($memcacheConnection, 'sqs_status', FALSE);
}

The Queue Logic

if (memcache_get($memcacheConnection, 'sqs_status') == FALSE) {
  $switchToDatabase = true;
}

Circuit breaker Examples

Imagine a scenario in which you’re using AWS SQS queues and suddenly the SQS services stop working or become unstable. Without, circuit breaker pattern your application will stop and will be continuously trying to connect to the SQS service. This will impact the user experience, increase memory consumption and degrade the service. By implementing the pattern, all these poor points can be avoided. This is a great example of implementing the circuit breaker pattern.