Home
Ensuring that messages are retrieved in the correct order
If we need to ensure that your messages are delivered in the correct order in all circumstances, we can use one of the following strategies:
- A SequenceNumber parameter is supported on the Publish message. A publisher can include this with each message, increasing the value by one for each successive message that it publishes for the same stream and topic. The broker does not check or set this parameter; the responsibility for it lies with the publisher. The number can be checked by the subscriber, which needs to remember the last sequence number it received for each stream and topic combination.
If a subscriber receives a publication message that is out of order, it can react in various ways:
- If it needs only the latest information (for example, a stock price) and the sequence number is greater than it should be (that is, one or more previous publications have not yet been received), this publication message is accepted. If the sequence number is less than it should be (that is, this is a previous publication), the publication message is ignored.
- If it needs to keep track of all information, it must record this information and its sequence number.
- A PublishTimestamp parameter, in Universal time, is provided on the Publish message. A publisher can include this with each message (with or without the SequenceNumber parameter). This is particularly useful if subscribers are interested only in the latest information; they can check whether the timestamp is greater than that of the last Publish message that they processed.
In both of the above solutions, the publisher and subscriber need to remember information about the last message they processed for a particular stream and topic. In the first solution this is the SequenceNumber for the Publish message, and in the second solution it is the PublishTimestamp. This information might need to be remembered atomically with issuing or receiving a publication. This can be accomplished by saving the information on a queue, using the same unit-of-work as the one in which the publication is put or retrieved.
Home