Building Resilient Message Queues: Notes on At-Least-Once Delivery
A technical deep-dive into write-ahead logs, consumer heartbeats, and disk fsync semantics.
The Guarantee Trap
Every engineer eventually encounters the three classic fallacies of message brokers:
1. "Messages will always arrive in order."
2. "Messages will be delivered exactly once without coordinated two-phase commit."
3. "The disk write is persisted as soon as the OS returns from write()."
The Reality of Fsync and Disk Controllers
When an application calls write() on a POSIX socket or file descriptor, the operating system kernel copies the bytes into page cache memory. The return code is immediate. But if power fails a millisecond later, the data evaporates into the ether.
To truly guarantee durability, an append-only log must issue an explicit fsync(fd) or fdatasync(fd). And even then, drive manufacturers frequently lie about physical platter flush completion unless ordered by a battery-backed write cache.
``c
// The fundamental persistence barrier
int append_entry(int log_fd, const void *record, size_t len) {
ssize_t written = write(log_fd, record, len);
if (written != (ssize_t)len) {
return -1; // Partial write error
}
// Force disk controller to flush internal volatile DRAM cache
if (fdatasync(log_fd) != 0) {
return -2; // Persistence failure
}
return 0;
}
``
Designing for failure from day one is what separates hobbyist scripts from mission-critical infrastructure.
Related Essays & Studies
CONTINUE READING FROM THE ARCHIVE
The Art of the First-Principles Debugger: What Really Happens When Code Silently Lies
When a distributed consensus algorithm stalls at 3:00 AM, the bug rarely lives where you think. A journey down the rabbit hole of memory alignments, compiler reorderings, and why disciplined observation beats blind speculation.
The Cursor Knows When You're Thinking
Without it, an empty text box feels unfinished; with it, the same emptiness feels like an invitation. One tiny animation quietly tells your brain: something is waiting to become a thought.
I Finally Found Out What Claude Is Doing While It's "Thinking"
I just found out what Claude is actually doing while I wait for it to answer my prompts, and it’s kinda wild. Here is what’s actually happening under the hood when we use reasoning models for our engineering work.