Use of Correlation IDs to track various downstream service interactions

With a large number of services interacting to provide any given end-user capability,
a single initiating call can end up generating multiple more downstream service calls.
For example, consider the example of a customer being registered. The customer fills
in all her details in a form and clicks submit. Behind the scenes, we check validity of
the credit card details with our payment service, talk to our postal service to send out
a welcome pack in the post, and send a welcome email using our email service. Now
what happens if the call to the payment service ends up generating an odd error?
We’ll talk at length about handling the failure in Chapter 11, but consider the diffi‐
culty of diagnosing what happened.
If we look at the logs, the only service registering an error is our payment service. If
we are lucky, we can work out what request caused the problem, and we may even be
able to look at the parameters of the call. Now consider that this is a simple example,

and that one initiating request could generate a chain of downstream calls and maybe
events being fired off that are handled in an asynchronous manner. How can we
reconstruct the flow of calls in order to reproduce and fix the problem? Often what
we need is to see that error in the wider context of the initiating call; in other words,
we’d like to trace the call chain upstream, just like we do with a stack trace.
One approach that can be useful here is to use correlation IDs. When the first call is
made, you generate a GUID for the call. This is then passed along to all subsequent
calls, as seen in Figure 8-5, and can be put into your logs in a structured way, much as
you’ll already do with components like the log level or date. With the right log aggre‐
gation tooling, you’ll then be able to trace that event all the way through your system:

15-02-2014 16:01:01 Web-Frontend INFO [abc-123] Register
15-02-2014 16:01:02 RegisterService INFO [abc-123] RegisterCustomer …
15-02-2014 16:01:03 PostalSystem INFO [abc-123] SendWelcomePack …
15-02-2014 16:01:03 EmailSystem INFO [abc-123] SendWelcomeEmail …
15-02-2014 16:01:03 PaymentGateway ERROR [abc-123] ValidatePayment …

Book: Building Microservices by Sam Newman