[slf4j-user] Would MDC mess up when used with Java Thread Pool Executor
Joachim Durchholz
jo at durchholz.org
Thu Sep 27 18:44:51 CEST 2018
Am 27.09.2018 um 12:48 schrieb kumar saurabh:
> 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?
Yes, you can get whatever MDC was left in the pool.
This could be an empty MDC (in those cases where some other thread
wasn't for microservices but for something else), or the MDC of a
previous microservice.
In general, whenever you start processing for a request in a thread, you
need to set up the MDC so that it reflects the request it is working
for. That's not just for ThreadPool, but for any thread, e.g. Swing
workers and such.
(I am pretty unconvinced that thread pools are a great idea. Essentially
you need to wipe all thread-local storage before you can reuse a thread
from the pool, and I'm somewhat sceptical that this is still worth it.)
Regards,
Jo
More information about the slf4j-user
mailing list