Video in progress
When the disk slowed down, the whole service's p99 latency shot up, even though requests barely touch files. How does this happen, and how do you prevent it?
Usually synchronous logging is to blame: every request writes a log, the appender writes to disk under a lock, and when the disk freezes, all threads line up on that lock. An async appender, a bounded queue and keeping disk IO off the request path are the main protection.
📖 Answer
Java has no timeout for disk IO: if a write hangs, the thread waits. If that write is under some shared lock, the freeze spreads to the whole service.
The typical chain: Logback's FileAppender guards writing with a lock. The disk (say, a network disk in the cloud) stalls for a few seconds, or a background backup saturates it. The first thread hangs in the write, and the other 200 Tomcat threads wait for the lock in their log calls. From the outside it looks like "the service isn't responding", while the CPU is idle. In a thread dump, all the threads sit BLOCKED inside OutputStreamAppender: that is where the diagnosis comes from.
The layers of protection:
- Async appender. Log events go into a bounded queue and a single thread writes them to disk. The key setting is what happens when the queue is full: by default Logback's
AsyncAppenderstarts dropping TRACE/DEBUG/INFO when the queue is 80% full, and withneverBlock=trueit never blocks at all. Losing logs or latency is an explicit trade-off you have to accept. - Write to stdout. In a container, writing logs to stdout and leaving collection to an agent (Fluent Bit, Vector) separates the service from the disk, but blocking can still happen if the pipe fills up, so you still need an async layer.
- Bulkhead. Functions that work with files (export, upload) run in a separate bounded executor. If the disk freezes, only that function stops, not the main API.
- Reducing volume. 20 lines of INFO logs per request is a problem in itself. Reconsider sampling and levels.
The point is: there is no code path that "doesn't touch the file". For every shared resource (the log, the temp directory, a metrics file), ask at design time what happens when it freezes.
Sign up to mark studied questions and save your progress.