Search code examples
node.jsrabbitmqstoragemessage-queuenode-amqplib

RabbitMQ data lost on crash


I'm using RabbitMQ to store and retrieve data. I referred this article. I have set the durable flag to true and the noAck flag to false (i need to store the messages on the queue even after consuming).

I created these scenarios:

I updated stock data 3 times with consumers off state (inactive). Then I activated the consumer.It consumed all the three messages from the queue. [Works good.]

Now I again produced three messages (consumer inactive again) then I turned off the rabbitmq server. When I restarted the server and activated the consumer. It doesn't seem to be consuming the data (are the messages that were on the queue has been lost?)

Consumer :

connection.createChannel(function (error1, channel) {
if (error1) {
  throw error1;
}
var queue = "updateStock2";

channel.assertQueue(queue, {
  durable: true,
});

console.log(
  " [*] Waiting for stockData messages in %s. To exit press CTRL+C",
  queue
);

channel.consume(
  queue,
  function (data) {
    stock = JSON.parse(data.content.toString());
    console.log(" [x] Received Stock:", stock.name + " : " + stock.value);
  },
  {
    noAck: false,
  }
);

Producer :

  connection.createChannel(function (error1, channel) {
  if (error1) {
    throw error1;
  }

  var queue = "updateStock2";

  channel.assertQueue(queue, {
    durable: true,
  });
  channel.sendToQueue(queue, Buffer.from(data));

  console.log(" [x] Sent %s", data);
});
setTimeout(function () {
  connection.close();
  //process.exit(0);
}, 500);});

Aren't they persistent? If the server crashes all the messages in the queue are gone forever?

How to retrieve data that were in the queue when the server crashes?

Thanks in advance.


Solution

  • Why your messages have lost?

    Regret to say, you did not declare {persistent: true} when you send message.Check https://www.rabbitmq.com/tutorials/tutorial-two-javascript.html, so you should use channel.sendToQueue(queue, Buffer.from(msg), {persistent: true});

    Aren't they persistent?

    Durable queues will be recovered on node boot, including messages in them published as persistent. Messages published as transient will be discarded during recovery, even if they were stored in durable queues.

    Which middleware maybe better for you?

    If you want a middleware which can persist messages even if consumed by consumers, you maybe need kafka