Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove particular messages in rabbitmq before publishing new messages?

I have a subscriber which pushes data into queues. Now the messages looks this

{
 "Content": {
   "_id" ""5ceya67bbsbag3",
   "dataset": { 
     "upper": {},
      "lower": {}

}
}

Now a new message can be pushed with same content id but data will be different. So in that i want to delete the old message with same id or replaece the message those id is same & retain only latest message.

I have not found a direct solution for this in rabbitmq. Please guide me how we can do this ?

I have already gone through some posts.

Post 1

Post 2

like image 573
TechChain Avatar asked Jun 26 '20 07:06

TechChain


1 Answers

What you are trying to achieve cannot be trivially solved with RabbitMQ (or rather the AMQP protocol).

RabbitMQ queues are simple FIFO stacks and don't offer any mean of access to the elements beyond publishing at their top and consuming from their bottom.

Therefore, the only way to "update" an already existing message without relying on an another service would be to fetch all the messages until you find the one you are interested in, discard it, and publish the new one with the other messages you fetched together with it.

Overall, the recommendation when using RabbitMQ in regards of message duplication is to make their consumption idempotent. In other words, the consumption of 2 messages deemed to be the same should lead to the same outcome.

One way to achieve idempotency is to rely on a secondary cache where you store the message identifiers and their validity. Once a consumer fetches a new message from RabbitMQ, it would check the cache to see if it's a valid message or not and act accordingly.

like image 139
noxdafox Avatar answered Sep 28 '22 09:09

noxdafox