Middle · Trace · Per-aggregate ordering, gaps and poison records

Portable .NET9 console only: predict every printed line and explain how last sequence changes across a gap, a valid predecessor and a quarantined amount. 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;

int last=0; string Apply(int sequence,int amount) { if(sequence!=last+1)return "gap:"+(last+1); if(amount<0)return "quarantined"; last=sequence; return "applied:"+last; }
Console.WriteLine(Apply(2,1)); Console.WriteLine(Apply(1,1)); Console.WriteLine(Apply(2,-1)); Console.WriteLine(Apply(3,1)); Console.WriteLine(Apply(2,1));
Reveal answer and reasoning

The console begins with last zero. Sequence two is not next, so it prints gap:1. Sequence one with a valid amount advances last and prints applied:1. A negative amount at sequence two prints quarantined without advancing. Sequence three is therefore still too early and prints gap:2. Finally a valid two prints applied:2. The helper does not store message identities, perform duplicate detection or write dead letters; those are deliberately external capabilities.

Its exact five-line output explains the sequence policy while making clear that a failed observation is not an applied predecessor.

The real test sends independent message ids through the same progression, then inspects ordered_inbox, aggregate state and deadletters. It also replays an already applied id and tests incompatible reuse. The final fixture has two applied messages, last sequence two and one quarantined record. A new id carrying an old sequence is rejected as stale, while an exact applied replay changes nothing.

The corrected sequence-two input has its own specified identity rather than silently rewriting the meaning of a previously immutable message. These actual rows provide evidence for the local policy, not for external broker ordering or delivery retention.

What a strong answer covers

  • The console prints gap1, applied1, quarantined, gap2 and applied2.
  • Quarantine records failure without advancing the aggregate.
  • Actual duplicate, stale and incompatible-id cases are separate from a valid next sequence.

Common pitfall

Treating dead-letter storage as success or assuming one local sequence establishes a broker-wide total order.

Follow-up questions

  1. Who assigns a sequence when several producers exist?
  2. What evidence and authority should a repair or skip operation require?

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