When working in integration, one of the most fundamental concepts to understand is the difference between topics and queues in a messaging system, such as JMS.
What do each of them do? And when would you choose to use a topic over a queue?
First let’s make sure you’re up to speed with messaging. There are usually three parties involved in messaging:
- Producer – this is a component that wants to send a message.
- Consumer – this is a component that receives the message.
- Message Broker – this is the bit of software in the middle (e.g. ActiveMQ) that handles the messages, kind of like the Post Office.
Of the two types of destinations, queues are the most commonly seen.
Queues are used in a scenario when one or more producers wants to send a message to a single consumer.
Think of your e-mail inbox. Anyone who knows your address can send you messages, and they will arrive in your inbox in order. Then you pick them up when you like, and you go through them one by one.
Queues work in a similar way. Queues are very powerful because if the consumer goes away, or disconnects from the queue for some reason, the broker won’t throw an error. Instead, the broker will safely keep every message sent to the queue. Then, when the consumer connects again, it will receive all of the messages while it was away.
This kind of integrating consumer and producer is sometimes called point-to-point integration, because a message is delivered from one place to another.
Topics are perhaps seen less often, but they are very useful.
Topics are used when one or more producers want to broadcast out a message to a bunch of consumers (known as subscribers).
The clever thing about topics is that a producer doesn’t need to worry about sending a message to every subscriber, or indeed who all the subscribers are. Instead, all of that is handled by the message broker.
Consumers can come to the message broker and say they’d like to subscribe to a topic. The broker knows who all the subscribers are, so when a message is published to the topic, it makes sure all the subscribers get a copy.
Topics are even more clever, because a consumer can choose whether or not they want the message broker to keep messages for them while they’re away. If a consumer registers itself as a durable subscriber with the broker, then any messages sent to the topic while the subscriber is away, will be sent to the consumer when they come back online.
Otherwise, if a consumer registers as a non-durable subscriber, they won’t receive messages that were sent to the topic while they were away.
When would you use a topic?
Topics come into their own when we have a message that we want to broadcast, but we don’t know who the consumers are. You might think that sounds quite unlikely, but actually has a lot of potential uses.
For example, you might have a topic that messages are published to, whenever a new member of staff joins your organisation. Then, you could have various applications that subscribe to that topic, such as your payroll application and your ID card application. These applications will each be notified about the new member of staff and can process his/her payroll, staff card, and so on.
The advantage of a topic is that you only need to publish the message once, and the broker takes care of distributing it to all the interested subscribers.
The key differences
I’m going to wrap up this post with a list of the key differences between queues and topics:
- Point-to-point style of integration
- A message is received by exactly one consumer
- Publish-subscribe style of integration
- A message is distributed to all subscribers of the topic
I hope you found this article useful. If you have anything to contribute I’d love to see your feedback in the comments below!