amirdoosti6060 / CircuitBreaker

Sample for Circuit Breaker, Retry and Timeout design patterns

Home Page:https://www.linkedin.com/pulse/circuit-breaker-design-pattern-amir-doosti

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CircuitBreaker

Introduction

In this sample I tried to show you how to implement several Resilience Patterns like Circuit Breaker, Retry and Timeout in microservices unit C#.Net asd Polly package. Following article in my Linkedin describe resilience pattern and provide in-deep descriptions:
https://www.linkedin.com/pulse/circuit-breaker-design-pattern-amir-doosti

Structure of solution

This sample consists two projects:

  • CircuitBreaker: It is a console application and let you send a request to a WebService and see how different resilience patterns work.
  • WebService: It is a very simple Web API that auto generated by Visual Studio and has only one get API. We use it for out test in CircuitBreacker program.

Technology Stack

  • OS: Windows 10 Enterprise - 64 bits
  • IDE: Visual Studio Enterprise 2022 (64 bits) - version 17.2.5
  • Framework: .Net 6
  • Language: C#

How to run

Test Circuit Breaker behavior
Run WebService and normally it will be available in http://localhost:5000/weatherforecast
But you may host application in another host and with another port and so the address will change.
Run Circuit breacker like this:
CircuitBreacker http://localhost:5000/weatherforecast circuitbreacker
You will find out that everything is ok and circuit breacker do nothing
Then stop WebService and re-run the CircuitBreaker or change the url to an unavailable address.
This time you will see after two request, CircuitBreaker mechanism activate and open the circuit.

Test Retry behavior
Run WebService and normally it will be available in http://localhost:5000/weatherforecast
But you may host application in another host and with another port and so the address will change.
Run Circuit breacker like this:
CircuitBreacker http://localhost:5000/weatherforecast retry
While WebService is running, nothing happen.
But as soon as it is stopped, after 4 attempt, it break each request.

Test Timeout behavior
Run WebService and normally it will be available in http://localhost:5000/weatherforecast
But you may host application in another host and with another port and so the address will change.
Run Circuit breacker like this:
CircuitBreacker http://localhost:5000/weatherforecast timeout
While WebService is running, nothing happen.
But as soon as it is stopped, after 1 second, it break each request and a timeout happens.

You can play with different options and see the reaction of these resilience patterns.
Also you can extend the example and try other patterns

What you may learn from this project?

  • Circuit Breacker design pattern
  • Retry design pattern
  • Timeout design pattern
  • How to work with Polly library
  • What each strategy options do