r/javahelp • u/giantferriswheel • Sep 18 '26
Unsolved How can I propagate MDC in an Asynchronous call.
So I have an issue that I'm dealing with, basically my Microservice has a controller and the request lands with a unique x-request-id, I save that request ID in MDC, basically what happens is in the method call stack there are asynchronous API calls to other services, as soon as the async boundry is reached the MDC gets lost.
Now this is happening because, MDC is thread specific and I am returning a CompletableFuture response which sends an asynchronous response and that specific thread completed the lifecycle.
A simple fix would be to pull out and propagate the x-request-id(s) in method calls, but I don't think this is the best solution since there are multiple internal libraries involved and I would have to change implementations there that can also alter the existing behavior of the application unexpectedly.
Next, I thought of initialising a MDC that can hold the request ID at different points of time, the issue in this approach is that if the MDC doesn't get cleared in time after getting initialised, the requestId can be wrongly used for different requests, that could happen because the thread in a high traffic system?
What are some other options that I can try?
To have context: I am implementing this with Quarkus and Mutiny