Circuit Breaker with Ocelot & Polly – Part 1

One of the key features of Microservices is the resilience to failures. In this blog post, we will aim to implement Circuit Breaker Pattern in our API Gateway. We would be using the Quality of Service (QoS) functionality exposed by Ocelot. Internally, Ocelot uses Polly library to implement Circuit Breaker.

Timeout Failures from Downstream services

As the first step, we would aim to trigger the Circuit Breaker when the downstream services fail due to timeout for a specified number of times. This functionality comes out of the box with the Ocelot.Provider.Polly package.

Let us begin by installing the package.

Install-Package Ocelot.Provider.Polly

To test the application, we have an endpoint exposed by the sample microservice, which would timeout randomly.

app.MapGet("services/randomtimeout", () =>
    var random = new Random();
    var value = random.Next(0, 2);
    if ( value== 0)
        throw new Exception("Random Timeout");
    return value;

To achieve the goal, first open your ocelot.json configuration file and add the qos configuration for the route for which the circuit breaker functionality is required. For example,

  "Routes": [
      "DownstreamPathTemplate": "/services/raiseexception",
      "DownstreamScheme": "https",
      "DownstreamHttpMethod": "GET",
      "RouteIsCaseSensitive": false,
      "DownstreamHostAndPorts": [
          "Host": "localhost",
          "Port": 7038
      "UpstreamPathTemplate": "/api/circuitbreaker/raiseexception",
      "UpstreamHttpMethod": ["Get"],
      "QoSOptions": {
        "ExceptionsAllowedBeforeBreaking": 2,
        "DurationOfBreak": 10000,
        "TimeoutValue": 2000
  "GlobalConfiguration": {
    "BaseUrl": "https://localhost:7247"

In the above configuration, I have a route that uses the downstream path of “/services/raiseexception”. To add QoS functionality for the route, I have added the “QosOptions” configuration to the Json. There are mainly 3 properties in the configuration

  • ExceptionAllowedBeforeBreaking : Number of exceptions which are allowed before the circuit breaker is triggered. This value needs to be greater than 0 for the rule to be implemented
  • DurationOfBreak – Duration in milliseconds for which the circuit breaker would remain open after been tripped.
  • TimeoutValue – Duration after which the request is considered as timedout.

With the configuration added in “Ocelot.json” (your ocelot configuration file), there is one more step that needs to be done. You need to call the AddPolly() extension method, which would set up the QoS middleware.


That’s all you need to configure the Circuit breaker with Ocelot and Polly. Now you could run the application and notice that once there are two instances of the timeout, the circuit breaker is tripped.

warn: Ocelot.Responder.Middleware.ResponderMiddleware[0]
      requestId: 0HMEUAL3POSR1:00000005, previousRequestId: no previous request id, message: Error Code: RequestTimedOutError Message: Timeout making http request, exception: Polly.CircuitBreaker.BrokenCircuitException: The circuit is now open and is not allowing calls.

In the next part of the blog post, we will look into how to trigger the circuit breaker when the downstream service responds with 500 Error a number of times.


2 thoughts on “Circuit Breaker with Ocelot & Polly – Part 1

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s