Round #721
10 May 2026, 15:13· csharp· Meta-Llama-3.3-70B-Instruct· r13-clean-hints
- Complexity
- band 1
- ZP / UO / OBS
- 4 / 3 / 4
- Journey weight
- 652
- Lightshift ΔL
- +0.25
Prompt
Add a fallback resilience pipeline using ResiliencePipeline.AddFallback that swaps to a cached IDistributedCache value on inner failure.
Zero-point index (what the engine surfaced)
ZPI-ZPI-0020 | 2026-05-10T15:13:50Z
Add a fallback resilience pipeline using ResiliencePipeline.AddFallback that swaps to a cached IDistributedCache value on inner failure.
ZERO POINT — 4 entries (structural invariants)
● hsl(329.61, 100%, 40%) IDistributedCache [contract]
● hsl(145.55, 100%, 36%) IMemoryCache [contract]
● hsl(296.42, 100%, 50%) ResiliencePipeline [contract]
● hsl(211.57, 100%, 50%) ResiliencePipelineBuilder [contract]
UNOBSERVED — 3 entries (implied truths)
○ hsl(27.69, 100%, 50%) async [feature]
○ hsl(49.23, 100%, 50%) try [feature]
○ hsl(144.00, 100%, 42%) Connection resilience [function]
OBSERVED — 4 entries (explicit in request)
◉ hsl(73.85, 100%, 15%) on [feature]
◉ hsl(85.13, 100%, 50%) using [feature]
◉ hsl(92.31, 100%, 15%) value [feature]
◉ hsl(234.87, 100%, 50%) using [feature]
COMPLEXITY │ 11 entries │ spread: 207° │ band: MEDIUM
Baseline response (no ZPI)
Here's an example of how you can add a fallback resilience pipeline using `ResiliencePipeline.AddFallback` in C#:
```csharp
using Microsoft.Extensions.Caching.Distributed;
using Polly;
public class MyService
{
private readonly IDistributedCache _distributedCache;
private readonly Policy<HttpResponseMessage> _resiliencePolicy;
public MyService(IDistributedCache distributedCache)
{
_distributedCache = distributedCache;
_resiliencePolicy = Policy.Handle<HttpRequestException>()
.Or<TimeoutException>()
.FallbackAsync(
async (context, token) =>
{
// Get the cached value
var cachedValue = await _distributedCache.GetStringAsync("myCacheKey");
// If the cached value exists, return it
if (cachedValue != null)
{
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(cachedValue)
};
}
// If the cached value does not exist, throw an exception
throw new Exception("No cached value available");
},
onFallbackAsync: async (exception, ts, context) =>
{
// Log the exception
Console.WriteLine($"Exception occurred: {exception.Message}");
}
);
}
public async Task<HttpResponseMessage> GetDataAsync()
{
// Use the resilience policy to execute the HTTP request
return await _resiliencePolicy.ExecuteAsync(async () =>
{
// Simulate an HTTP request
var httpClient = new HttpClient();
var response = await httpClient.GetAsync("https://example.com/api/data");
// If the response is successful, return it
if (response.IsSuccessStatusCode)
{
return response;
}
// If the response is not successful, throw an exception
throw new HttpRequestException("Failed to retrieve data");
});
}
}
```
In this example, the `MyService` class uses the `IDistributedCache` interface to cache values. The `ResiliencePipeline.AddFallback` method is used to add a fallback policy that swaps to a cached value on inner failure.
When the `GetDataAsync` method is called, it uses the resilience policy to execute an HTTP request. If the request fails, the fallback policy is triggered, which attempts to retrieve the cached value. If the cached value exists, it is returned; otherwise, an exception is thrown.
Note that you need to configure the `IDistributedCache` instance and the `ResiliencePipeline` policy in the `Startup.cs` file or in a separate configuration file.
Here's an example of how you can configure the `IDistributedCache` instance and the `ResiliencePipeline` policy in the `Startup.cs` file:
```csharp
public void ConfigureServices(IServiceCollection services)
{
services.AddDistributedMemoryCache();
services.AddHttpClient("myHttpClient")
.AddPolicyHandler(Policy.Handle<HttpRequestException>()
.Or<TimeoutException>()
.FallbackAsync(
async (context, token) =>
{
// Get the cached value
var cachedValue = await _distributedCache.GetStringAsync("myCacheKey");
// If the cached value exists, return it
if (cachedValue != null)
{
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(cachedValue)
};
}
// If the cached value does not exist, throw an exception
throw new Exception("No cached value available");
},
onFallbackAsync: async (exception, ts, context) =>
{
// Log the exception
Console.WriteLine($"Exception occurred: {exception.Message}");
}
));
}
```
You can then inject the `IHttpClientFactory` instance into your service and use it to create an instance of the `HttpClient` class:
```csharp
public class MyService
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly IDistributedCache _distributedCache;
public MyService(IHttpClientFactory httpClientFactory, IDistributedCache distributedCache)
{
_httpClientFactory = httpClientFactory;
_distributedCache = distributedCache;
}
public async Task<HttpResponseMessage> GetDataAsync()
{
var httpClient = _httpClientFactory.CreateClient("myHttpClient");
var response = await httpClient.GetAsync("https://example.com/api/data");
// If the response is successful, return it
if (response.IsSuccessStatusCode)
{
return response;
}
// If the response is not successful, throw an exception
throw new HttpRequestException("Failed to retrieve data");
}
}
```
Enhanced response (with ZPI)
```csharp
using Microsoft.Extensions.Caching.Distributed;
using Polly;
public class ResiliencePipelineBuilder
{
private readonly IDistributedCache _distributedCache;
public ResiliencePipelineBuilder(IDistributedCache distributedCache)
{
_distributedCache = distributedCache;
}
public ResiliencePipeline BuildPipeline()
{
return Policy.Handle<Exception>()
.FallbackAsync(
async (context, token) =>
{
var cacheKey = context["CacheKey"];
var cachedValue = await _distributedCache.GetStringAsync(cacheKey, token);
return cachedValue;
},
onFallbackAsync: async (exception, span, context, token) =>
{
// Log the exception or perform other actions on fallback
Console.WriteLine($"Fallback occurred: {exception.Message}");
}
);
}
}
public class Program
{
public static async Task Main(string[] args)
{
var distributedCache = new DistributedCacheMock(); // Replace with actual implementation
var pipelineBuilder = new ResiliencePipelineBuilder(distributedCache);
var pipeline = pipelineBuilder.BuildPipeline();
// Usage example
var cacheKey = "example-key";
var result = await pipeline.ExecuteAsync(async (token) =>
{
// Simulate an inner failure
throw new Exception("Inner failure");
}, new Dictionary<string, object> { { "CacheKey", cacheKey } });
Console.WriteLine(result);
}
}
public class DistributedCacheMock : IDistributedCache
{
public byte[] Get(string key)
{
// Mock implementation, replace with actual cache retrieval
return System.Text.Encoding.UTF8.GetBytes("Cached value");
}
public Task<byte[]> GetAsync(string key, CancellationToken token = default)
{
// Mock implementation, replace with actual cache retrieval
return Task.FromResult(System.Text.Encoding.UTF8.GetBytes("Cached value"));
}
public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
{
// Mock implementation, replace with actual cache storage
}
public Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default)
{
// Mock implementation, replace with actual cache storage
return Task.CompletedTask;
}
public void Refresh(string key)
{
// Mock implementation, replace with actual cache refresh
}
public Task RefreshAsync(string key, CancellationToken token = default)
{
// Mock implementation, replace with actual cache refresh
return Task.CompletedTask;
}
public void Remove(string key)
{
// Mock implementation, replace with actual cache removal
}
public Task RemoveAsync(string key, CancellationToken token = default)
{
// Mock implementation, replace with actual cache removal
return Task.CompletedTask;
}
public string GetString(string key)
{
// Mock implementation, replace with actual cache retrieval
return "Cached value";
}
public Task<string> GetStringAsync(string key, CancellationToken token = default)
{
// Mock implementation, replace with actual cache retrieval
return Task.FromResult("Cached value");
}
public void SetString(string key, string value, DistributedCacheEntryOptions options)
{
// Mock implementation, replace with actual cache storage
}
public Task SetStringAsync(string key, string value, DistributedCacheEntryOptions options, CancellationToken token = default)
{
// Mock implementation, replace with actual cache storage
return Task.CompletedTask;
}
}
```
This implementation utilizes the Polly library to create a resilience pipeline with a fallback strategy. When an inner failure occurs, the pipeline will attempt to retrieve a cached value from the `IDistributedCache` instance. If the cache retrieval is successful, the cached value will be returned; otherwise, the pipeline will propagate the exception.
The `ResiliencePipelineBuilder` class is responsible for constructing the pipeline with the specified fallback strategy. The `BuildPipeline` method returns a `ResiliencePipeline` instance that can be used to execute actions with the fallback resilience strategy.
In the example usage, a mock `IDistributedCache` implementation is used for demonstration purposes. You should replace this with an actual implementation, such as the `Microsoft.Extensions.Caching.StackExchangeRedis` package for Redis caching.
The `Main` method demonstrates how to use the `ResiliencePipeline` instance to execute an action with the fallback resilience strategy. The `ExecuteAsync` method takes an action to execute and a dictionary of context data, which includes the cache key used for fallback retrieval. If an inner failure occurs during the action execution, the pipeline will attempt to retrieve the cached value and return it as the result.
Grader chain
Each grader reads the same baseline + enhanced pair and casts an independent verdict. Disagreement is the framework's own signal — not noise to be averaged away.
V1samba/Meta-Llama-3.3-70B-Instruct
BetterV4qwen2.5-coder-3b-instruct
BetterV5openrouter/qwen/qwen3-235b-a22b-2507
BetterV7qwen2.5-coder-7b-instruct
BetterV8openai/gpt-4.1
BetterV9anthropic/claude-opus-4-7
WorseV12openai/gpt-4o
BetterComments
Our grader said what it said. What do you say? Comment as a guest below.
No comments yet. Be the first to say what you make of this round.