Round #232

07 May 2026, 15:43· csharp· meta/llama-3.3-70b-instruct· r12-real-contracts
Complexity
band 1
ZP / UO / OBS
5 / 4 / 2
Journey weight
810
Lightshift ΔL

Prompt

Write a distributed transaction coordinator using the Saga pattern

Zero-point index (what the engine surfaced)

ZPI-ZPI-0025 | 2026-05-07T15:43:05Z Write a distributed transaction coordinator using the Saga pattern ZERO POINT — 5 entries (structural invariants) ● hsl(27.69, 100%, 50%) async [feature] ● hsl(145.64, 100%, 16%) Task of T [feature] ● hsl(7.18, 100%, 18%) class [feature] ● hsl(0.00, 100%, 18%) public [feature] ● hsl(84.10, 100%, 18%) namespace [feature] UNOBSERVED — 4 entries (implied truths) ○ hsl(199.38, 100%, 16%) Redis distributed cache [function] ○ hsl(188.31, 100%, 16%) Distributed cache (IDistributedCache) [function] ○ hsl(31.38, 100%, 40%) Scoped registration [function] ○ hsl(44.31, 100%, 25%) IOptions of T binding [function] OBSERVED — 2 entries (explicit in request) ◉ hsl(85.13, 100%, 36%) using [feature] ◉ hsl(234.87, 100%, 36%) using [feature] COMPLEXITY │ 11 entries │ spread: 203° │ band: MEDIUM

Baseline response (no ZPI)

**Distributed Transaction Coordinator using the Saga Pattern** =========================================================== The Saga pattern is a design pattern that helps to manage distributed transactions across multiple services. It ensures that either all or none of the services complete their transactions, maintaining data consistency across the system. **Overview of the Saga Pattern** ----------------------------- The Saga pattern involves the following components: * **Saga Orchestrator**: Responsible for managing the distributed transaction. * **Local Transactions**: Each service performs its own local transaction. * **Compensating Transactions**: In case of a failure, each service performs a compensating transaction to undo the changes made by the local transaction. **Implementation in C#** ---------------------- Below is an example implementation of a distributed transaction coordinator using the Saga pattern in C#: ```csharp using System; using System.Collections.Generic; using System.Threading.Tasks; // Define the Saga orchestrator interface public interface ISagaOrchestrator { Task ExecuteSagaAsync(Saga saga); } // Define the Saga class public class Saga { public string Id { get; set; } public List<SagaStep> Steps { get; set; } } // Define the Saga step class public class SagaStep { public string ServiceName { get; set; } public Func<Task> Execute { get; set; } public Func<Task> Compensate { get; set; } } // Implement the Saga orchestrator public class SagaOrchestrator : ISagaOrchestrator { public async Task ExecuteSagaAsync(Saga saga) { var executedSteps = new List<SagaStep>(); try { // Execute each step in the saga foreach (var step in saga.Steps) { await step.Execute(); executedSteps.Add(step); } // If all steps are executed successfully, commit the saga await CommitSagaAsync(saga); } catch (Exception ex) { // If any step fails, compensate the executed steps await CompensateStepsAsync(executedSteps); throw; } } private async Task CommitSagaAsync(Saga saga) { // Commit the saga (e.g., save the saga state to a database) Console.WriteLine("Saga committed"); } private async Task CompensateStepsAsync(List<SagaStep> executedSteps) { // Compensate each executed step in reverse order for (int i = executedSteps.Count - 1; i >= 0; i--) { await executedSteps[i].Compensate(); } } } // Example usage public class Program { public static async Task Main(string[] args) { var sagaOrchestrator = new SagaOrchestrator(); var saga = new Saga { Id = "example-saga", Steps = new List<SagaStep> { new SagaStep { ServiceName = "Service A", Execute = async () => { Console.WriteLine("Executing Service A"); await Task.Delay(1000); }, Compensate = async () => { Console.WriteLine("Compensating Service A"); await Task.Delay(1000); } }, new SagaStep { ServiceName = "Service B", Execute = async () => { Console.WriteLine("Executing Service B"); await Task.Delay(1000); throw new Exception("Example exception"); }, Compensate = async () => { Console.WriteLine("Compensating Service B"); await Task.Delay(1000); } }, new SagaStep { ServiceName = "Service C", Execute = async () => { Console.WriteLine("Executing Service C"); await Task.Delay(1000); }, Compensate = async () => { Console.WriteLine("Compensating Service C"); await Task.Delay(1000); } } } }; try { await sagaOrchestrator.ExecuteSagaAsync(saga); } catch (Exception ex) { Console.WriteLine($"Saga execution failed: {ex.Message}"); } } } ``` **Explanation** -------------- In this example, we define a `Saga` class that represents a distributed transaction. The `Saga` class contains a list of `SagaStep` objects, each representing a local transaction in a service. The `SagaOrchestrator` class is responsible for managing the distributed transaction. It executes each step in the saga and compensates the executed steps if any step fails. In the example usage, we create a `Saga` object with three steps, each representing a local transaction in a service. We then execute the saga using the `SagaOrchestrator`. If any step fails, the `SagaOrchestrator` compensates the executed steps to maintain data consistency. **Advantages** ------------ The Saga pattern provides several advantages, including: * **Data consistency**: The Saga pattern

