151
CHAPTER 5 | Designing and Developing Multi-Container and Microservice-Based .NET Applications
Deduplicating messages when using RabbitMQ
When intermittent network failures happen, messages can be duplicated, and
the message receiver
must be ready to handle these duplicated messages. If possible, receivers should handle messages in
an idempotent way, which is better than explicitly handling them with deduplication.
According to the
RabbitMQ documentation
, "If a message is delivered to a
consumer and then
requeued (because it was not acknowledged before the consumer connection dropped, for example)
then RabbitMQ will set the redelivered flag on it when it is delivered again (whether to the same
consumer or a different one).
If the “redelivered” flag is set, the receiver mus
t
take that into account, because the message might
already have been processed. But that is not guaranteed; the message might
never have reached the
receiver after it left the message broker, perhaps because of network issues. On the other hand, if the
“redelivered” flag is not set, it is guaranteed that the message has not been sent more than once.
Therefore, the receiver needs to deduplicate messages or process messages in an idempotent way
only if the “redelivered” flag is set in the message.
Dostları ilə paylaş: