Understanding Virtual Hosts
1. Understanding Default vhost
| Property | Description | Default Value |
|---|---|---|
| / | Default virtual host in RabbitMQ | / |
Example: Connecting to Default vhost
const amqp = require('amqplib');
amqp.connect('amqp://user:pass@localhost:5672/') // connects to default vhost /
.then(conn => {
return conn.createChannel();
})
.then(ch => {
// Channel operations here
});
Note: The default vhost (
/) is automatically created when RabbitMQ is installed and is used for basic messaging operations if no specific vhost is specified.
2. Creating Virtual Host
| Property | Description | Default |
|---|---|---|
| name | Name of the virtual host to create | None |
| tags | Comma-separated list of tags to associate with the virtual host | None |
| description | Description for the virtual host | None |
Note: Virtual hosts are isolated environments within RabbitMQ, allowing for logical separation of resources like queues and exchanges.
3. Listing Virtual Hosts
| Command | Description | Notes |
|---|---|---|
| rabbitmqctl list_vhosts | List all virtual hosts on the broker | Requires admin privileges |
| rabbitmqctl list_vhosts -q | Quiet mode, outputs only vhost names | Useful for scripting and parsing |
4. Deleting Virtual Host
| Property | Value | Description |
|---|---|---|
| vhost.name | my-vhost |
Name of the virtual host to delete |
Note: Deleting a virtual host will remove all queues, exchanges, and permissions associated with it.
Warning: This action is irreversible. Ensure all data is backed up before deletion.
5. Using vhost for Isolation
RabbitMQ virtual hosts (vhosts) provide a way to isolate resources and users within a single RabbitMQ instance. Each vhost acts as a separate "namespace" for exchanges, queues, and users.
| Feature | Description | Notes |
|---|---|---|
| Isolation Level | Complete isolation of exchanges, queues, and bindings | Resources in one vhost do not interfere with another |
| User Access Control | Users can be granted permissions to specific vhosts | Permissions are scoped per vhost, not global |
| Resource Limits | Can apply limits to memory, file descriptors, etc. | Configurable per vhost via rabbitmq.conf |
Example: Creating a vhost and setting permissions
rabbitmqctl add_vhost /myapp
rabbitmqctl set_permissions -p /myapp guest ".*" ".*" ".*"
Note: By default, the
guest user has full access to all vhosts. It's recommended to create dedicated users for each application or environment.
Best Practices for vhost Isolation
- Create dedicated vhosts per application or environment
- Use specific user permissions for each vhost
- Apply resource limits to prevent one application from affecting others
6. Setting vhost Limits
| Property | Values | Description |
|---|---|---|
| max_connections | 100 |
Maximum number of connections allowed per virtual host |
| max_channels | 1000 |
Maximum number of channels allowed per virtual host |
| max_queues | 500 |
Maximum number of queues allowed per virtual host |
| max_exchanges | 500 |
Maximum number of exchanges allowed per virtual host |
Example: Configuring vhost limits via rabbitmq.conf
# rabbitmq.conf
vhost_limits.max_connections = 100
vhost_limits.max_channels = 1000
vhost_limits.max_queues = 500
vhost_limits.max_exchanges = 500
Example: Setting limits using rabbitmqctl
rabbitmqctl set_vhost_limits my_vhost max_connections 100
rabbitmqctl set_vhost_limits my_vhost max_channels 1000
Note: These limits apply per virtual host and are enforced by the broker. Exceeding limits will result in connection/channel rejection.
7. Granting User Permissions
| Permission | Resource | Pattern | Access |
|---|---|---|---|
| configure | .* |
.* |
Read and write configuration |
| read | .* |
.* |
Read messages and metadata |
| write | .* |
.* |
Write messages and metadata |
Note: The permission patterns use regular expressions to define access rights for queues, exchanges, and vhosts.
8. Configuring vhost Metadata
| Property | Value | Description |
|---|---|---|
| name | string |
Name of the virtual host |
| description | string |
Human-readable description of the vhost |
| tags | array<string> |
Array of tags for categorizing the vhost |
| cluster_state | string |
State of the cluster (e.g., running, stopped) |
Example: Setting vhost metadata via CLI
rabbitmqctl set_vhost_parameter / my-vhost description "Production vhost for user service"
rabbitmqctl set_vhost_parameter / my-vhost tags "["production", "user-service"]"
Example: Setting vhost metadata via Management API
const axios = require('axios');
const config = {
method: 'put',
url: 'http://localhost:15672/api/vhosts/%2F',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + Buffer.from('guest:guest').toString('base64')
},
data: {
description: 'Production vhost for user service',
tags: ['production', 'user-service']
}
};
axios(config)
.then(response => console.log('Vhost metadata updated'))
.catch(error => console.error('Error:', error));
9. Monitoring vhost Resources
| Resource | Description | Monitoring Method | Notes |
|---|---|---|---|
| Memory Usage | Amount of memory used by the vhost | rabbitmqctl report or rabbitmq-diagnostics report |
Can be monitored via RabbitMQ Management Plugin |
| Disk Space | Amount of disk space used by the vhost | rabbitmq-diagnostics status |
Important for persistence and queue storage |
| Message Count | Number of messages in queues within the vhost | rabbitmqctl list_queues -p <vhost> |
Useful for identifying queue backlog |
| Connection Count | Active connections to the vhost | rabbitmqctl list_connections -p <vhost> |
Helps in identifying connection spikes |
| Channel Count | Active channels within the vhost | rabbitmqctl list_channels -p <vhost> |
Indicates concurrent operations |
10. Tracing vhost Activity
| Configuration Option | Description | Default Value |
|---|---|---|
| trace_pattern | Pattern to match vhost names for tracing | .* |
| trace_exchange | Exchange to publish trace messages to | amq.rabbitmq.trace |
Note: Tracing is enabled per-vhost and requires the
rabbitmq_tracing plugin to be installed.
Warning: Enabling tracing can significantly impact performance and should only be used for debugging purposes.
Tracing Configuration Summary
- Enable the
rabbitmq_tracingplugin usingrabbitmq-plugins enable - Configure trace patterns to target specific vhosts
- Set up an exchange for trace messages
- Monitor trace output to debug message flow
11. Backing Up vhost
| Command | Description | Notes |
|---|---|---|
| rabbitmqctl export_vhost | Exports the specified virtual host to a file | Requires appropriate permissions to access the vhost |
Note: The exported file includes all definitions and permissions for the virtual host.
Warning: Ensure that sensitive data in the backup is handled securely.
12. Understanding vhost Best Practices
Best Practices for RabbitMQ Virtual Hosts
- Use virtual hosts to isolate environments (e.g., dev, test, prod) or applications
- Assign dedicated virtual hosts to teams or projects to prevent resource contention
- Limit the number of vhosts per broker to avoid performance degradation
- Implement consistent naming conventions for virtual hosts (e.g.,
dev-app1,prod-api) - Use permissions carefully to restrict access to sensitive vhosts
- Avoid overloading a single vhost with too many queues or exchanges
| Best Practice | Description | Impact |
|---|---|---|
| Isolation | Each vhost provides a separate namespace for queues, exchanges, and bindings | Prevents naming conflicts and improves security |
| Resource Management | Virtual hosts can be configured with limits on memory and file descriptors | Prevents one application from consuming all broker resources |
| Access Control | Permissions can be set per vhost to restrict access to specific users or roles | Enables fine-grained security control |
Note: Virtual hosts are not suitable for multi-tenancy in high-security environments without additional isolation mechanisms.