question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CircuitBreaker state into redis or some external storage

See original GitHub issue

Context:

  • .NET 6
  • ASP
  • Polly 7.2.4

I’m trying to store breaker state into redis or some external storage. This would allow me to have 3 pods of my app while they share the same circuit breaker state.

I actually have a list of ad domain controllers, so if one of them fails i automatically retry on the another one. With something like this:

.Retry(_configuration.HostNames.Count, onRetry: (exception, retryCount, context) =>
{
   context[CONTEXT_KEY_URL] = _configuration.HostNames[retryCount];
});

Then when i have to execute i use something like this:

 _connectPolicy.Execute(
    action: context =>
    {
        var url = (string) context[CONTEXT_KEY_URL];
        var policyOfUrl = _policyPerHostname[url];
        
        policyOfUrl.Execute(() => connection.Connect((string)context[CONTEXT_KEY_URL], _configuration.Port));                    
    },
    context: new Context {
        [CONTEXT_KEY_URL] = _configuration.HostNames.First()
    }
);

The object _policyPerHostname is just a Dictionary<string, Policy> where the key is the single hostname. In this way i can store the local state on the breaker per each hostname, so if it fails i keep the fail and on every request that I recive it falls back almost instantly to the working hostname.

So my question is how can i store the state? My idea was to keep the service that stores thìs whole logic Scoped. Then every time that i initialize the service i fetch the state from redis and by using the hooks like onReset, onBreak, onHalfOpen i store the new state… But i would prefer something like more native support from the library.

Issue Analytics

  • State:closed
  • Created 2 months ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
martincostellocommented, Jul 28, 2023

Hey there - we don’t expose the internal state of the circuit breakers, and we have no plans to do so. This use case isn’t something we would support in Polly.

I would recommend not attempting to do this and have each instance of your application have its own view of the state of the remote service with regards to the circuit breakers. For instance, if you had N > 1 instances and only a subset of them had a network routing issue that meant it could not successfully use the AD domain controller, then that degradation would spread to all of the instances even though they may not have the same problems.

0reactions
martincostellocommented, Jul 29, 2023

In that case I would stick with my original recommendation of not trying to synchronise the states between the instances.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Where is Circuit Breaker State stored? #259
Hi Team, i would like to know where Circuit Breaker state is stored. is it stored in memory ? is it possible to...
Read more >
Circuit Breaker Pattern. | Insider Engineering
Normally circuit breaker states need to be kept as permanently. We keep it in the Redis. For simplicity in this example, we will...
Read more >
When you use the Polly circuit-breaker, make sure ...
An issue in the external dependency, the exchange rate API going down, ... Circuit breaker state diagram taken from the Polly documentation.
Read more >
[Circuit Breaker] Can this component be used in an ...
Hello Carlos,. Regarding the usage of ASP.Net memory cache for circuit breaker persistence. Am I stating correctly that this component does ...
Read more >
Secure integrations with Circuit Breaker in Go
Let's say we have a high load online store with SMS notifications on order creation. Message sends via some external API and it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found