What is the difference between correlation id and delivery tag

Viewed 660

I've searched for a good explanation for the difference between these two, but didn't really find one.

What I know till now is that: correlation id is a string (Guid which was converted to string), and delivery tag is an int. correlation id is unique for each message, and delivery tag is unique only in the channel (The channel is the scope).

That's fine....but what is the difference in the purposes? why do we need two identifiers for a message?

2 Answers

The two identifiers exist at two different conceptual layers of communication, and have different properties that are useful in each case. While a protocol could probably be designed that had one identifier serving both purposes, keeping them separate makes both implementations simpler.

Delivery tags

  • Part of the AMQP communication layer, built into RabbitMQ itself.
  • Example use: a consumer process can acknowledge that a message has been processed and can be permanently discarded on the broker (RabbitMQ server).
  • Automatically assigned within the open channel for every message delivered.
  • Must be unique within that channel in order for the protocol to function correctly. Does not need to be unique across different channels, so a simple incrementing integer is simple to implement.
  • The same message may be delivered at different times with different delivery tags, or even exist on multiple queues and be delivered simultaneously to different consumers.

Correlation IDs

  • Part of the logic of the application that is using RabbitMQ, not the broker itself.
  • Example use: using a matching correlation ID and "reply to" on two separate messages, which the application wants to treat as a request and a response in an RPC pattern.
  • Needs to be manually added when the message is first created, and is optional.
  • Not guaranteed to be unique by the protocol, which just sees it as an arbitrary string. It is up to an application to generate in a way that is sufficiently unlikely to collide for its use case, such as an appropriate form of UUID.
  • Will stay the same every time a message is delivered, no matter how many times it is forwarded or duplicated into multiple queues.

Correlation ID is generally used in the context of RabbitMQ when I want to see a synchronous behavior in which a message is sent and in response to it another sender will send a response but will have the correlationID in the reply-to tag . The common pattern which is replicated in RabbitMQ is the RPC call which is more like a Synchronous messaging.

Delivery Tag is however an indicator of the delivery of the message per channel and generally comes in scope when Acknowledged Delivery model is being followed.

Both have completely different purpose and are not message identifier as such.

Related