Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly deal with Correlation IDs?

I have just been reading up on logging in a microservice architecture and there seems to be this concept of Correlation ID, which is basically passed along to each microservice. This Correlation ID can be useful for logging - you can trace requests across microservices just by searching the Correlation ID.

My question is, how is this usually implemented? Where is this Correlation ID generated? Do you generate some sort of UUID on the Frontend, and pass it along via the X-REQUEST-ID HTTP header?

My second question is: when you receive this Correlation ID in the server, how do you make it accessible to all the functions in the server?

Suppose your server had something like this:

requestHandler(httpRequest) {
    correlationId = httpRequest.header.get(X-REQUEST-ID)
    ...
    function2()
}

function2() {
    ...
    function3()
}

function3() {
    ...
    function4()
}

function4() {
    ...
}

Suppose I wanted to log something in function4() (assume that I want the log to include the Correlation ID as well), do I really have to pass the Correlation ID all the way down from requestHandler() to function4()? Or is there a better approach?

My first thought would be to have some sort of in-memory key-value DB where you can store the Correlation ID as a value, but what would be the key?

like image 745
Ryn Avatar asked Oct 25 '25 13:10

Ryn


1 Answers

Yes its a kind of UUID only, whenever frontend sends the request to API Gateway or orchestration layer, then this UUID is generated and added to the subsequent calls to be made to each of the microservices to be traced.

Hence if you are giving synchronous calls or asynchronous calls via some messaging broker, this is something that you are supposed to embed in the header always and the called function will put this into logging at the beginning only, using this correlationID later you can intercept the call flow in your logging client.

like image 192
Tushar Mahajan Avatar answered Oct 28 '25 04:10

Tushar Mahajan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!