Skip to content

RabbitMQ shovel across vhosts — moving messages between tenants on the same broker

A walkthrough of dynamic shovels for moving messages between vhosts on a single RabbitMQ cluster — tenant migrations, queue drains, and the gotchas of in-cluster shovels.

A multi-tenant RabbitMQ broker had grown three vhosts over five years — prod, prod-eu, and an accidental prod-test that someone created during a 2022 migration and never tore down. The prod-test vhost had 80,000 messages stuck in a deprecated queue that nobody wanted to delete because nobody knew whether the consumer was still out there. This article walks through using the shovel plugin to drain or copy messages across vhosts on the same cluster — a different problem from cross-cluster federation, and one that catches teams who reach for federation by reflex.

How to verify

Confirm the shovel plugin is on and that you have admin rights:

sudo rabbitmq-plugins list | grep shovel
sudo rabbitmqctl list_vhosts
sudo rabbitmqctl list_user_permissions $(whoami) 2>/dev/null || \
  sudo rabbitmqctl list_user_permissions admin

# Confirm the source queue exists and has messages
sudo rabbitmqctl list_queues -p prod-test name messages messages_ready

# Confirm a destination queue exists on the target vhost
sudo rabbitmqctl list_queues -p prod name durable

If rabbitmq_shovel and rabbitmq_shovel_management are not in the list, enable both — the management half adds shovel status to the web UI and the HTTP API.

What’s happening

A RabbitMQ shovel is an AMQP-to-AMQP forwarder running inside the broker. It opens a connection as a consumer on a source (a queue or exchange), pulls messages, and republishes them to a destination. The source and destination can be the same broker, the same vhost, different vhosts, or different brokers entirely.

For the cross-vhost case on the same cluster, the shovel still talks AMQP — it connects to itself over the loopback interface. That sounds wasteful, and it is in microseconds, but it’s what gives you the consistent semantics: the same configuration that moves messages between brokers across regions also moves them between vhosts on the same machine. One mental model, one set of operational tools.

Two flavors of shovel:

  • Static shovel. Configured in rabbitmq.conf (or advanced.config); starts at broker startup, requires a config change and restart to modify. Use for permanent topology where the shovel is part of the architecture (e.g. always drain dead-letter queue into a downstream cluster).
  • Dynamic shovel. Declared at runtime via rabbitmqctl set_parameter shovel or the HTTP API; lives in Mnesia, replicates across the cluster, can be deleted with a command. Use for one-off migrations, ad-hoc queue drains, or any shovel whose lifetime is bounded.

Almost every cross-vhost move is a dynamic shovel — you’re doing it because of a specific event (tenant offboarding, deprecation, rebalance), and you want to delete the shovel when the queue is empty.

A shovel has an ack-mode: on-confirm (the safe default — wait for the destination publish confirm before acking the source), on-publish (ack the source as soon as the destination accepts; faster, can lose messages on destination failure), and no-ack (acked at the source unconditionally; only use for replay-friendly streams).

