Engineering With Java

Engineering With Java

Spring Boot Interview Question - @Async Broke Our Audit Logs

Your request remembers everything. @Async remembers nothing

Suraj Mishra's avatar
Suraj Mishra
Aug 06, 2026
∙ Paid

Context

A payment service stores request metadata in MDC so every log line contains the request information.

The controller logs look correct:

requestId=8fa92
merchantId=paypal-123
userId=suraj

But every log produced inside sendReceipt() shows:

requestId=null
merchantId=null
userId=null

🚀 Level up your Java & Spring Boot skills.

Join 8100+ developers and get weekly, no-fluff content featuring practical coding tips, real-world backend insights, and interview questions based on production scenarios.

Founding Member Offer: Lock in founding member price at $50/year (~$4/month) for life. Limited spot left.

So far we have covered 70+ real world based interview questions and will add up to 100 by end of this year.

Testimonials


Why did the audit context disappear?

MDC uses ThreadLocal

Most logging frameworks store MDC using ThreadLocal. The values belong only to the current thread.

@Async executes on another thread

The executor picks another thread from its pool. That thread has a completely different ThreadLocal map.

Request Thread  -> Async Executor Thread

Hence MDC.get("requestId") returns null

ThreadLocal values are NOT inherited

Unlike method arguments, ThreadLocal state is never copied automatically. Even if the thread comes from the same executor every time, the context is not transferred.

How would you fix it without changing every @Async method in the application?

User's avatar

Continue reading this post for free, courtesy of Suraj Mishra.

Or purchase a paid subscription.
© 2026 Suraj Mishra · Privacy ∙ Terms ∙ Collection notice
Start your SubstackGet the app
Substack is the home for great culture