Updated on 2024-09-18 GMT+08:00

Configuring RabbitMQ Persistence

By default, messages produced by RabbitMQ producers are stored in the memory. When a node breaks down or restarts, messages are lost. RabbitMQ can persist data during such events for exchanges, queues, and messages.

Persistence means writing messages in the memory to the disk to prevent them from being lost due to exceptions. However, if message persistence is enabled, RabbitMQ performance deteriorates because read and write operations are much slower in disks than in memory. Different from the lazy queue mechanism, a persisted message is stored in both the disk and memory. It is deleted from the memory only when the memory is insufficient.

  • Non-persistent queues and exchanges are lost after a restart.
  • Non-persistent messages are lost after a restart. (Messages that are sent to persistent queues or exchanges will not automatically become persistent.)
  • A message will be lost if the server restarts before the message persistence is complete.
  • RabbitMQ AMQP-0-9-1 exchanges, queues, and messages support persistence.

Configuring Exchange Persistence

  • On the RabbitMQ management UI

    Set durable to true in exchange creation, as shown in Figure 1.

    Figure 1 Configuring exchange persistence (management UI)

    Figure 2 shows a successful configuration.

    Figure 2 Exchange persistence configured (management UI)
  • On the RabbitMQ console

    Configure exchange persistence when creating an exchange, as shown in Figure 3.

    Figure 3 Configuring exchange persistence (console)

    Figure 4 shows a successful configuration.

    Figure 4 Exchange persistence configured (console)

Configuring Queue Persistence

  • On the RabbitMQ management UI

    Set durable to true in queue creation, as shown in Figure 5.

    Figure 5 Configuring queue persistence (management UI)

    Figure 6 shows a successful configuration.

    Figure 6 Queue persistence configured (management UI)
  • On the RabbitMQ console

    Configure queue persistence when creating a queue, as shown in Figure 7.

    Figure 7 Configuring queue persistence (console)

    Figure 8 shows a successful configuration.

    Figure 8 Queue persistence configured (console)

Configuring Message Persistence

After configuring queue persistence, set MessageProperties to PERSISTENT_TEXT_PLAIN on the client to send persistent messages to the queue.

The following example shows how to configure message persistence on a Java client.

import com.rabbitmq.client.MessageProperties;
channel.basicPublish("", "my_queue",MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());