Round #209

07 May 2026, 13:54· csharp· meta/llama-3.3-70b-instruct· r12-real-contracts
Complexity
band 0
ZP / UO / OBS
4 / 0 / 0
Journey weight
316
Lightshift ΔL

Prompt

Implement idempotency key middleware that deduplicates POST requests

Zero-point index (what the engine surfaced)

ZPI-ZPI-0002 | 2026-05-07T13:54:27Z Implement idempotency key middleware that deduplicates POST requests ZERO POINT — 4 entries (structural invariants) ● hsl(314.84, 100%, 21%) IMiddleware [contract] ● hsl(280.69, 100%, 21%) RequestDelegate [contract] ● hsl(329.61, 100%, 21%) IDistributedCache [contract] ● hsl(136.10, 100%, 21%) DbContext [contract] UNOBSERVED — 0 entries (implied truths) OBSERVED — 0 entries (explicit in request) COMPLEXITY │ 4 entries │ spread: 0° │ band: LOW

Baseline response (no ZPI)

Idempotency Key Middleware ========================== The idempotency key middleware is used to prevent duplicate POST requests from being processed multiple times. This is particularly useful in scenarios where a client may accidentally send the same request multiple times, such as when a user clicks a submit button multiple times. Implementation -------------- Below is an example implementation of idempotency key middleware in C#: ```csharp using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Extensions; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; public class IdempotencyKeyMiddleware { private readonly RequestDelegate _next; private readonly ILogger<IdempotencyKeyMiddleware> _logger; private readonly IIdempotencyKeyStore _idempotencyKeyStore; public IdempotencyKeyMiddleware(RequestDelegate next, ILogger<IdempotencyKeyMiddleware> logger, IIdempotencyKeyStore idempotencyKeyStore) { _next = next; _logger = logger; _idempotencyKeyStore = idempotencyKeyStore; } public async Task InvokeAsync(HttpContext context) { if (context.Request.Method == "POST") { var idempotencyKey = context.Request.Headers["Idempotency-Key"].FirstOrDefault(); if (!string.IsNullOrEmpty(idempotencyKey)) { var requestUri = context.Request.GetEncodedUrl(); var requestMethod = context.Request.Method; var requestBody = await GetRequestBody(context); var idempotencyKeyEntry = await _idempotencyKeyStore.GetIdempotencyKeyEntry(idempotencyKey); if (idempotencyKeyEntry != null && idempotencyKeyEntry.RequestUri == requestUri && idempotencyKeyEntry.RequestMethod == requestMethod && idempotencyKeyEntry.RequestBody == requestBody) { _logger.LogInformation($"Duplicate request detected for idempotency key {idempotencyKey}. Returning cached response."); context.Response.StatusCode = idempotencyKeyEntry.StatusCode; await context.Response.WriteAsync(idempotencyKeyEntry.ResponseBody); return; } else { await _idempotencyKeyStore.SaveIdempotencyKeyEntry(idempotencyKey, requestUri, requestMethod, requestBody); } } } await _next(context); } private async Task<string> GetRequestBody(HttpContext context) { context.Request.EnableBuffering(); var buffer = new byte[context.Request.ContentLength.Value]; await context.Request.Body.ReadAsync(buffer); context.Request.Body.Position = 0; return System.Text.Encoding.UTF8.GetString(buffer); } } public interface IIdempotencyKeyStore { Task SaveIdempotencyKeyEntry(string idempotencyKey, string requestUri, string requestMethod, string requestBody); Task<IdempotencyKeyEntry> GetIdempotencyKeyEntry(string idempotencyKey); } public class IdempotencyKeyEntry { public string IdempotencyKey { get; set; } public string RequestUri { get; set; } public string RequestMethod { get; set; } public string RequestBody { get; set; } public int StatusCode { get; set; } public string ResponseBody { get; set; } } public class InMemoryIdempotencyKeyStore : IIdempotencyKeyStore { private readonly Dictionary<string, IdempotencyKeyEntry> _idempotencyKeyEntries = new Dictionary<string, IdempotencyKeyEntry>(); public async Task SaveIdempotencyKeyEntry(string idempotencyKey, string requestUri, string requestMethod, string requestBody) { _idempotencyKeyEntries[idempotencyKey] = new IdempotencyKeyEntry { IdempotencyKey = idempotencyKey, RequestUri = requestUri, RequestMethod = requestMethod, RequestBody = requestBody }; } public async Task<IdempotencyKeyEntry> GetIdempotencyKeyEntry(string idempotencyKey) { if (_idempotencyKeyEntries.TryGetValue(idempotencyKey, out var idempotencyKeyEntry)) { return idempotencyKeyEntry; } return null; } } ``` Usage ----- To use the idempotency key middleware, you need to add it to the ASP.NET Core pipeline in the `Startup.cs` file: ```csharp public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseRouting(); app.UseMiddleware<IdempotencyKeyMiddleware>(new InMemoryIdempotencyKeyStore()); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } ```

