Round #223

07 May 2026, 15:09· csharp· meta/llama-3.3-70b-instruct· r12-real-contracts
Complexity
band 0
ZP / UO / OBS
2 / 0 / 0
Journey weight
158
Lightshift ΔL

Prompt

Implement an outbox pattern that ensures at-least-once delivery

Zero-point index (what the engine surfaced)

ZPI-ZPI-0016 | 2026-05-07T15:09:16Z Implement an outbox pattern that ensures at-least-once delivery ZERO POINT — 2 entries (structural invariants) ● hsl(136.10, 100%, 21%) DbContext [contract] ● hsl(61.72, 100%, 21%) BackgroundService [contract] UNOBSERVED — 0 entries (implied truths) OBSERVED — 0 entries (explicit in request) COMPLEXITY │ 2 entries │ spread: 0° │ band: LOW

Baseline response (no ZPI)

**Outbox Pattern Implementation in C#** ===================================== The outbox pattern is a design pattern used to ensure at-least-once delivery of messages in a distributed system. It works by storing messages in a local database (the "outbox") before sending them to their destination. If the send operation fails, the message is retried until it is successfully delivered. **Database Schema** ------------------ The outbox pattern requires a database schema to store the messages. The schema should include the following columns: * `Id` (primary key): a unique identifier for the message * `Message`: the message payload * `Destination`: the destination of the message (e.g. a queue, topic, or API endpoint) * `Status`: the status of the message (e.g. "pending", "sent", "failed") * `RetryCount`: the number of times the message has been retried * `CreatedAt`: the timestamp when the message was created * `UpdatedAt`: the timestamp when the message was last updated **Outbox Class** ---------------- The `Outbox` class is responsible for storing and sending messages. It should have the following methods: * `AddMessage`: adds a new message to the outbox * `SendMessages`: sends all pending messages in the outbox * `RetryFailedMessages`: retries all failed messages in the outbox ```csharp using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Threading; using System.Threading.Tasks; public class Outbox { private readonly string _connectionString; private readonly int _maxRetryCount; public Outbox(string connectionString, int maxRetryCount) { _connectionString = connectionString; _maxRetryCount = maxRetryCount; } public async Task AddMessageAsync(string message, string destination) { using (var connection = new SqlConnection(_connectionString)) { await connection.OpenAsync(); var command = connection.CreateCommand(); command.CommandText = "INSERT INTO Outbox (Message, Destination, Status, RetryCount, CreatedAt, UpdatedAt) VALUES (@Message, @Destination, 'pending', 0, GETDATE(), GETDATE())"; command.Parameters.AddWithValue("@Message", message); command.Parameters.AddWithValue("@Destination", destination); await command.ExecuteNonQueryAsync(); } } public async Task SendMessagesAsync() { using (var connection = new SqlConnection(_connectionString)) { await connection.OpenAsync(); var command = connection.CreateCommand(); command.CommandText = "SELECT * FROM Outbox WHERE Status = 'pending'"; var reader = await command.ExecuteReaderAsync(); while (await reader.ReadAsync()) { var messageId = reader["Id"].ToString(); var message = reader["Message"].ToString(); var destination = reader["Destination"].ToString(); try { // Send the message to the destination await SendMessageAsync(message, destination); // Update the message status to "sent" var updateCommand = connection.CreateCommand(); updateCommand.CommandText = "UPDATE Outbox SET Status = 'sent', UpdatedAt = GETDATE() WHERE Id = @Id"; updateCommand.Parameters.AddWithValue("@Id", messageId); await updateCommand.ExecuteNonQueryAsync(); } catch (Exception ex) { // Update the message status to "failed" and increment the retry count var updateCommand = connection.CreateCommand(); updateCommand.CommandText = "UPDATE Outbox SET Status = 'failed', RetryCount = RetryCount + 1, UpdatedAt = GETDATE() WHERE Id = @Id"; updateCommand.Parameters.AddWithValue("@Id", messageId); await updateCommand.ExecuteNonQueryAsync(); // Log the error Console.WriteLine($"Error sending message {messageId}: {ex.Message}"); } } } } public async Task RetryFailedMessagesAsync() { using (var connection = new SqlConnection(_connectionString)) { await connection.OpenAsync(); var command = connection.CreateCommand(); command.CommandText = "SELECT * FROM Outbox WHERE Status = 'failed' AND RetryCount < @MaxRetryCount"; command.Parameters.AddWithValue("@MaxRetryCount", _maxRetryCount); var reader = await command.ExecuteReaderAsync(); while (await reader.ReadAsync()) { var messageId = reader["Id"].ToString(); var message = reader["Message"].ToString(); var destination = reader["Destination"].ToString(); try { // Send the message to the destination await SendMessageAsync(message, destination); // Update the message status to "sent" var updateCommand = connection.CreateCommand(); updateCommand.CommandText = "UPDATE Outbox SET Status = 'sent', UpdatedAt = GETDATE() WHERE Id = @Id"; updateCommand.Parameters.AddWithValue("@Id", messageId); await updateCommand.ExecuteNonQueryAsync(); } catch (Exception ex) { // Update the message status to "failed" and increment the retry count var updateCommand = connection.CreateCommand