The procedure

  1. Create a shovel user with permissions on both vhosts. The shovel authenticates as an AMQP client on both ends; if the source and destination are different vhosts, the user must have permissions on each:

    sudo rabbitmqctl add_user shovel-runner "$(openssl rand -base64 24)"
    
    # Source vhost: read the source queue
    sudo rabbitmqctl set_permissions -p prod-test shovel-runner \
      "" "^orders\.legacy$" "^orders\.legacy$"
    
    # Destination vhost: write to the destination exchange or queue
    sudo rabbitmqctl set_permissions -p prod shovel-runner \
      "^orders\.legacy$" "^orders\.legacy$" ""

    Tight permissions are non-negotiable — a typo in the shovel config that points the destination at the wrong exchange can flood production with messages from a deprecated tenant. The permission regex caps the blast radius.

  2. Declare the dynamic shovel. The source URI carries the source vhost, the destination URI carries the destination vhost — both point at amqp://localhost:

    sudo rabbitmqctl set_parameter shovel drain-prod-test-orders \
      '{
        "src-protocol": "amqp091",
        "src-uri": "amqp://shovel-runner:PASS@localhost/prod-test",
        "src-queue": "orders.legacy",
        "src-prefetch-count": 200,
        "dest-protocol": "amqp091",
        "dest-uri": "amqp://shovel-runner:PASS@localhost/prod",
        "dest-queue": "orders.legacy",
        "ack-mode": "on-confirm",
        "src-delete-after": "queue-length"
      }'

    The single most important key for a one-shot drain is src-delete-after: queue-length — the shovel reads the source queue’s message count at startup and stops itself after moving that many. Without it, the shovel runs forever and any new message published into the source vhost gets shovelled too.

  3. Watch the shovel run. The web UI has a Shovels tab; from CLI:

    sudo rabbitmqctl shovel_status
    # name, vhost, type, state, src_uri, dest_uri, ...
    
    # Or via the HTTP API (more detail)
    curl -s -u admin:PASS http://localhost:15672/api/shovels/%2F \
      | jq '.[] | {name, state, src_protocol, dest_protocol}'

    state: running is healthy. state: terminated means the shovel finished (you’ll see this after queue-length messages have moved). state: starting for more than 30 seconds usually means a credential or vhost-permission problem.

  4. Verify the move. Source should be empty (assuming nothing is still publishing); destination should have the original count:

    sudo rabbitmqctl list_queues -p prod-test name messages
    sudo rabbitmqctl list_queues -p prod name messages

    If the source still has messages, check whether a producer is still active on it — src-delete-after: queue-length only counts the initial size. Use never if you want a long-running tail.

  5. Delete the shovel parameter once you’re done. Even a terminated shovel remains as a parameter in Mnesia and will restart if the broker restarts:

    sudo rabbitmqctl clear_parameter shovel drain-prod-test-orders
    sudo rabbitmqctl shovel_status | grep -c drain-prod-test-orders
    # should print 0

    Then delete the source queue or vhost if that was the goal:

    sudo rabbitmqctl delete_queue -p prod-test orders.legacy
    # or, after confirming no other queues exist on the vhost
    sudo rabbitmqctl delete_vhost prod-test

Operational notes

  • The shovel is a process inside the broker; CPU and memory show up on the broker, not on a sidecar. A drain of 80,000 small messages takes seconds; a drain of 10 million large messages can run for hours and chew memory on a hot cluster.
  • src-prefetch-count controls how many messages the shovel holds in-flight. Default 1000. Lower it (200-500) for large-payload queues to avoid memory spikes; raise it for tiny messages where the round-trip dominates.
  • ack-mode: on-confirm requires publisher confirms to be supported on the destination. If you’re shovelling to an exchange with no bound queues, the publish “succeeds” but the message goes nowhere — the confirm fires, the source acks, and the message is lost. Verify destination bindings before starting.
  • Shovels run on a single node in the cluster. If that node restarts mid-drain, the shovel resumes on whichever node picks up the parameter — message order is not preserved across the restart.
  • You can shovel between different protocols (AMQP 0.9.1 to AMQP 1.0) with the appropriate src-protocol and dest-protocol. Useful when moving from a legacy producer to a 1.0-native one without re-coding the consumer.
  • A shovel between two vhosts on the same cluster is the simplest way to copy a queue’s contents without losing the original — set ack-mode: on-publish and the source isn’t acked (messages stay), but the destination still receives a copy.

Stack Harbor runs RabbitMQ migrations and tenant cleanups as a routine part of managed messaging infrastructure — shovels declared, drained, and torn down with monitoring on confirm latency and queue depth so we know the move is healthy before the source vhost is deleted. If you’ve got a multi-vhost broker carrying historical baggage you want to consolidate, this is the kind of work we cover under managed operations.