ActiveMQ Queues vs Topics Interview Questions and Answers

Learn the difference between Queues and Topics in Apache ActiveMQ with interview questions, Mermaid diagrams, Spring Boot examples, and production best practices.

Queues vs Topics - Interview Questions & Answers

One of the most common ActiveMQ interview questions is:

What is the difference between a Queue and a Topic?

Apache ActiveMQ supports two messaging models defined by the Java Message Service (JMS) specification:

  • Point-to-Point (Queue)
  • Publish-Subscribe (Topic)

Choosing the right messaging model is critical for building scalable enterprise applications.


Messaging Models

flowchart LR

Producer --> Queue

Queue --> Consumer

Producer --> Topic

Topic --> Subscriber1

Topic --> Subscriber2

Topic --> Subscriber3

Q1. What is a Queue in ActiveMQ?

Answer

A Queue is a Point-to-Point (P2P) messaging model where a message is consumed by only one consumer.

Characteristics:

  • One Producer
  • One or Many Consumers
  • One Message → One Consumer
  • Reliable Delivery
  • Load Balancing

Queue Architecture

flowchart LR

Producer --> Queue

Queue --> ConsumerA["Consumer A"]

Queue --> ConsumerB["Consumer B"]

Queue --> ConsumerC["Consumer C"]

Only one consumer receives each message.


Q2. What is a Topic in ActiveMQ?

Answer

A Topic follows the Publish-Subscribe (Pub/Sub) messaging model.

Every active subscriber receives a copy of the published message.

Characteristics:

  • One Publisher
  • Multiple Subscribers
  • One Message → Multiple Consumers
  • Event Broadcasting

Topic Architecture

flowchart LR

Publisher --> Topic

Topic --> SubscriberA["Subscriber A"]

Topic --> SubscriberB["Subscriber B"]

Topic --> SubscriberC["Subscriber C"]

Every subscriber receives the same message.


Q3. What is the difference between Queue and Topic?

Answer

Feature Queue Topic
Messaging Model Point-to-Point Publish-Subscribe
Consumers One consumer processes each message All subscribers receive the message
Load Balancing Yes No
Event Broadcasting No Yes
Best For Work Distribution Notifications

Comparison

flowchart TD

Queue --> SingleConsumer["Single Consumer"]

Topic --> MultipleSubscribers["Multiple Subscribers"]

Interview Tip

Queue = Work Distribution

Topic = Event Distribution


Q4. When should we use a Queue?

Answer

Queues are ideal when a task should be processed only once.

Examples:

  • Payment Processing
  • Order Processing
  • Invoice Generation
  • File Processing
  • Background Jobs

Queue Use Case

flowchart LR

OrderService["Order Service"] --> OrderQueue["Order Queue"]

OrderQueue["Order Queue"] --> PaymentService["Payment Service"]

Only one Payment Service instance processes the order.


Q5. When should we use a Topic?

Answer

Topics are used when multiple systems need to receive the same event.

Examples:

  • Order Created Event
  • Customer Registration
  • Stock Price Updates
  • Audit Events
  • Notifications

Topic Use Case

flowchart LR

OrderService["Order Service"] --> OrderTopic["Order Topic"]

OrderTopic["Order Topic"] --> InventoryService["Inventory Service"]

OrderTopic["Order Topic"] --> NotificationService["Notification Service"]

OrderTopic["Order Topic"] --> AnalyticsService["Analytics Service"]

Every service receives the event.


Q6. How do Queues provide load balancing?

Answer

When multiple consumers listen to the same queue:

  • Broker distributes messages
  • Each message goes to only one consumer
  • Processing is shared

Queue Load Balancing

flowchart TD

Queue --> Consumer1["Consumer 1"]

Queue --> Consumer2["Consumer 2"]

Queue --> Consumer3["Consumer 3"]

Consumer1["Consumer 1"] --> MessageA["Message A"]

Consumer2["Consumer 2"] --> MessageB["Message B"]

Consumer3["Consumer 3"] --> MessageC["Message C"]

Benefits

  • Faster processing
  • Better scalability
  • Higher throughput

Q7. What are Durable and Non-Durable Subscribers in Topics?

Answer

Non-Durable Subscriber

Receives messages only while connected.

Durable Subscriber

