Virtual Hosts (vhosts)
Beginnervhosts provide logical namespacing within a single RabbitMQ instance; exchanges, queues, and bindings in one vhost are completely isolated from another.
Overview
A virtual host (vhost) in RabbitMQ is a logical partition of the broker — it has its own exchanges, queues, bindings, user permissions, and policies. Resources in one vhost are invisible to connections in another, just like separate databases in a relational DB server. The default vhost is "/". Vhosts are ideal for multi-tenant deployments, environment separation (dev/staging on one broker), or team isolation without spinning up separate brokers. Each user is granted access to specific vhosts with configure, write, and read permission regexes.
Creating and Connecting to Vhosts
Vhosts are managed via the management UI, CLI (rabbitmqctl), or HTTP API. Applications specify the vhost in the connection URI.
# Create a vhost
rabbitmqctl add_vhost orders
# Grant user "order-svc" full access to /orders vhost
rabbitmqctl set_permissions -p orders order-svc ".*" ".*" ".*"
# Connection URI with vhost
# amqp://user:password@host:5672/orders
# Spring Boot application.yml
spring:
rabbitmq:
host: rabbitmq.internal
port: 5672
username: order-svc
password: secret
virtual-host: ordersVhost Isolation in Practice
Two services in different vhosts can have exchanges with the same name without collision. This is useful when deploying multiple environments on a shared broker.
# Development environment: vhost = /dev
rabbitmqctl add_vhost /dev
rabbitmqctl set_permissions -p /dev dev-user ".*" ".*" ".*"
# Staging environment: vhost = /staging
rabbitmqctl add_vhost /staging
rabbitmqctl set_permissions -p /staging staging-user ".*" ".*" ".*"
# Both vhosts can have an exchange named "orders" — fully isolated
# /dev/orders exchange and /staging/orders exchange never interact
# List vhosts
rabbitmqctl list_vhostsVhost Policies and Limits
Policies apply to queues/exchanges matching a pattern within a vhost. You can set max queue length, message TTL, or HA mirrors per vhost. Max-connections and max-channels per vhost prevent noisy-neighbour issues.
# Apply a policy to all queues in /orders vhost (max 100k messages)
rabbitmqctl set_policy -p orders MaxLen ".*" '{"max-length": 100000, "overflow": "reject-publish"}' --apply-to queues
# Limit connections per vhost (management HTTP API)
PUT /api/vhost-limits/orders
{ "max-connections": 50, "max-queues": 200 }
# List policies for a vhost
rabbitmqctl list_policies -p ordersKey Points to Remember
- 1Vhosts provide complete isolation: exchanges, queues, and bindings are scoped to a single vhost
- 2Default vhost is "/"; connect with virtual-host: / in Spring or amqp://host/vhost in URIs
- 3User permissions are per-vhost with configure (declare), write (publish), read (consume) regexes
- 4Use vhosts for environment separation (dev/staging) or team isolation on a shared broker
- 5Policies (TTL, max-length, HA) are applied at vhost level and affect only resources within it
- 6Vhost limits (max-connections, max-queues) prevent one tenant from monopolising broker resources
Interview Questions
Sign in to ask AriaWhat is a RabbitMQ virtual host and how does it differ from a separate broker?
How do you grant a user access to a specific vhost?
How would you use vhosts to isolate dev and staging environments?
What are the three permission types per vhost and what do they control?
How do vhost-level policies differ from per-queue arguments?
Ask Aria about Virtual Hosts (vhosts)
Your personal AI tutor — ask anything about this concept
Revision Status
Personal Notes
Sign in to save personal notes for this topic.
Discussion
Sign in to join the discussion.