in Coding

Queues vs. Topics vs. Virtual Topics (in ActiveMQ)

ActiveMQ provides a variety of different messaging patterns. While queues and topics are the most famous ones, virtual topics can combine the best of both worlds: multiple consumers with their own dedicated queue.

Queues

Queues are the most obvious messaging pattern implemented by ActiveMQ. They provide a direct channel between a producer and a consumer. The producer creates messages, while the consumer reads one after another. After a message was read, it’s gone. If multiple consumers are registered for a queue, only one of them will get the message.

Pros

  • Simple messaging pattern with a transparent communication flow
  • Messages can be recovered by putting them back on the queue

Cons

  • Only one consumer can get the message
  • Implies a coupling between producer and consumer as it’s an one-to-one relation

Topics

Topics implement an one-to-many channel between a producer and multiple consumers. Unlike an queue, every consumer will receive a message send by the producer.

Pros

  • Multiple consumers can get a message
  • Decoupling between producer and consumers (publish-and-subscribe pattern)

Cons

  • More complicated communication flow
  • A message cannot be recovered for a single listener

Virtual Topics

Virtual topics combine both approaches. While the producer sends messages to a topic, consumers will receive a copy of the message on their own dedicated queue.

Pros

  • Multiple consumers can get a message
  • Decoupling between producer and consumers (publish-and-subscribe pattern)
  • Messages can be recovered by putting them back on the queue

Cons

  • Might require additional configuration in the broker

Conclusion

Both, queues and topics, have their own drawbacks. While queues tighten producer and consumer, topics lack of an easy way to recover errors of a single consumer. Virtual topics are provide a solution for both problems. Producer and consumers are decoupled by a publish-and-subscribe pattern while error recovery can be done on individual queues.

More

Best regards,
Thomas

Write a Comment

Comment

  1. Hi, I still do not fully understand how a topic works. For instance, if a consumer is connected to a topic, and suddenly disconnects, any message published after that will not be received by that consumer when it reconnects, correct?
    I’m not sure if that’s what “A message cannot be recovered for a single listener” means.
    And in this case, are their ways to guarantee the delivery of messages to consumers in topics, even if they disconnect?

    • >> For instance, if a consumer is connected to a topic, and suddenly disconnects, any message published after that will not be received by that consumer when it reconnects, correct?

      Exactly, yes.

      >> And in this case, are their ways to guarantee the delivery of messages to consumers in topics, even if they disconnect?

      Yes, use virtual topics. They will create a queue for each consumer behind the topic and will guarantee that the messages are delivered.

Webmentions

  • Virtual Topics in ActiveMQ – Thomas Uhrig September 23, 2020

    […] couple of days ago I published a post about the difference between queues, topics and virtual topics. Today I want to share some […]