RabbitMQ message persistence

Source: Internet
Author: User

RabbitMQ message persistence
I. Preface

If we want to avoid Message loss even when the RabbitMQ service is restarted, we can set both Queue and Message as durable ), this ensures that our RabbitMQ messages will not be lost in most cases. Of course there will still be some small probability events that will lead to message loss.

Ii. Queue persistence 2.1 check the number of existing queues and messages

In windows, view the information in the rabbitmq installation directory/sbin through rabbitmqctl. bat list_queues

Two producers are started here to generate two queues hello and hello1 respectively, and both of them have a message

Restart rabbitmq to simulate a fault

We can see that the two queues disappear after the restart.

2.2 persistent queue

We will keep the hello queue persistent.

When the queue name is declared, both the producer and consumer must be in the persistent queue.

channel.queue_declare(queue = 'hello' , durable = True )

We repeat the above operation, but do persistence for the hello queue, while hello1 does not, and restart rabbitmq

We can see that after the restart, the hello queue is still there, and the hello1 queue disappears, but a message in the original hello is not saved. So here we only achieve message queue persistence, and we have not made the message persistence.

Iii. Message persistence

We just realized that when rabbitmq crashes, the queue itself is saved and the queue is still running after the restart. Next, we will save the message, that is, message persistence.

channel.basic_publish(exchange = '',                       routing_key = 'hello' ,                       body = 'hello' ,                       properties = pika.BasicProperties(                           delivery_mode = 2 # make message persistent                       ))  # Add properties. This properties is the properties in the consumer callback function. # Delivery_mode = 2 persistent messages

The production end generates a message and restarts rabbitmq.

We can see that the hello message after the queue and message persistence does not disappear when it is restarted.

After the consumer end restarts, it can receive messages normally.

  

Iv. Summary
  1. The queue persistence parameter durable = True must be added when the queue is declared, so that the queue can be saved when rabbitmq crashes.
  2. Only durable = True can be used, and messages cannot be persisted only in the queue.
  3. Message persistence requires you to add the parameter properties = pika. BasicProperties (delivery_mode = 2) when the message is generated)

Setting up a RabbitMQ cluster in CentOS 7.2

Install and use professional message queue product RabbitMQ in CentOS7 Environment

RabbitMQ getting started

How to install RabbitMQ on CentOS7

How to Use NServiceBus with RabbitMQ

RabbitMQ cluster installation and configuration in CentOS 7

RabbitMQ practice: deploy distributed message queue in Chinese PDF

Detailed installation of RabbitMQ on CentOS7

RabbitMQ distributed cluster architecture and high availability (HA)

RabbitMQ details: click here
RabbitMQ: click here

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.