# RabbitMQ Persistence and Durability

RabbitMQ has great durability support, but it’s usually not enabled by default in the drivers. To persist messages to disk, and thus survive a server restart, you need to publish to a durable exchange, the receiving queue has to be durable, and you got to set the “persistent” flag on the message you’re publishing.

```ruby
require "amqp"

EM.run do
  AMQP.connection = AMQP.connect ENV['CLOUDAMQP_URL'] || 'amqp://guest:guest@localhost'

  AMQP::Channel.new do |ch|
    # Define a durable exchange
    topic = ch.topic('mytopic', durable: true)

    # Define a durable queue, with replication (the x-ha-policy=all argument)
    q1 = ch.queue('my-queue', durable: true, arguments: { 'x-ha-policy' => 'all' })

    # bind the queue to the exchange, limiting to a routing key
    q1.bind(topic, routing_key: 'my.route')

    # Publish a message and set the "persistent" flag
    msg = { :a => 1 }.to_json
    topic.publish(msg, routing_key: 'my.route', persistent: true)
  end
end
```

In Python (with Pika) it’s a little different, there you set the delivery_mode to 2.

```python
channel.basic_publish(exchange='',
          routing_key="task_queue",
          body=message,
          properties=pika.BasicProperties(
             delivery_mode = 2, # make message persistent
          ))
```

## Replication High availability queues

Next Generation Highly Available Queues are called [Quorum queues,](https://www.rabbitmq.com/quorum-queues.html) and should be used in favour of [classic mirrored queues](https://www.rabbitmq.com/ha.html). The default quorum queue cluster size is set to the same value as the number of nodes in the cluster (on your selected CloudAMQP plan). See [“The reasons you should switch to Quorum Queues”](/blog/reasons-you-should-switch-to-quorum-queues/) for more information.

Queues in CloudAMQP are by default replicated between two nodes in the cluster.

More [information on HA](https://www.rabbitmq.com/ha.html) is available from RabbitMQ.com.
