Persistent messages (Intermediate)

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.

Getting ready

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.

Note

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

How to do it...

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.

  1. The first change we have made is to set durable to true when creating a queue:
    var q = connect.queue('shop.queue', {durable:true});
  2. We also mark our messages as persistent by supplying a delivery_mode property with a value 2.
    ex.publish('order.key', newOrder, {deliveryMode:2});

Take a look at our consumer.js script:

  1. When we create our queue, we set durable to true:
    var q = connect.queue('shop.queue', {durable:true});

Let's demonstrate this with the following set of commands:

  1. Open a command-line console and start RabbitMQ:
    rabbitmq-server
    
  2. Now open a command-line console, navigate to our source code examples folder, and locate the folder 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.

  1. Let's open another command-line console and run the 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.
    
  2. Now run the following command to stop and then restart RabbitMQ:
    rabbitmqctl stop
    rabbitmq-server
    
  3. Now, again run the list_queues command. You will notice that the response still contains queues/messages:
    rabbitmqctl list_queues
    Listing queues …
    shop.queue 10
    ...done.
    

Note

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.179.100