[slf4j-user] Would MDC mess up when used with Java Thread Pool Executor

Gérald Quintana gerald.quintana at gmail.com
Thu Sep 27 13:43:05 CEST 2018


Hi Kumar,

MDC is stored in Thead Local variable.
When you are using a thead pool: the calling thread A pushes an event in a
wait queue, and then (sometimes later), the called thread B pops the event
from the queue and runs it.

As a result,
1) the MDC is not propagated from thread A to thread B.
2) the MDC of thread B may reused for all events consumed from the queue:
this can cause wrong MDC.

To solver these problems, you should:
1) MDC not propagated: Thread A copies the MDC info in the event, Thread B
sets MDC from info in the event.
2) MDC is reused: Thread B clears the whole MDC before running a new event.

This is not a problem with SLF4J but with thread locals in general.

Gérald


Le jeu. 27 sept. 2018 à 12:48, kumar saurabh <sabs.blitz at gmail.com> a
écrit :

> Hi,
>
> I am working on microservices stack and using correlation id (Unique
> request id) generated via nginx to log into each microservice logs. I am
> using MDC for extracting this from request header in OncePerRequestFilter
> in java.
>
> One of the microservice then use java thread executor framework and then
> call other microservices.
>
> Can using MDC with executor (java thread pool) mess up due to thread re
> use? Is it possible that I get either empty MDC context or incorrect
> context due to thread reuse?
>
> I am referring below link where one such scenario is explained:
> http://ashtonkemerling.com/blog/2017/09/01/mdc-and-threadpools/
>
> A response is appreciated! Thanks
>
> --
> Regards,
> Kumar Saurabh
> _______________________________________________
> slf4j-user mailing list
> slf4j-user at qos.ch
> http://mailman.qos.ch/mailman/listinfo/slf4j-user
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/slf4j-user/attachments/20180927/dde6e3c8/attachment.html>


More information about the slf4j-user mailing list