Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

A service writes logs synchronously to a file on a network volume. When the volume slows down, request latency jumps and threads block in the appender. What do you change?

Logback appenders write synchronously and hold a lock while they write, so when the network volume slows down, every request thread that logs waits behind the slow write. We wrap the file appender in an AsyncAppender (or switch to Log4j2 async loggers) with a bounded queue and decide its discardingThreshold and neverBlock behaviour, and, more importantly, we stop writing to network storage at all: write to local disk or stdout and let an agent ship the logs. We monitor dropped events if the queue discards, and remove caller data such as %L and %M from the pattern.

Why this question is asked

This bug looks like a database or network problem at first: latency jumps, threads pile up, and the service may even fail health checks. Only a thread dump shows that the threads are stuck inside a log call. Interviewers ask it because it tests whether we know that logging is synchronous by default and that a log statement can block a request for as long as the storage behind it takes. In this answer, we will reproduce what the thread dump shows, explain why Logback blocks, add an asynchronous appender with clear full-queue behaviour, move the files off the network volume, monitor drops, and remove the expensive pattern elements.

1What the Thread Dump Shows

When latency jumps, we take a thread dump with jcmd <pid> Thread.print or from the Actuator threaddump endpoint. In this case, most request threads look like the excerpt below: one thread is inside a file write, and dozens are waiting for the lock it holds.

Text
"http-nio-8080-exec-17" RUNNABLE
at java.io.FileOutputStream.writeBytes(Native Method)
at ch.qos.logback.core.recovery.ResilientOutputStreamBase.write(...)
at ch.qos.logback.core.OutputStreamAppender.writeBytes(...) // holds the appender lock
"http-nio-8080-exec-18" WAITING (parking)
at java.util.concurrent.locks.ReentrantLock.lock(...)
at ch.qos.logback.core.OutputStreamAppender.writeBytes(...) // waits for exec-17
at com.acme.orders.OrderService.place(OrderService.java:88) // a plain log.info(...)
... 63 more threads in the same state

Frame names differ between Logback versions, but the shape is always the same: a single writer in a native write call and a queue of threads parked on a lock inside the appender. The request code itself did nothing wrong; it just called log.info().

2Why a Slow Disk Blocks Application Threads

By default, a Logback appender does its work on the thread that called the logger. The file appender formats the event, then writes the bytes to the file, and it holds a lock while writing so that lines from different threads do not interleave. On a local SSD a write takes microseconds, so nobody notices. On a network volume such as NFS or a cloud file share, a write can take 50 ms on a normal day and several seconds during a storage hiccup.

So the latency of the storage becomes the latency of the request, multiplied by contention: if 64 threads each need to log twice per request and every write takes 200 ms, requests wait seconds just for the log lock. Tomcat's thread pool fills, new requests queue, and the service looks down even though its real work is fine. Notice that immediateFlush, which is true by default and flushes after every event, makes each write go to the storage straight away, which is exactly what makes the network latency visible.

Step throughA slow network write blocks every logging thread
Request ARequest BFile appenderNetwork volume
log.info: lock taken
write + flush
log.info: waits for lock
done after 800 ms
lock released
write for B
done after 800 ms
B returns after 1.6 s

8 messages in order. Play them, or step through them one at a time.

3Fix 1: An Asynchronous Appender With a Bounded Queue

The first fix takes the write off the request thread. Logback's AsyncAppender puts each event into a bounded in-memory queue and returns immediately; one background thread drains the queue into the real file appender. Only that background thread now waits on the volume.

XML
<appender name="ASYNC_FILE" class="ch.qos.logback.classic.AsyncAppender">
<queueSize>8192</queueSize> <!-- default 256 -->
<discardingThreshold>0</discardingThreshold> <!-- default: drop TRACE/DEBUG/INFO when 80% full -->
<neverBlock>true</neverBlock> <!-- default false: block when completely full -->
<appender-ref ref="FILE"/>
</appender>

The two settings that matter are the behaviour as the queue fills. discardingThreshold controls early dropping: by default, when less than 20 percent of the queue is free, Logback quietly drops events below WARN and keeps warnings and errors. Setting it to 0 disables that. neverBlock controls what happens when the queue is completely full: false means the caller blocks until there is room, which brings back the original problem during a long slowdown, and true means the event is dropped.

