Practical examples of how correlation id is used in messaging?

JD. picture JD. · Nov 25, 2013 · Viewed 13.1k times · Source

Can anyone give me examples of how in production a correlation id can be used?

I have read it is used in request/response type messages but I don't understand where I would use it?

One example (which maybe wrong) I can think off is in a publish subscribe scenario where I could have 5 subscribers and if I get 5 replies with the same correlation id then I could say all my subscribers have received it. Not sure if this would the be correct usage of it.

Or if I send a simple message, the I can use the correlation to guarantee that the client received it.

Any other examples?

Answer

Juha Palomäki picture Juha Palomäki · Jul 21, 2015

A web application that is providing HTTP API for outsiders for performing a processing task and you want to give the results for the caller as a response to the HTTP request they made.

A request comes in, message describing the task is pushed to queue by the frontend server. After that the frontend server blocks to wait for response message with the same correlation id. A pool of worker machines are listening on queue and one of them picks up the task, performs it and returns the result as message. Once a message with right correlation id comes in, frontend server continues to return the response to the caller.