Scrivio Interview library
InternalsIntermediate7 min read · updated 12 November 2026

Compare Log4j2 async loggers based on the LMAX Disruptor with an async appender based on a blocking queue. What happens when the ring buffer is full, and how would you tune or configure it for a service that logs heavily under bursts?

Log4j2 async loggers hand events to a pre-allocated, lock-free LMAX Disruptor ring buffer as soon as the log call is made, which gives lower latency and higher throughput than an async appender, which puts events into a blocking queue guarded by locks. When the ring buffer is full, the AsyncQueueFullPolicy decides: the default makes the caller wait, while the discarding policy drops events at or below a threshold level (INFO by default). For bursty services we tune the ring buffer size, the wait strategy and the discard policy, keep location info off, and above all reduce volume, because async logging hides the cost of I/O but does not remove it.

Why this question is asked

Log4j2's async loggers are often presented as a free performance upgrade: switch them on and logging becomes fast. They really are fast, but they change what happens under pressure, and a service that logs heavily during bursts is exactly where that matters. Interviewers ask this internals question to check whether we understand the difference between a ring buffer and a blocking queue, what the system does when it runs out of space, and which settings trade CPU for latency. In this answer, we will compare the two designs, enable async loggers in both modes, follow an event through the ring buffer, see what happens when it fills, and tune a configuration for a bursty service.

1Two Ways to Make Logging Asynchronous

An async appender, Log4j2's <Async> element or Logback's AsyncAppender, works at the end of the pipeline. The logger runs its filters and decides which appenders get the event on the calling thread, then the async appender puts the event into a java.util.concurrent BlockingQueue, usually an ArrayBlockingQueue. A background thread takes events out and passes them to the real appender. ArrayBlockingQueue uses a lock shared by producers and the consumer, so with many threads logging at once, they contend for that lock.

Async loggers work at the start. As soon as the application calls log.info, the logger copies the event's data into a slot of a ring buffer from the LMAX Disruptor library and returns. A background thread then does everything else: filtering by logger configuration, formatting and writing. The ring buffer is an array allocated once at start-up, with slots that are reused, and producers claim slots using atomic sequence counters instead of locks. That is why async loggers show lower latency and higher throughput in Log4j2's own benchmarks, especially with many logging threads.

Async appender versus async loggers
AspectAsync appender (queue)Async loggers (Disruptor)
Hand-off pointafter logger filtering, before the appenderright at the log call
Data structureBlockingQueue with lockspre-allocated lock-free ring buffer
Default capacityLog4j2 Async bufferSize 1024262,144 slots (check your version)
Contentionthreads share the queue lockatomic sequence claims
Extra dependencynonecom.lmax:disruptor jar
Location infoincludeLocation on the loggeroff by default; capturing it costs a lot

2Enabling Async Loggers

There are two modes. In all-async mode, every logger is asynchronous; we switch it on by selecting the async context selector with a system property, usually in log4j2.component.properties. The property name has changed between 2.x versions, so check yours: older releases used Log4jContextSelector, newer ones log4j2.contextSelector. In mixed mode, we keep the normal context and mark chosen loggers async in the configuration with <AsyncLogger> and <AsyncRoot>. Either way, the Disruptor jar must be on the classpath.

Java
# log4j2.component.properties: all loggers async
log4j2.contextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
log4j2.asyncLoggerRingBufferSize=262144
log4j2.asyncLoggerWaitStrategy=Timeout
<!-- or mixed mode in log4j2-spring.xml -->
<Loggers>
<AsyncLogger name="com.shop" level="INFO" includeLocation="false"/>
<Root level="WARN"><AppenderRef ref="JSON"/></Root> <!-- stays synchronous -->
</Loggers>

All-async mode is the fastest because the hand-off happens before any configuration lookup. Mixed mode is useful when some loggers, for example an audit logger, must stay synchronous so their events are written before the call returns.

3What Happens When the Ring Buffer Is Full

A ring buffer, however large, fills when the background thread writes more slowly than the application logs for long enough. Then a component called the AsyncQueueFullPolicy decides what the calling thread does. The default policy makes the caller wait for a free slot, so no event is lost but request threads stall; recent versions also write synchronously when the background thread itself is the one logging, to avoid a deadlock. The alternative, DiscardingAsyncQueueFullPolicy, selected with log4j2.asyncQueueFullPolicy=Discard, drops events at or below a threshold level, INFO by default (log4j2.discardThreshold), and still waits for more important ones.

Java
# drop INFO and below when full; WARN and ERROR still wait for a slot
log4j2.asyncQueueFullPolicy=Discard
log4j2.discardThreshold=INFO
// burst: 40,000 events/s for 10 s, disk writes 25,000 events/s
// backlog grows 15,000/s -> 262,144 slots full after about 17 s: this burst fits
// a 30 s burst: full after ~17 s; default policy -> callers wait; Discard -> INFO dropped
Step throughLog call when the buffer is full
  1. Thread calls log.info()
  2. Free slot in ring buffer?
    YesCopy event into slot, returnNoAsk AsyncQueueFullPolicy
  3. Discard policy and level at or below threshold?
    YesDrop the eventNoWait for a slot
  4. Background thread formats and writes

