Reputation: 1120
My application cannot afford to lose any messages. RabbitMQ does a pretty good work, but it is not always flawless.
Marking messages as persistent is not enough. There is small time window where messages may be written to the cache but not to the disk. If something happens here, like a crash (very unlikely), message that were not written to the disk get lost. Here is a note from the documentation (link here)
Note on message persistence
Marking messages as persistent doesn't fully guarantee that a message won't be lost. Although it tells RabbitMQ to save the message to disk, there is still a short time window when RabbitMQ has accepted a message and hasn't saved it yet. Also, RabbitMQ doesn't do fsync(2) for every message -- it may be just saved to cache and not really written to the disk. The persistence guarantees aren't strong, but it's more than enough for our simple task queue. If you need a stronger guarantee then you can use publisher confirms.
Publisher confirms are the needed mechanism to guarantee that messages don't get lost.
I cannot understand how to enable this with Node.js, and in particular with the amqplib
package (github code).
Digging a little in the library I have found this:
var open = require('amqplib').connect();
open.then(function(c) {
c.createConfirmChannel().then(function(ch) {
ch.sendToQueue('foo', new Buffer('foobar'), {},
function(err, ok) {
if (err !== null)
console.warn('Message nacked!');
else
console.log('Message acked');
});
});
});
But I am not sure this is all I need. Also, what should I do on the consumer side? Connecting in the same way, creating the channel again with createConfirmChannel
?
Upvotes: 3
Views: 2240
Reputation: 9667
Your code is correct, createConfirmChannel
creates a channel with publisher confirms enabled. You should check out this example code as well.
Upvotes: 5
Reputation: 11
You pretty much can't, whenever message gets lost, you're able to catch an Exception I think. These are handled using the try / catch construct provided by the JS.
Upvotes: 0