Trace messaging delivery and ordering
Trace messaging delivery and ordering
Message delivery guarantees describe delivery attempts, not automatically the business effect. At-most-once can lose a message; at-least-once can redeliver it; exactly-once claims need a precisely defined boundary. A consumer should acknowledge only after its effect is durably complete. Ordering often applies within a partition or key, not across the entire system.
package main
import ( "fmt")
type Message struct { ID string Amount int}
func consume(m Message, seen map[string]bool, total *int) bool { if seen[m.ID] { return false } *total += m.Amount seen[m.ID] = true return true}func main() { seen := map[string]bool{} total := 0 for _, m := range []Message{{"x", 3}, {"x", 3}, {"y", 4}} { fmt.Println(consume(m, seen, &total), total) }}The duplicate could represent a lost acknowledgement. The model assumes stable unique IDs and one sequential consumer. A real consumer needs a durable atomic effect and dedup record, followed by acknowledgement; acknowledging first can lose work. The sample does not implement a broker.
Expected output:
true 3false 3true 7Detect a gap rather than assuming order
package main
import ( "fmt")
func classify(last, seq int) string { if seq <= last { return "old" } if seq == last+1 { return "next" } return "gap"}func main() { for _, seq := range []int{2, 3, 5} { fmt.Println(classify(2, seq)) }}This model uses contiguous positive sequence numbers for one stream, with small integer values. A gap requires a policy: buffer within limits, recover missing events, or reconcile from a snapshot. Do not silently discard a new event merely because it arrived early. A dead-letter queue needs monitoring and recovery, not just storage.
Expected output:
oldnextgapTry it locally
Given last sequence 4, classify 4 as duplicate, 5 as next and 7 as gap. Print each label; assume contiguous sequence numbers.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Distributed services 06: Trace messaging delivery and ordering
Check the lesson objective before moving on.
When should a consumer acknowledge an effect?
What does at-least-once delivery permit?