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.bl...@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@qos.ch
> http://mailman.qos.ch/mailman/listinfo/slf4j-user
_______________________________________________
slf4j-user mailing list
slf4j-user@qos.ch
http://mailman.qos.ch/mailman/listinfo/slf4j-user

Reply via email to