Video in progress

In an event-driven system, one order passes through 4 Kafka topics and dozens of consumers. How do you design observability that can answer "where did this order get stuck?"

A classic request-response trace is not enough here. Every message carries the trace context and a business correlation ID (orderId), span links are used for batches and fan-out, and every stage records its state change as an event. You also need consumer lag and "message age along the pipeline" metrics.

📖 Answer

In an async system a single "request" can last for hours, and the trace forms a graph, not a tree. So I put in three layers.

  1. Trace context and links: every producer writes traceparent into the header. If one message goes to one consumer, parent-child is enough. In a batch, or when one event combines several messages (for example "order ready" after the payment and warehouse events arrive), you open a new span and put span links to the incoming messages. You also shouldn't cram very long flows into one trace: many backends don't display a 6-hour trace well.

  2. A business correlation ID: orderId in every message header and as a separate field in the logs. Even if the trace wasn't sampled, you can reconstruct the whole path from the logs. The support team especially needs this, because they know the order number, not the trace ID.

  3. A state journal: every stage writes an order.state_changed event (previous state, new state, time, service), to a separate topic or table. This answers "where is order 991 right now and for how long" with a single query. From this data you can build an alert such as "orders that have been in PAID for more than 15 minutes", which is the signal closest to the business.

Metrics:

  • lag for each consumer group, measured in time (the age of the oldest unprocessed message), not in message count. Nobody knows whether "50 thousand messages" is a lot or a little, but "8 minutes behind" is clear;
  • end-to-end age: a histogram of now - order.createdAt at the end of the flow, which is the latency SLI for this flow;
  • the number of messages that land in the DLT, and why.

The trade-off: the state journal is extra writes and storage cost. But during an incident, getting the list of "which orders were affected" in a minute makes it pay for itself.