The Resilience SDK is a .NET-based library designed to provide robust and configurable resilience strategies for applications handling billions of requests per second. It includes implementations for Circuit Breakers and Load Shedders, enabling applications to gracefully handle failures and maintain stability under high-load conditions.
- Protects systems from cascading failures by monitoring failures and opening circuits when thresholds are exceeded:
- Basic Circuit Breaker: Monitors failure counts and enforces timeouts.
- Error Rate Circuit Breaker: Tracks error rates over a sliding window to trigger state transitions.
- Prevents overload by rejecting low-priority requests when the system load exceeds a defined threshold:
- Static Load Shedder: Uses a fixed threshold to shed low-priority traffic.
- Responsive Load Shedder: Dynamically adjusts thresholds based on historical metrics and current conditions.
- Provides distributed state management for resilience components.
- Supports Redis for distributed caching.
- Customizable caching interface for other backends.
- Resilience strategies can be fully customized through JSON-based configuration, allowing runtime adjustments without redeployments.
- Distributed State Management: Integrates with Redis or other distributed caches to synchronize thresholds, failure metrics, and load states.
- Thread-Safe Updates: Supports safe updates to thresholds and metrics in both synchronous and asynchronous contexts.
- Add custom circuit breakers, load shedders or caching strategies by implementing the provided interfaces:
- ICircuitBreaker
- ILoadShedder
- IResilienceDistributedCache
resilience.sln
CircuitBreakerDemo/
Program.cs
resilienceConfig.json
Resilience/
CircuitBreaker/
BasicCircuitBreaker.cs
CircuitBreaker.cs
CircuitBreakerFactory.cs
ErrorRateCircuitBreaker.cs
Configuration/
ComponentConfiguration.cs
ResilienceConfigParser.cs
ResilienceConfiguration.cs
Caching/
IResilienceDistributedCache.cs
RedisResilienceDistributedCache.cs
LoadShedder/
StaticLoadShedder.cs
ResponsiveLoadShedder.cs
LoadShedderFactory.cs- .NET 9.0 SDK or later
- Visual Studio or Visual Studio Code
- Clone the repository:
git clone https://github.com/vrn11/resilience.git cd resilience - Build the solution:
dotnet build resilience.sln- Run the demo application:
dotnet run --project ResilienceDemo/ResilienceDemo.csprojThe resilience strategies are configured using a JSON file (resilienceConfig.json). Below is an example configuration:
{
"Gateways": {
"CommonSettings": {
"FailureThreshold": 5
},
"CircuitBreaker": {
"Type": "latency",
"Options": {
"OpenTimeout": "00:00:01"
}
},
"LoadShedder": {
"Type": "static",
"Options": {
"LoadThreshold": 0.8
}
},
"Cache": {
"Type": "redis",
"Options": {
"ConnectionString": "localhost:6379"
}
}
}
}The demo application demonstrates how to use the SDK:
string configPath = "resilienceConfig.json";
ResilienceConfiguration config = await ResilienceConfigParser.ParseConfigurationAsync(configPath);
// Circuit Breaker Initialization
ICircuitBreaker circuitBreaker = CircuitBreakerFactory.Create(
config.Gateways.CircuitBreaker.Type,
config.Gateways.CircuitBreaker.Options
);
// Load Shedder Initialization
ILoadShedder loadShedder = LoadShedderFactory.Create(
config.Gateways.LoadShedder.Type,
() => new Random().NextDouble(), // Load monitor function
config.Gateways.LoadShedder.Options
);// Execute an action using Load Shedder and Circuit Breaker
string result = await loadShedder.ExecuteAsync(
RequestPriority.Medium,
async () => await circuitBreaker.ExecuteAsync(
async () => "Success",
async () => "Circuit Breaker Fallback"
),
async () => "Load Shedder Fallback"
);
Console.WriteLine(result);- Implement the ICircuitBreaker interface.
- Add your implementation to the CircuitBreakerFactory.
- Implement the ILoadShedder interface.
- Add your implementation to the LoadShedderFactory.
- Implement the IResilienceDistributedCache interface.
- Use your implementation in the circuit breaker or load shedder.
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a feature branch.
- Submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
- Inspired by resilience patterns such as Circuit Breaker and Load Shedding.
- Built with .NET 9.0.
- Redis integration powered by StackExchange.Redis.