So we have to choose. For a request-serving service, losing INFO lines while the storage is slow is usually better than failing requests, so neverBlock=true with a generous queue is a sensible default. Log4j2 users get the same choice with async loggers, whose behaviour on a full ring buffer is set by an AsyncQueueFullPolicy (block by default, or discard below a threshold level). Either way, the queue only buys time: if the volume is slow for minutes, the queue fills and the choice applies.

4Fix 2: Stop Writing Logs to Network Storage

The asynchronous appender treats the symptom. The cause is that the application writes to storage it does not control and whose latency is unpredictable. The durable fix is to write to fast local storage, a local disk on a VM or stdout in a container, and let a log agent such as Fluent Bit, Vector or the OpenTelemetry Collector read the local file or stream and ship it to the central system.

The agent runs outside the request path, has its own buffering and retries, and can fall behind for minutes without affecting requests. If logs must end up on a shared volume for an old reason, such as a tool that reads them there, the agent can copy them there too. A local file still needs its size limits, max-file-size, max-history and total-size-cap, so a slow agent can never fill the disk.

5Monitoring Drops and Removing Caller Data

If we allow the async appender to discard, we must know when it does, otherwise a gap in the logs looks like nothing happened. We expose a dropped-events counter as a metric, for example by wrapping the appender or using a custom appender that counts rejected events, and alert when it is above zero. Lines lost while the storage is degraded are an accepted trade-off; lines lost every day mean the queue or the volume is wrong.

Finally, we check the pattern. Caller data, %L for the line number, %M for the method, %F for the file and %C for the class, is computed by building a stack trace for each event, which is expensive. With an async appender it is worse, because the stack trace must be captured on the caller's thread before the event is queued, so it removes much of the benefit; this is why AsyncAppender's includeCallerData is false by default and patterns with %L print ? instead. We use %logger, which is free, instead.

6Follow-up questions

Why not just set immediateFlush to false?

It reduces the number of flushes, because Logback then writes through a buffer and the operating system flushes larger chunks, which helps throughput. But the write still happens on the request thread under the lock, and when the buffer fills or the storage stalls, threads still block. It also risks losing the last lines on a crash. It is a tuning option, not the fix.

Will the async appender lose logs on shutdown?

It tries to drain the queue on stop, waiting up to maxFlushTime, one second by default, and drops what is left after that. That is another reason to keep the drain fast by writing locally; a fast local write empties even a full queue of 8,192 events in well under a second.

Is this problem specific to Logback?

No. Any synchronous appender in any framework behaves the same, including Log4j2's synchronous appenders and java.util.logging handlers. Log4j2 async loggers avoid the lock contention with a lock-free ring buffer, but they face the same block-or-drop decision when the ring buffer fills.

7Conclusion

In this answer, we saw from a thread dump that request threads were parked on the file appender's lock while one thread waited on the network volume, because Logback appenders write synchronously on the calling thread. We wrapped the appender in an AsyncAppender with a bounded queue and an explicit choice of discardingThreshold and neverBlock, moved logs to local disk or stdout with an agent shipping them, monitored dropped events, and removed caller data such as %L and %M from the pattern.

What to say in the interviewAbout 90 seconds aloud

Open with

A thread dump would show one thread inside a native file write and dozens of request threads parked on a lock inside Logback's OutputStreamAppender. Logback appenders are synchronous by default: the calling thread formats and writes the event, and it holds a lock so lines do not interleave.

Then, if they want more

On a network volume a write can take hundreds of milliseconds, so the storage latency becomes request latency, multiplied by contention. My first change is to wrap the file appender in an AsyncAppender with a bounded queue, a few thousand events instead of the default 256, so request threads only enqueue.

Then I decide the full-queue behaviour explicitly: by default Logback drops INFO and below when the queue is eighty percent full and blocks when it is completely full, so for a normal service I would set neverBlock to true and accept dropping. Log4j2 async loggers give the same choice.

But the real fix is to stop writing to network storage: write to local disk with size caps, or stdout in containers, and let an agent like Fluent Bit ship the logs, so the application never waits on remote storage. If we allow drops, I expose a dropped-events metric and alert on it.

And I remove caller data like %L and %M from the pattern, because it builds a stack trace per event on the calling thread.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring Boot reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A service writes logs synchronously to a file on a network volume. When the volume slows...": https://scrivio-interview-library.netlify.app/spring-boot/boot-logging/0395342d2e/