Exclusive & Auto-Delete Queues
IntermediateExclusive queues are accessible by a single connection and deleted when it closes; auto-delete queues are deleted when the last consumer unsubscribes — ideal for temporary reply queues.
Overview
Exclusive queues can only be accessed (declared, consumed, purged) by the connection that declared them and are deleted automatically when that connection closes. They are ideal for RPC reply queues or per-session fanout subscriptions. Auto-delete queues persist until their last consumer disconnects, then are automatically deleted. These two flags serve overlapping but distinct purposes: exclusive ties the queue lifecycle to a connection; auto-delete ties it to consumer presence. Combining both (exclusive=true, auto-delete=true) produces a queue that is both private and cleaned up on consumer disconnect.
Exclusive Queues for RPC
Each RPC client declares a unique exclusive reply queue, publishes requests with reply-to set to that queue name, and the server routes responses back. The queue is automatically deleted when the client connection closes.
// RPC client — declare a unique exclusive reply queue
String replyQueueName = channel.queueDeclare().getQueue(); // auto-named, exclusive, auto-delete
AMQP.BasicProperties props = new AMQP.BasicProperties.Builder()
.correlationId(UUID.randomUUID().toString())
.replyTo(replyQueueName)
.build();
channel.basicPublish("", "rpc.requests", props, request.getBytes());
// Spring AMQP RPC — RabbitTemplate handles reply queue automatically
Object response = rabbitTemplate.convertSendAndReceive(
"rpc.requests", requestPayload);Auto-Delete Queues for Fanout Subscribers
When a subscriber connects to a fanout exchange it declares a private, auto-delete queue and binds it. When the subscriber disconnects, the queue (and unread messages in it) is cleaned up automatically — perfect for live-feed or broadcast patterns where stale messages have no value.
@Configuration
public class LiveFeedConfig {
@Bean
public FanoutExchange liveFeedExchange() {
return new FanoutExchange("feed.live");
}
// Each application instance gets its own auto-delete queue
@Bean
public Queue subscriberQueue() {
return QueueBuilder.nonDurable() // auto-named
.autoDelete()
.build();
}
@Bean
public Binding subscriberBinding(Queue subscriberQueue, FanoutExchange liveFeedExchange) {
return BindingBuilder.bind(subscriberQueue).to(liveFeedExchange);
}
}Differences and Gotchas
Exclusive and auto-delete sound similar but have important behavioural differences. Exclusive deletes on connection close regardless of consumers; auto-delete deletes when the consumer count drops to zero (and it had at least one consumer). An auto-delete queue with no consumer that was never consumed is not deleted.
// Exclusive queue — tied to connection lifecycle
Queue exclusive = QueueBuilder.nonDurable("my-exclusive")
.exclusive() // deleted when declaring connection closes
.build();
// Auto-delete queue — tied to consumer lifecycle
Queue autoDelete = QueueBuilder.durable("my-autodelete")
.autoDelete() // deleted when last consumer leaves
.build();
// Combined: exclusive + auto-delete (most restrictive)
Queue temp = QueueBuilder.nonDurable()
.exclusive()
.autoDelete()
.build();
// Auto-named, private, deleted on connection closeKey Points to Remember
- 1Exclusive queue: only the declaring connection can use it; deleted when that connection closes
- 2Auto-delete queue: deleted when the last consumer unsubscribes (must have had at least one)
- 3RPC reply queues are the canonical use case for exclusive + auto-delete queues
- 4Auto-delete fanout subscriber queues prevent stale message accumulation for absent subscribers
- 5Exclusive queues cannot be shared across connections — use durable queues for shared consumers
- 6An auto-delete queue that never had a consumer is NOT automatically deleted
Interview Questions
Sign in to ask AriaWhat is the difference between an exclusive queue and an auto-delete queue?
When is an auto-delete queue NOT automatically deleted?
Why are exclusive queues preferred for RPC reply patterns?
How do you declare an anonymous (auto-named) temporary queue in Spring AMQP?
What happens to messages in an auto-delete queue when it is deleted?
Ask Aria about Exclusive & Auto-Delete Queues
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.