Our sHop application is business critical. Losing sHop orders is not an option; we need to ensure every message is delivered and processed correctly. The next three tasks explore our options for reliable message delivery. This task will examine persistent messages.
If RabbitMQ stops for any reason, we will lose all of our exchanges, queues, and more importantly our messages (orders in our application).
In order to make sure that messages are not lost in the event of RabbitMQ restarting, we need to mark the queues durable and messages as persistent.
If you make changes to the properties of exchanges and queues that already exist in the RabbitMQ server, our RabbitMQ client will throw an error when trying to connect. When starting this task and each new task, please remove all the data (messages, exchanges, and queues) from the RabbitMQ database, by executing the following commands:
rabbitmqctl stop_app rabbitmqctl reset rabbitmqctl start_app
Let's navigate to our source code examples folder and locate the folder Persistent-Messages
. These scripts simply builds on the code from the previous task. Take a look at the producer.js
script and examine the changes made to persist messages.
durable
to true
when creating a queue:var q = connect.queue('shop.queue', {durable:true});
delivery_mode
property with a value 2
.ex.publish('order.key', newOrder, {deliveryMode:2});
Take a look at our consumer.js
script:
durable
to true
:var q = connect.queue('shop.queue', {durable:true});
Let's demonstrate this with the following set of commands:
rabbitmq-server
Persistent-Messages
. Execute the following command:Persistent-Messages> node producer
Let the producer create several message/orders, press Ctrl + C while on the console to stop the producer creating orders.
list_queues
command:rabbitmqctl list_queues
The response should display shop queue; details include the name and the number of messages ready to be processed.
Listing queues ... shop.queue 10 ...done.
rabbitmqctl stop rabbitmq-server
list_queues
command. You will notice that the response still contains queues/messages:rabbitmqctl list_queues Listing queues … shop.queue 10 ...done.
Marking messages as persistent tells RabbitMQ to persist the message to the disk. This does not completely guarantee that a message won't be lost. There exists a short time window when RabbitMQ has accepted a message and hasn't saved it, in which a RabbitMQ failure will result in a message being lost. Task Publisher Confirms will explore a solution to this.
3.138.179.100