Receives messages published while temporarily offline.

Durable Subscriber Flow

flowchart LR

Publisher --> Topic

Topic --> DurableSubscriber["Durable Subscriber"]

Topic --> NondurableSubscriber["Non-Durable Subscriber"]

Best Practice

Use Durable Subscribers for important business events.


Q8. How does Spring Boot use Queues and Topics?

Answer

Spring Boot integrates with ActiveMQ using Spring JMS.

Queue Example:

JmsTemplate

↓

Queue

↓

@JmsListener

Topic Example:

JmsTemplate

↓

Topic

↓

Multiple Subscribers

Spring Boot Integration

flowchart TD

SpringBoot["Spring Boot"] --> JmsTemplate

JmsTemplate --> Queue

JmsTemplate --> Topic

Queue --> Consumer

Topic --> Subscribers

Q9. What are common Queue vs Topic implementation mistakes?

Answer

Common mistakes include:

  • Using Topics for work distribution
  • Using Queues for event broadcasting
  • Ignoring durable subscriptions
  • Unlimited queue growth
  • Missing Dead Letter Queue
  • Broadcasting unnecessary events
  • Not monitoring consumer lag

Wrong Design

Order Queue

↓

Inventory

↓

Notification ❌

Correct Design

Order Topic

↓

Inventory

↓

Notification

↓

Analytics ✅

Q10. What are the enterprise best practices for using Queues and Topics?

Answer

Follow these recommendations:

Use Queues For

  • Payment Processing
  • Background Jobs
  • Order Processing
  • Batch Processing
  • Task Scheduling

Use Topics For

  • Domain Events
  • Notifications
  • Audit Logs
  • Event Streaming
  • Microservice Communication

Enterprise Architecture

flowchart TD

OrderService["Order Service"] --> OrderQueue["Order Queue"]

OrderQueue["Order Queue"] --> PaymentService["Payment Service"]

OrderService["Order Service"] --> OrderTopic["Order Topic"]

OrderTopic["Order Topic"] --> Inventory

OrderTopic["Order Topic"] --> Shipping

OrderTopic["Order Topic"] --> Notification

OrderTopic["Order Topic"] --> Analytics

Messaging Decision

flowchart LR

BusinessEvent["Business Event"] --> NeedOneConsumer["Need One Consumer?"]

NeedOneConsumer["Need One Consumer?"] -- Yes --> Queue

NeedOneConsumer["Need One Consumer?"] -- No --> Topic

Queue vs Topic

mindmap
  root((Messaging Models))
    Queue
      Point to Point
      Load Balancing
      One Consumer
    Topic
      Publish Subscribe
      Multiple Consumers
      Event Broadcasting

Real-World Banking Example

Queue Example

ATM Withdrawal Request

↓

Withdrawal Queue

↓

Transaction Processor

Only one processor should complete the withdrawal.


Topic Example

Account Created Event

↓

Customer Topic

↓

Fraud Detection

↓

Notification Service

↓

CRM

↓

Analytics

↓

Audit Service

Every service receives the same event independently.


Senior Interview Tip

Choosing the correct messaging model is an architectural decision.

A production-ready enterprise system typically uses:

Queue

  • Payment Processing
  • Loan Processing
  • Batch Jobs
  • Background Tasks
  • Order Fulfillment

Topic

  • Event-Driven Architecture
  • Microservices
  • Notifications
  • Auditing
  • Real-Time Analytics
  • Event Streaming

Enterprise implementations also include:

  • Spring Boot
  • Spring JMS
  • ActiveMQ Cluster
  • Persistent Messaging
  • Dead Letter Queues
  • Durable Subscribers
  • Monitoring
  • High Availability

Remember:

  • Queue = One message → One consumer
  • Topic = One message → Many subscribers

Quick Revision

  • Queue follows the Point-to-Point messaging model.
  • Topic follows the Publish-Subscribe messaging model.
  • Queue distributes work among consumers.
  • Topic broadcasts events to all subscribers.
  • Use Queues for business processing.
  • Use Topics for event notifications.
  • Durable subscribers receive missed messages.
  • Spring Boot supports both through Spring JMS.
  • Monitor queues, topics, and subscribers in production.
  • Choose the messaging model based on business requirements rather than implementation convenience.