Enhanced response (with ZPI)

### Outbox Pattern Implementation The outbox pattern is a design pattern used to ensure at-least-once delivery of messages in a distributed system. It works by storing messages in a database (the "outbox") before sending them to their destination. If a message fails to send, it will be retried until it is successfully delivered. #### Database Context We will use Entity Framework Core to interact with the database. First, let's define the `OutboxMessage` entity: ```csharp public class OutboxMessage { public int Id { get; set; } public string Destination { get; set; } public string Payload { get; set; } public DateTime CreatedAt { get; set; } public DateTime? SentAt { get; set; } public int Attempts { get; set; } } ``` Next, we'll define the `OutboxDbContext`: ```csharp public class OutboxDbContext : DbContext { public OutboxDbContext(DbContextOptions<OutboxDbContext> options) : base(options) { } public DbSet<OutboxMessage> OutboxMessages { get; set; } } ``` #### Outbox Service We'll create an `IOutboxService` interface to define the contract for the outbox service: ```csharp public interface IOutboxService { Task SendMessageAsync(string destination, string payload); Task ProcessOutboxAsync(); } ``` The `OutboxService` class will implement this interface: ```csharp public class OutboxService : IOutboxService { private readonly OutboxDbContext _dbContext; private readonly IMessageSender _messageSender; public OutboxService(OutboxDbContext dbContext, IMessageSender messageSender) { _dbContext = dbContext; _messageSender = messageSender; } public async Task SendMessageAsync(string destination, string payload) { var message = new OutboxMessage { Destination = destination, Payload = payload, CreatedAt = DateTime.UtcNow, Attempts = 0 }; _dbContext.OutboxMessages.Add(message); await _dbContext.SaveChangesAsync(); } public async Task ProcessOutboxAsync() { var messages = await _dbContext.OutboxMessages .Where(m => m.SentAt == null) .ToListAsync(); foreach (var message in messages) { try { await _messageSender.SendMessageAsync(message.Destination, message.Payload); message.SentAt = DateTime.UtcNow; message.Attempts++; _dbContext.OutboxMessages.Update(message); await _dbContext.SaveChangesAsync(); } catch (Exception ex) { message.Attempts++; _dbContext.OutboxMessages.Update(message); await _dbContext.SaveChangesAsync(); // Log the exception } } } } ``` #### Message Sender We'll create an `IMessageSender` interface to define the contract for the message sender: ```csharp public interface IMessageSender { Task SendMessageAsync(string destination, string payload); } ``` The `MessageSender` class will implement this interface: ```csharp public class MessageSender : IMessageSender { public async Task SendMessageAsync(string destination, string payload) { // Implement the logic to send the message to the destination // For example, using a messaging library like RabbitMQ or Azure Service Bus } } ``` #### Background Service We'll create a background service to process the outbox messages: ```csharp public class OutboxBackgroundService : BackgroundService { private readonly IOutboxService _outboxService; public OutboxBackgroundService(IOutboxService outboxService) { _outboxService = outboxService; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { await _outboxService.ProcessOutboxAsync(); await Task.Delay(TimeSpan.FromSeconds(10), stoppingToken); } } } ``` #### DI Registration We'll register the services in the `Program.cs` file: ```csharp builder.Services.AddDbContext<OutboxDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("OutboxDb"))); builder.Services.AddTransient<IOutboxService, OutboxService>(); builder.Services.AddTransient<IMessageSender, MessageSender>(); builder.Services.AddHostedService<OutboxBackgroundService>(); ``` ### Explanation The outbox pattern ensures at-least-once delivery of messages by storing them in a database before sending them to their destination. If a message fails to send, it will be retried until it is successfully delivered. The `OutboxService` class is responsible for sending messages and processing the outbox. The `SendMessageAsync` method adds a new message to the outbox, and the `ProcessOutboxAsync` method sends the messages in the outbox to their destinations. The `OutboxBackgroundService` class is

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