Enhanced response (with ZPI)

### Idempotency Key Middleware Implementation The following implementation utilizes the `IMiddleware` interface to create a middleware that checks for idempotency keys in incoming POST requests. If a key is found, it checks the distributed cache to see if the request has already been processed. If it has, the middleware returns a cached response instead of passing the request to the next middleware in the pipeline. #### IdempotencyKeyMiddleware.cs ```csharp using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Logging; using System; using System.IO; using System.Text; using System.Threading.Tasks; public class IdempotencyKeyMiddleware { private readonly RequestDelegate _next; private readonly IDistributedCache _cache; private readonly ILogger<IdempotencyKeyMiddleware> _logger; public IdempotencyKeyMiddleware(RequestDelegate next, IDistributedCache cache, ILogger<IdempotencyKeyMiddleware> logger) { _next = next; _cache = cache; _logger = logger; } public async Task InvokeAsync(HttpContext context) { if (context.Request.Method == HttpMethods.Post) { var idempotencyKey = context.Request.Headers["Idempotency-Key"].FirstOrDefault(); if (!string.IsNullOrEmpty(idempotencyKey)) { var cachedResponse = await _cache.GetStringAsync(idempotencyKey); if (cachedResponse != null) { _logger.LogInformation($"Returning cached response for idempotency key {idempotencyKey}"); context.Response.StatusCode = 200; await context.Response.WriteAsync(cachedResponse); return; } } } await _next(context); if (context.Request.Method == HttpMethods.Post) { var idempotencyKey = context.Request.Headers["Idempotency-Key"].FirstOrDefault(); if (!string.IsNullOrEmpty(idempotencyKey)) { var responseBody = await GetResponseBody(context); await _cache.SetStringAsync(idempotencyKey, responseBody, new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1) }); _logger.LogInformation($"Cached response for idempotency key {idempotencyKey}"); } } } private async Task<string> GetResponseBody(HttpContext context) { var responseBody = string.Empty; var stream = context.Response.Body; using var memoryStream = new MemoryStream(); context.Response.Body = memoryStream; await context.Response.WriteAsync(string.Empty); memoryStream.Position = 0; using var reader = new StreamReader(memoryStream); responseBody = await reader.ReadToEndAsync(); context.Response.Body = stream; return responseBody; } } ``` #### Startup.cs (or Program.cs in .NET 6+) ```csharp using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddDistributedMemoryCache(); services.AddControllers(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseRouting(); app.UseMiddleware<IdempotencyKeyMiddleware>(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } ``` ### Explanation This implementation uses the `IMiddleware` interface to create a middleware that checks for idempotency keys in incoming POST requests. If a key is found, it checks the distributed cache to see if the request has already been processed. If it has, the middleware returns a cached response instead of passing the request to the next middleware in the pipeline. The `IdempotencyKeyMiddleware` class takes in the `RequestDelegate`, `IDistributedCache`, and `ILogger` instances through its constructor. The `InvokeAsync` method is where the middleware's logic is implemented. In the `InvokeAsync` method, the middleware first checks if the incoming request is a POST request. If it is, it checks for the presence of an "Idempotency-Key" header. If the header is present, it checks the distributed cache for a cached response associated with the idempotency key. If a cached response is found, it returns the cached response instead of passing the request to the next middleware in the pipeline. If no cached response is found, the middleware passes the request to the next middleware in the pipeline. After the request has been processed, the middleware checks again for the presence of an "Idempotency-Key" header. If the header is present, it caches the response body associated with the idempotency key in the distributed cache. The `GetResponseBody` method is a helper method that reads the response body from the `HttpContext` instance. This method is used to cache the response body after the request has been processed. In the `Startup` class, the `IdempotencyKeyMiddleware` instance is added to the middleware pipeline using the `UseMiddleware`

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.

V1claude-haiku-4-5-20251001
Worse
V4qwen2.5-coder-7b-instruct
Better

Comments

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.

← back to all rounds