Enhanced response (with ZPI)

```csharp using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using StackExchange.Redis; using System; using System.Collections.Generic; using System.Threading.Tasks; // Define the Saga transaction step public interface ISagaStep { Task ExecuteAsync(); Task CompensateAsync(); } // Define the Saga transaction coordinator public class SagaTransactionCoordinator { private readonly IDistributedCache _distributedCache; private readonly IOptions<SagaOptions> _options; public SagaTransactionCoordinator(IDistributedCache distributedCache, IOptions<SagaOptions> options) { _distributedCache = distributedCache; _options = options; } // Start a new Saga transaction public async Task StartTransactionAsync(List<ISagaStep> steps) { // Create a new transaction ID var transactionId = Guid.NewGuid().ToString(); // Store the transaction steps in the distributed cache await _distributedCache.SetStringAsync(transactionId, Newtonsoft.Json.JsonConvert.SerializeObject(steps)); try { // Execute each step in the transaction foreach (var step in steps) { await step.ExecuteAsync(); } // If all steps complete successfully, remove the transaction from the cache await _distributedCache.RemoveAsync(transactionId); } catch (Exception ex) { // If an error occurs, compensate each step in reverse order foreach (var step in steps.AsEnumerable().Reverse()) { await step.CompensateAsync(); } // Remove the transaction from the cache await _distributedCache.RemoveAsync(transactionId); // Rethrow the exception throw ex; } } } // Define the Saga options public class SagaOptions { public string RedisConnectionString { get; set; } } // Register the Saga transaction coordinator in the DI container public static class SagaRegistration { public static void AddSagaTransactionCoordinator(this IServiceCollection services, string redisConnectionString) { services.AddDistributedRedisCache(options => { options.InstanceName = "SagaTransactionCoordinator"; options.Configuration = redisConnectionString; }); services.Configure<SagaOptions>(options => { options.RedisConnectionString = redisConnectionString; }); services.AddSingleton<SagaTransactionCoordinator>(); } } // Example usage public class Program { public static async Task Main(string[] args) { var services = new ServiceCollection(); services.AddSagaTransactionCoordinator("localhost"); var serviceProvider = services.BuildServiceProvider(); var sagaTransactionCoordinator = serviceProvider.GetService<SagaTransactionCoordinator>(); var steps = new List<ISagaStep> { new ExampleSagaStep(), new ExampleSagaStep(), }; await sagaTransactionCoordinator.StartTransactionAsync(steps); } } // Example Saga step public class ExampleSagaStep : ISagaStep { public async Task ExecuteAsync() { Console.WriteLine("Executing example step"); await Task.Delay(1000); } public async Task CompensateAsync() { Console.WriteLine("Compensating example step"); await Task.Delay(1000); } } ``` This implementation provides a distributed transaction coordinator using the Saga pattern. The `SagaTransactionCoordinator` class is responsible for managing the transaction, executing each step, and compensating for any errors that occur. The `ISagaStep` interface defines the contract for each step in the transaction, which must implement the `ExecuteAsync` and `CompensateAsync` methods. The `SagaOptions` class stores the Redis connection string, which is used to configure the distributed cache. The `AddSagaTransactionCoordinator` method registers the `SagaTransactionCoordinator` instance in the DI container, along with the distributed Redis cache and Saga options. In the example usage, we create a new `ServiceCollection`, add the `SagaTransactionCoordinator` instance, and build the service provider. We then retrieve the `SagaTransactionCoordinator` instance and start a new transaction with two example steps. Note that this implementation uses the `IDistributedCache` interface to store the transaction steps in a distributed cache, which allows the transaction to be recovered in case of a failure. The `StackExchange.Redis` package is used to implement the distributed Redis cache.

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
Better
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