We use analytics cookies.We use cookies to analyze site traffic and improve your experience.
Loading...
System Design Masterclass · Messaging Event Systems
Free preview: the first 2 parts of this 7-part lesson.
Already bought the course? Sign in to read the whole lesson.
this lesson is part of
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$25 once, by PayPal
See the System Design courseread the rest of this lesson in
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$25 once, by PayPal
See the System Design coursein Messaging Event Systems
This lesson is part of the full System Design Masterclass. Unlock it along with 769 other lessons for a one-time payment.
One-time payment by card or PayPal
Get Lifetime AccessOne-time payment: UPI, card or netbanking
Get Lifetime Access18 Foundation lessons are free, no payment needed. Try them now
It is easy and good
“It is really nice and good Please join the course”
You're receiving a fax, page by page. But the fax machine hiccups, and the pages arrive as: 3, 1, 4, 2, 5. You can't read the document until you sort them back into order: 1, 2, 3, 4, 5.
In messaging systems, messages can arrive out of order for many reasons, parallel processing, network routing differences, retries, competing consumers. If your downstream logic requires messages in sequence ( replay, financial transactions, step-by-step workflows), you need a resequencer.
A resequencer collects out-of-order messages, buffers them, and releases them in the correct sequence number order. It's like a sorting buffer, messages go in out of order and come out in order.
Each message carries a sequence number. The resequencer tracks the next expected sequence number and holds messages that arrive too early:
Expected: 1
Arrives: 3 → buffer (waiting for 1, 2)
Arrives: 1 → release 1, expected = 2
Arrives: 4 → buffer (waiting for 2)
Arrives: 2 → release 2, then release 3 (buffered), then release 4 (buffered)
expected = 5
Arrives: 5 → release 5, expected = 6
Implementation:
class Resequencer {
private nextExpected = 1;
private buffer = new Map<number, Message>();
receive(msg: Message) {
if (msg.sequenceNumber === this.nextExpected) {
this.release(msg);
this.nextExpected++;
// Release any buffered messages that are now in sequence
while (this.buffer.has(this.nextExpected)) {
this.release(this.buffer.get(this.nextExpected)!);
this.buffer.delete(this.nextExpected);
this.nextExpected++;
}
} else if (msg.sequenceNumber > this.nextExpected) {
this.buffer.set(msg.sequenceNumber, msg);
}
// Ignore messages with sequence < nextExpected (duplicates)
}
}
The gap problem: What if message 2 is lost entirely? The resequencer holds messages 3, 4, 5, ... Forever, waiting for 2. Solutions: