Round #493
09 May 2026, 19:51· csharp· qwen2.5-coder-7b-instruct· r13-clean-hints
- Complexity
- band 0
- ZP / UO / OBS
- 4 / 0 / 0
- Journey weight
- 222
- Lightshift ΔL
- +0.25
Prompt
Implement idempotency key middleware that deduplicates POST requests
Zero-point index (what the engine surfaced)
ZPI-ZPI-0002 | 2026-05-09T19:51:23Z
Implement idempotency key middleware that deduplicates POST requests
ZERO POINT — 4 entries (structural invariants)
● hsl(314.84, 100%, 50%) IMiddleware [contract]
● hsl(280.69, 100%, 50%) RequestDelegate [contract]
● hsl(329.61, 100%, 34%) IDistributedCache [contract]
● hsl(136.10, 100%, 44%) DbContext [contract]
UNOBSERVED — 0 entries (implied truths)
OBSERVED — 0 entries (explicit in request)
COMPLEXITY │ 4 entries │ spread: 0° │ band: LOW
Baseline response (no ZPI)
To implement an idempotency key middleware in C#, you can use ASP.NET Core middleware to intercept incoming POST requests, check for an idempotency key in the request headers or body, and store the response if it hasn't been processed yet. If the same request is received again with the same idempotency key, the middleware will return the stored response instead of processing the request again.
Here's a step-by-step guide to creating this middleware:
1. **Create a Middleware Class**: This class will handle the logic for checking and storing the idempotency keys.
2. **Register the Middleware in Startup.cs**: This ensures that the middleware is added to the request pipeline.
3. **Store Responses**: You can use an in-memory dictionary or a more persistent storage solution like Redis to store responses based on the idempotency key.
Below is a complete example:
### Step 1: Create the Middleware Class
```csharp
using Microsoft.AspNetCore.Http;
using System.Collections.Concurrent;
using System.IO;
using System.Threading.Tasks;
public class IdempotencyKeyMiddleware
{
private readonly RequestDelegate _next;
private readonly ConcurrentDictionary<string, (int statusCode, string responseBody)> _idempotencyStore;
public IdempotencyKeyMiddleware(RequestDelegate next)
{
_next = next;
_idempotencyStore = new ConcurrentDictionary<string, (int statusCode, string responseBody)>();
}
public async Task InvokeAsync(HttpContext context)
{
if (context.Request.Method == "POST")
{
var idempotencyKey = GetIdempotencyKey(context);
if (!string.IsNullOrEmpty(idempotencyKey))
{
if (_idempotencyStore.TryGetValue(idempotencyKey, out var storedResponse))
{
context.Response.StatusCode = storedResponse.statusCode;
await context.Response.WriteAsync(storedResponse.responseBody);
return;
}
}
}
// Call the next middleware in the pipeline
await _next(context);
if (context.Request.Method == "POST" && !string.IsNullOrEmpty(idempotencyKey))
{
var originalBody = await new StreamReader(context.Response.Body).ReadToEndAsync();
context.Response.Body.Seek(0, SeekOrigin.Begin);
context.Response.Body.SetLength(0);
// Store the response
_idempotencyStore[idempotencyKey] = (context.Response.StatusCode, originalBody);
}
}
private string GetIdempotencyKey(HttpContext context)
{
// Check for idempotency key in headers
if (context.Request.Headers.TryGetValue("X-Idempotency-Key", out var idempotencyKey))
{
return idempotencyKey;
}
// Check for idempotency key in body (for JSON payloads)
using (var reader = new StreamReader(context.Request.Body))
{
var requestBody = await reader.ReadToEndAsync();
context.Request.Body.Seek(0, SeekOrigin.Begin);
if (requestBody.Contains("\"idempotencyKey\":"))
{
// Parse the JSON to extract the idempotency key
var json = System.Text.Json.JsonDocument.Parse(requestBody);
return json.RootElement.GetProperty("idempotencyKey").GetString();
}
}
return null;
}
}
```
### Step 2: Register the Middleware in Startup.cs
```csharp
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// Add other services here
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMiddleware<IdempotencyKeyMiddleware>();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
```
### Step 3: Use the Middleware
Now, any POST request that includes an `X-Idempotency-Key` header or has an `idempotencyKey` field in the JSON body will be checked for duplicates. If a duplicate is detected, the middleware will return the stored response instead of processing the request again.
### Example Request
```http
POST /api/resource HTTP/1.1
Host: example.com
Content-Type: application/json
X-Idempotency-Key: 1234567890
{
"data": "example"
}
```
If this request is received again with the same `X-Idempotency-Key`, the middleware will return the stored response without processing the request again.
This implementation uses an in-memory dictionary to store responses, which is suitable for small-scale applications. For larger applications or production environments, consider using a more persistent storage solution like Redis.
Enhanced response (with ZPI)
To implement an idempotency key middleware that deduplicates POST requests, we can use the `IMiddleware` contract. This allows us to intercept and process HTTP requests in a pipeline before they reach the controller actions.
Here's how you can implement this:
1. **Create the Middleware Class**: We'll create a class that implements `IMiddleware`.
2. **Use an IDistributedCache**: To store and check idempotency keys, we'll use an `IDistributedCache` instance.
3. **Register the Middleware in DI**: We'll register the middleware in the dependency injection container.
### Implementation
```csharp
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Distributed;
using System.Text.Json;
using System.Threading.Tasks;
public class IdempotencyKeyMiddleware
{
private readonly RequestDelegate _next;
private readonly IDistributedCache _cache;
public IdempotencyKeyMiddleware(RequestDelegate next, IDistributedCache cache)
{
_next = next ?? throw new ArgumentNullException(nameof(next));
_cache = cache ?? throw new ArgumentNullException(nameof(cache));
}
public async Task InvokeAsync(HttpContext context)
{
if (context.Request.Method == "POST")
{
var idempotencyKey = await GetIdempotencyKeyFromRequest(context);
if (await IsDuplicateRequest(idempotencyKey))
{
context.Response.StatusCode = StatusCodes.Status409Conflict;
await context.Response.WriteAsync("Duplicate request detected.");
return;
}
// Store the idempotency key in cache
await _cache.SetStringAsync(idempotencyKey, JsonSerializer.Serialize(context.Request.Body), new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10) // Adjust as needed
});
}
await _next(context);
}
private async Task<string> GetIdempotencyKeyFromRequest(HttpContext context)
{
if (!context.Request.Headers.TryGetValue("X-Idempotency-Key", out var idempotencyKey))
{
throw new InvalidOperationException("Idempotency key header is missing.");
}
return idempotencyKey;
}
private async Task<bool> IsDuplicateRequest(string idempotencyKey)
{
var cachedBody = await _cache.GetStringAsync(idempotencyKey);
if (cachedBody != null)
{
// Compare the current request body with the cached body
using var reader1 = new System.IO.StringReader(await context.Request.Body.ReadAsStringAsync());
using var reader2 = new System.IO.StringReader(cachedBody);
return await reader1.ReadToEndAsync() == await reader2.ReadToEndAsync();
}
return false;
}
}
public static class IdempotencyKeyMiddlewareExtensions
{
public static IApplicationBuilder UseIdempotencyKeyMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<IdempotencyKeyMiddleware>();
}
}
```
### Explanation
1. **Middleware Class**:
- The `IdempotencyKeyMiddleware` class implements the `IMiddleware` interface.
- It takes a `RequestDelegate` and an `IDistributedCache` instance in its constructor.
2. **InvokeAsync Method**:
- This method is called for each request.
- If the request method is "POST", it retrieves the idempotency key from the request headers.
- It checks if the request is a duplicate by comparing the current request body with the cached body stored in the cache.
- If a duplicate is detected, it returns a 409 Conflict status.
- Otherwise, it stores the current request body in the cache.
3. **Extensions Class**:
- The `IdempotencyKeyMiddlewareExtensions` class provides an extension method to register the middleware in the application builder.
### Usage
To use this middleware, you need to register it in your `Startup.cs` or wherever you configure your middleware pipeline:
```csharp
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Other middleware registrations...
app.UseIdempotencyKeyMiddleware();
// Other middleware registrations...
}
```
This implementation ensures that POST requests are deduplicated based on the idempotency key provided in the request headers.
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
WorseV7qwen2.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.