Skip to main content

Working with Policies

1. Creating Policy

Property Description Values
name Name of the policy string
pattern Regular expression to match queue/exchange names string
definition JSON object defining the policy rules object
priority Priority of the policy (0-2147483647) integer

Example: Creating a Policy

rabbitmq-policy set my-policy "^my-queue" '{"ha-mode":"all"}' 1
Note: Policies are applied to queues and exchanges matching the pattern.

2. Using Policy Pattern

Example: Define a policy for message TTL

{
  "name": "message-ttl-policy",
  "pattern": "^queue-.*",
  "definition": {
    "x-message-ttl": 30000
  },
  "priority": 1
}
Property Description Values
name Unique identifier for the policy string
pattern Regex to match queue names string
definition Policy configuration as key-value pairs object
priority Higher values override lower ones integer
Note: Policies apply to queues matching the pattern. They can override queue-level settings.
Warning: Policies are applied at queue creation time. Changing a policy does not affect existing queues.

3. Setting Policy Priority

Property Values Description
priority 0-100 Defines the priority of the policy. Higher numbers indicate higher priority.

Example: Setting Policy Priority

rabbitmq-policy set --priority 50 my-policy-name ^my-queue$ '{"max-length": 1000}'

4. Setting HA Policy

Property Value Description
pattern ^my-queue.* Regular expression to match queue names
definition ha-mode Policy to apply to matching queues
priority 0 Priority of the policy (higher number = higher priority)

Example: Setting HA Policy

rabbitmq-policy set ha-all "^my-queue.*" '{"ha-mode":"all"}'
Note: The ha-mode can be set to all, exactly, or nodedown.
Warning: Ensure that the pattern matches the intended queues to avoid unintended policy application.

5. Setting Federation Policy

Property Value Description
federation.upstream string Name of the upstream federation exchange or queue
federation.upstream-exchange string Name of the upstream exchange to federate
federation.max-hops integer Maximum number of federation hops allowed (default: 1)
federation.reconnect-delay integer Seconds to wait before reconnecting (default: 5)

Example: Setting Federation Policy

rabbitmq-policy set federation-policy "federation-upstream" "^federated\." "{\"federation.upstream\":\"upstream-exchange\",\"federation.max-hops\":2}"
Note: Policies are applied to queues and exchanges matching a given pattern. Ensure the policy is set on the correct queue or exchange for federation to work.

6. Setting TTL Policy

Property Value Description
ttl 3600000 TTL in milliseconds (1 hour)
message-ttl 3600000 Time-to-live for messages in queue (1 hour)

Example: Set TTL Policy

rabbitmq-policy set ttl ^queue-name$ "{""message-ttl"":3600000}"
Note: The policy applies to all queues matching the pattern ^queue-name$.

7. Setting Max-Length Policy

Policy Name Value Description
max-length 1000 Maximum number of messages to keep in a queue before removing the oldest ones
max-length-bytes 10485760 Maximum total size of messages in bytes to keep in a queue before removing the oldest ones

Example: Setting max-length policy via CLI

rabbitmq-policy set --vhost / my-max-length-policy "^\w+queue$" "{\"max-length\": 1000}"

Example: Setting max-length-bytes policy via CLI

rabbitmq-policy set --vhost / my-max-length-bytes-policy "^\w+queue$" "{\"max-length-bytes\": 10485760}"
Note: The max-length policy applies to all queues matching the pattern, and will remove messages in FIFO order when the limit is reached.

8. Setting Queue Mode

Mode Description Configuration
classic Standard queue type, stored in memory and disk "queue-mode": "classic"
lazy Messages are stored on disk and only loaded into memory when needed "queue-mode": "lazy"
quorum Highly available queue, replicated across cluster nodes "queue-mode": "quorum"

Example: Setting Queue Mode via Policy

{
  "pattern": "^myqueue.*",
  "definition": {
    "queue-mode": "lazy"
  },
  "priority": 10
}
Note: Queue mode can be set at queue creation time or via a policy. Changing the mode of an existing queue requires deletion and recreation.

9. Combining Multiple Definitions

Example: Combining Policy Definitions

{
  "definition": "{\\"ha-mode\\":\\"all\\"}",
  "pattern": "^ha-.*$",
  "priority": 0,
  "apply-to": "all"
}
Property Description Values
definition JSON string defining the policy {"ha-mode":"all"}
pattern Regex to match queue names ^ha-.*$
priority Policy priority (0-2147483647) 0
apply-to Which entities the policy applies to all
Note: Policies are applied in order of priority, with higher numbers taking precedence.
Warning: Ensure pattern matches are specific enough to avoid unintended application.

10. Understanding Policy Application

Policy Property Description Values
pattern Regular expression to match queue/exchange names ^myqueue.*
definition JSON object defining the policy rules {"ha-mode":"all"}
priority Higher values take precedence over lower ones 0

Example: Applying a Policy to Queues

rabbitmq-policy set ha-all "^myqueue.*" '{"ha-mode":"all"}' --priority 1
Note: Policies are applied at the virtual host level and can target queues, exchanges, or both.

11. Listing Policies

Policy Name Pattern Definition Priority
ha-all .*
{
  "ha-mode": "all"
}
0
ttl-30days logs.*
{
  "message-ttl": 2592000
}
1

Example: List all policies

rabbitmqctl list_policies

Example: List policies with specific pattern

rabbitmqctl list_policies 'ha-.*'

12. Clearing Policy

Property Description Values
policy-name Name of the policy to clear string
vhost Virtual host where the policy is defined string

Example: Clearing a Policy

rabbitmqctl clear_policy -p /my_vhost my_policy_name
Note: The policy will be removed from the specified virtual host. If no virtual host is specified, the default vhost (/) will be used.

Was this article helpful?