RabbitMQ

RabbitMQ Topic vs Direct exchange

Nastel Technologies®
June 14, 2022

1. Overview

In this article, we will learn the differences between RabbitMQ Topic vs Direct exchange. To learn more about other RabbitMQ topics, refer to these articles.

 

2. RabbitMQ Topic vs Direct Exchange

 

Direct Topic
A direct exchange delivers messages to queues based on the message routing key. Topic exchanges route messages to one or many queues based on matching between a message routing key and the pattern that was
used to bind a queue to an exchange.
Can’t behave like other exchanges Can behave like other exchanges
Does not support # or * wildcard characters Supports special characters “*” and “#”
Can’t behave like a topic exchange When special characters “*” (star) and “#” (hash) aren’t used in bindings, the topic exchange will behave just like a direct one.
You can bind multiple queues with the same binding key.
In that case, the direct exchange will behave like fanout and will broadcast the message to all the matching queues.
When a queue is bound with “#” (hash) binding key – it will receive all the messages, regardless of the routing key – like in fanout exchange.
It can’t do routing based on multiple criteria. It can route based on multiple criteria
RabbitMQ Direct vs Topic exhange

See this article to understand the terminologies Queue, Exchange, Routing and binding keys.

 

3. RabbitMQ Direct exchange

In direct exchange, a message goes to the queues whose binding key exactly matches the routing key of the message.

A direct exchange is ideal for the Unicast routing (one-to-one) of messages (although we can use it for multi cast – one-to-many routing as well).

 

Here is how it works:

  • A queue binds to the exchange with a routing key K
  • When a new message with routing key R arrives at the direct exchange, the exchange routes it to the queue if K = R

 

3.1. Work queues

We can use the Work Queue to distribute time-consuming tasks among multiple workers or consumers.

In this way, you can avoid doing a resource-intensive task immediately and having to wait for it to complete. Instead, we schedule the task to be done later. We encapsulate a task as a message and send it to a queue. A worker process running in the background will pop the tasks and eventually execute the job.

 

This concept is especially useful in web applications, where it’s impossible to handle a complex task during a short HTTP request window.

 

The RabbitMQ can share the tasks with multiple workers.

 

3.1.1. Direct exchange with work queues

Direct exchanges are often used to distribute tasks between multiple workers or consumers (instances of the same application) in a round-robin manner.

 

It load balance the messages between consumers and not between queues.

 

The RabbitMQ will send each message to the next consumer, in sequence. On average, every consumer will get the same number of messages. This way of distributing messages is called round-robin. 

 

Look at the following block. We have two workers listening for messages. RabbitMQ delivers each message sequentially. Worker 1 gets the first message and then worker 2 gets the second message. Again, worker 1 gets the third message and worker 2 gets the fourth message. The same process repeats.

 

This article originally appeared on tedblob.com, to read the full article, click here.

Nastel Technologies is the global leader in Integration Infrastructure Management (i2M). It helps companies achieve flawless delivery of digital services powered by integration infrastructure by delivering Middleware Management, Monitoring, Tracking, and Analytics to detect anomalies, accelerate decisions, and enable customers to constantly innovate, to answer business-centric questions, and provide actionable guidance for decision-makers. It is particularly focused on IBM MQ, Apache Kafka, Solace, TIBCO EMS, ACE/IIB and also supports RabbitMQ, ActiveMQ, Blockchain, IOT, DataPower, MFT and many more.

 

The Nastel i2M Platform provides:

  • Secure self-service configuration management with auditing for governance & compliance
  • Message management for Application Development, Test, & Support
  • Real-time performance monitoring, alerting, and remediation
  • Business transaction tracking and IT message tracing
  • AIOps and APM
  • Automation for CI/CD DevOps
  • Analytics for root cause analysis & Management Information (MI)
  • Integration with ITSM/SIEM solutions including ServiceNow, Splunk, & AppDynamics

Comments

Write a comment
Leave a Reply
Your email address will not be published. Required fields are marked *
Comment * This field is required!
First name * This field is required!
Email * Please, enter valid email address!
Website

Register to Download

Subscribe

Schedule a Meeting to Learn More

Become an Expert

Schedule a Demo