Your log buffer can drop lines and still return true

I believed something about fast loggers for about two years, and it turned out to be wrong.
The belief went like this: a fast logger must collect your log lines in a buffer and write them out in batches, because batching is what makes disk writes cheap. It sounds obvious. Every article about file I/O tells you to batch your writes.
So I read the source of sonic-boom, the write stream that pino uses underneath, and then I ran it. Reading source gives you a plausible story. Running it gives you a true one.
Two things came out of that afternoon. Sonic-boom does not buffer by default. And when its buffer is capped and fills up, it can throw your log lines away while write() returns true — the exact same value you get when nothing is wrong.
Both of those are one-line surprises with real consequences, so let me show the measurements rather than assert them.
The buffer that is not a buffer by default
Here is the line in sonic-boom 5.0.1 that decides whether a write() call goes to disk right now or waits:
if (!this._writing && this._len >= this.minLength) {
this._actualWrite()
}this._len is how many bytes are sitting in the buffer. this.minLength is the threshold you are supposed to cross before a write happens. And a few lines up in the constructor:
this.minLength = minLength || 0The default is zero. Any byte count is greater than or equal to zero, so that half of the condition is always true. Nothing waits for a threshold, because there is no threshold.
That leaves !this._writing as the only thing that can hold a line back. It means “no write is currently in flight.”
To check that I understood it, I created three streams with different options, wrote five lines to each, and counted the actual write events the stream emitted. One event is one real write syscall. Here is what it printed:
| Options | Lines written | Real writes |
|---|---|---|
| default (async) | 5 | 2 |
sync: true | 5 | 5 |
minLength: 4096 | 5 | 1 |
Look at the middle row. In sync mode, five log lines produced five separate writes. There is no batching at all, because a synchronous write finishes before the next write() call starts, so _writing is never true when the next line arrives.
The default async row is the interesting one. The first line went out immediately. While that write was in flight, the other four piled up and left together as one write. That is not a buffer doing its job on a schedule. That is four lines catching a ride because they happened to arrive while the first write was still in flight.
Batching here is a side effect of concurrency, not a strategy. Under a slow destination you get large batches. On an idle server you get one write per line.
The third row is what most people assume the default looks like. You only get it if you ask for it by passing minLength yourself.
What pino actually passes
I checked pino 10.3.1 rather than guessing. In pino.js it builds its destination like this:
buildSafeSonicBoom({ dest: normalizeDestFileDescriptor(dest), minLength: 0 })It passes minLength: 0 explicitly, and the stdout path does not pass one at all, which lands on the same default. So a plain pino() writing to stdout is in the top row of that table: mostly one write per log line, with opportunistic grouping when the destination is slow.
That is a deliberate choice, not an oversight. Buffered log lines are lines that do not exist yet if the process dies, which is the same failure I hit in why your serverless function loses its last logs. Pino defaults to durability and lets you trade it away.
The part that actually cost me an afternoon
Sonic-boom takes a maxLength option: a hard ceiling on how many bytes may sit in the buffer. Crossing it means the destination cannot keep up, and the stream has to choose between growing memory forever and throwing data away. It throws data away. That is the right call.
This is how it does it:
if (this.maxLength && len > this.maxLength) {
this.emit('drop', data)
return this._len < this._hwm
}Read the return statement closely. The line was rejected, so this._len was never increased — it still holds whatever was already buffered. The function returns a comparison against a completely separate number, _hwm.
I wanted to know what a caller actually sees, so I set maxLength: 200, wrote 40 lines to a real file, recorded the return value of every call, and counted the lines that survived. It printed:
lines attempted : 40
lines dropped : 34
write() return value on the call that was DROPPED: true
lines on disk : 6
silently lost : 34Thirty-four of forty log lines were destroyed. Every single write() call returned true.
If you are checking that return value the way you would check any other stream write — and that is the normal thing to do, since a false from writable.write() is Node’s standard “slow down” signal — you get told everything is fine while 85% of your logs go in the bin.
Why the boolean cannot warn you
There are two independent limits here, and this is the bit that took me longest to see.
maxLength decides whether a line is accepted. _hwm decides what the return value is. They are set separately:
this._hwm = Math.max(minLength || 0, 16387)
this.maxLength = maxLength || 0_hwm is fixed at 16387 bytes unless your minLength is larger. It is never derived from maxLength. So with a maxLength of 200 bytes, the buffer can never hold more than 200 bytes, and 200 is always below 16387, so the return value is permanently true. The one signal that could warn you is mathematically pinned to “all good” by the very setting that causes the drops.
flowchart TD
A["write(line)"] --> B{"buffer + line > maxLength?"}
B -- yes --> C["emit 'drop'<br/>line is discarded"]
B -- no --> D["add line to buffer"]
C --> E{"_len < _hwm?"}
D --> E
E -- "true (usually)" --> F["return true"]
E -- false --> G["return false"]Both paths, the healthy one and the destructive one, arrive at the same return statement. The boolean answers “is the buffer roomy,” never “did I keep your line.”
The fix is one listener, and it is the only way to find out:
const stream = new SonicBoom({ dest: '/var/log/app.log', maxLength: 1048576 })
let dropped = 0
stream.on('drop', () => { dropped++ })
setInterval(() => {
if (dropped) {
process.stderr.write(`sonic-boom dropped ${dropped} log lines\n`)
dropped = 0
}
}, 10_000).unref()Count them and report them somewhere that is not the stream you just proved unreliable. I send mine to stderr for exactly that reason. Silent loss inside a logging path is a nasty class of bug, and it is the same shape as one BigInt in your log data deleting every other field: the data is gone, and the code that lost it reported success.
A default I also got backwards
While I was in there I checked the retry logic, because I assumed retries were bounded. When a write to a pipe fails with EAGAIN or EBUSY, the destination is temporarily full rather than broken, so retrying after a short pause is correct. Sonic-boom waits 100 ms between attempts.
I expected a small retry cap. The constructor says otherwise:
this.maxWriteRetries = maxWriteRetries || 0And the check that uses it:
const retriesExhausted = this.maxWriteRetries > 0 && this._writeRetries > this.maxWriteRetriesWith the default of 0, the first half of that && is false, so retriesExhausted is always false. The default is unlimited retries, every 100 ms, forever. The source comment confirms it in plain words: zero “preserves the existing unbounded-retry behavior for backward compatibility.”
If you want a bound, you have to pass one. Pass maxWriteRetries: 5 and a permanently stuck pipe surfaces an error instead of retrying until the process is killed.
One more constructor detail worth knowing before you start tuning: minLength must be smaller than maxWrite, which defaults to 16 KB. Set minLength: 65536 and the constructor throws immediately rather than misbehaving later, which I would rather have than a silent clamp.
What I would set
If you are running pino at volume and you tune anything here, set these two together:
Set maxLength and a drop listener in the same commit. A ceiling without a counter converts a memory problem into an invisible data-loss problem. That is a worse bug, not a better one, because memory pressure eventually shows up on a dashboard and dropped log lines never do.
Set maxWriteRetries if your destination is a pipe. Unbounded retry is a reasonable default for a file that will come back. For a container log driver that has gone away for good, it means a stream that retries every 100 ms until something else kills it.
The takeaway
The thing I would actually carry to other code is smaller than any of the specifics. Sonic-boom is careful, well-tested, and genuinely 2-3x faster than fs.createWriteStream(), which its README states and its benchmarks back up. None of that stopped me from being wrong about it twice in one afternoon, because I was reasoning about what a fast logger should do rather than what this one does.
A return value only means what the code returning it says it means. write() returning true here means “the buffer has room,” and I read it as “your line was accepted.” Those are different sentences, and the gap between them was 34 log lines.
Read the return statement. Then write the five-line script that proves what it does, because that is the part that cannot be wrong.