Middle · Trace · Outbox leases, replay and stale acknowledgments

Portable .NET9 console only: predict every printed line and explain how known times, owner identity and generation determine the four booleans. State which real HTTP, SQL or infrastructure guarantee this program does not test.

Think through your answer before opening the explanation.

Production engineering

using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

var first=new Lease("one",10,1); Console.WriteLine("owned:"+(5<first.Until)); Console.WriteLine("expired:"+(11>=first.Until));
var current=new Lease("two",21,2); bool Ack(Lease offered)=>offered.Owner==current.Owner&&offered.Generation==current.Generation;
Console.WriteLine("stale ack:"+Ack(first)); Console.WriteLine("current ack:"+Ack(current));
record Lease(string Owner,int Until,int Generation);
Reveal answer and reasoning

The first Lease belongs to one, expires at model time ten and has generation one. Comparing model time five prints owned:True, and comparing eleven prints expired:True. The replacement lease belongs to two with generation two. The Ack helper checks only offered owner and generation against that replacement, so the remaining lines are stale ack:False and current ack:True. It does not read a clock during Ack or perform a database update.

The real endpoint adds expiry and persisted-state conditions; those stronger checks must not be inferred from the smaller helper.

After one real order creates an outbox row, owner one claims it with attempt generation one. Another claim inside that lease finds no eligible row. After the short actual deadline passes, owner two reclaims the same row with generation two. An old acknowledgment and an acknowledgment carrying the wrong generation both receive409; the current owner and generation can mark it sent with204.

A later claim finds no pending work. The test records actual state transitions and allows elapsed scheduling time, rather than treating the500ms lease as an exact latency expectation or a universal production duration.

What a strong answer covers

  • The model prints ownedTrue, expiredTrue, stale ackFalse and current ackTrue.
  • Actual acknowledgments also check persisted status, generation and expiry.
  • A reclaimed lease implies possible duplicate delivery, not an exactly-once effect.

Common pitfall

Treating a reusable owner name or a lease expiry as proof of current ownership and non-delivery.

Follow-up questions

  1. What happens when the same worker name is reused for a newer claim?
  2. How would an already completed remote delivery be reconciled after local lease loss?

Self-review

Choose what to revisit. These marks are your own assessment and are separate from lesson completion.

Primary references

Open lesson and code ↗

← All interview questions