4 steps in order. Play them, or step through them one at a time.

Notice what the numbers say. The buffer converts a short burst into a delay for the background thread, which is what we want. It cannot help with a long burst, because the backlog keeps growing as long as the input rate exceeds the output rate.

4Tuning for a Service That Logs in Bursts

The ring buffer size sets how long a burst we can absorb. The default of 262,144 slots is already large; increasing it costs memory, since each slot holds a reusable event object with its own fields and buffers, so measure before raising it. The wait strategy decides how the background thread waits for new events: Timeout, the default in recent versions, and Block put it to sleep on a lock and use little CPU; Sleep and Yield poll more aggressively, and busy spin keeps a core fully busy for the lowest latency. Check your version for exact names. For most services Timeout is right; spinning only makes sense on dedicated hardware.

Location info must stay off: includeLocation="true" forces a stack trace capture on the calling thread for every event, which wipes out the gain. For bursts, I would also choose the Discard policy for INFO and below, so request threads never stall on routine messages, and export the number of discarded events, which recent Log4j2 versions can report, so we see when it happens.

5Async Hides the Cost, It Does Not Remove It

Every event is still formatted, encoded and written; async logging only moves that work to another thread. If the disk or the log shipper cannot keep up with the average rate, the ring buffer fills sooner or later, and then we block or drop exactly as before. The background thread also uses CPU that the application could use. So the most effective tuning is to log less: remove per-request INFO lines that metrics can replace, sample routine events and log state changes rather than every step. A service that halves its log volume gains more than any wait strategy can give it.

6Follow-up questions

Why can ThreadContext (MDC) values be wrong with async logging if you mutate maps?

Async loggers copy the ThreadContext into the ring buffer event at the moment of the log call, so the background thread sees a snapshot. With the default copy-on-write context map, each put creates a new immutable map, so the snapshot is safe. Problems appear when values are mutable objects that our code changes after logging, because the snapshot holds a reference to the same object and the background thread may format it later, or when a garbage-free context map is configured and code relies on mutating it in place. Another trap is an inheritable ThreadContext in thread pools: pooled threads keep the values copied when they were created, which go stale. Keep context values immutable, such as strings, and set them per task.

Are message parameters also formatted on the background thread?

By default, Log4j2 formats the message on the calling thread before putting it into the ring buffer, so a mutable parameter changed after the call does not change the log output. Formatting can be moved to the background thread with log4j2.formatMsgAsync or for message types marked as safe for it, and then mutable parameters become a risk.

Can async loggers lose events at shutdown?

Yes, events still in the ring buffer are lost if the JVM stops without stopping the logger context. Log4j2 registers a shutdown hook by default, and Spring Boot stops the logging system on a normal shutdown, which drains the buffer within a timeout. A crash or kill -9 loses whatever was still queued.

7Conclusion

In this answer, we compared async appenders, which hand events to a lock-based blocking queue after logger filtering, with Log4j2 async loggers, which copy events into a pre-allocated lock-free Disruptor ring buffer at the log call and need the Disruptor jar. We enabled them in all-async mode through the context selector property and in mixed mode with AsyncLogger elements, saw that a full buffer makes callers wait by default or drops events at or below a threshold with the discarding policy, tuned buffer size, wait strategy, discard policy and location info for bursts, and noted that async logging moves the cost rather than removing it.

What to say in the interviewAbout 100 seconds aloud

Open with

An async appender works at the end of the pipeline: the logger filters on the calling thread, then the appender puts the event in a blocking queue, which uses locks, and a background thread writes it.

Then, if they want more

Log4j2 async loggers hand off right at the log call: the event is copied into a slot of an LMAX Disruptor ring buffer, which is pre-allocated, reuses its slots and uses atomic sequences instead of locks, so latency is lower and throughput higher, especially with many threads. It needs the Disruptor jar.

You enable all-async with the context selector system property set to AsyncLoggerContextSelector, and the property name differs between versions, or mixed mode with AsyncLogger and AsyncRoot elements.

When the ring buffer is full, the AsyncQueueFullPolicy decides: by default the caller waits for a free slot, and with the Discard policy, events at or below the discard threshold, INFO by default, are dropped.

For a bursty service, I'd size the ring buffer from the burst math, since it only absorbs bursts where the backlog fits, keep the Timeout wait strategy unless we have dedicated cores, use the Discard policy for INFO and below so request threads never stall, keep location info off, and monitor discards.

But I'd also say clearly that async logging hides the cost rather than removing it: if the disk or shipper can't keep up on average, you still block or drop, so reducing volume is the real fix.

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 official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
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 "Compare Log4j2 async loggers based on the LMAX Disruptor with an async appender based on...": https://scrivio-interview-library.netlify.app/java/java-logging/25527580fb/