To publish/consume a message from RabbitMQ: Firstly, we need to connect to the RabbitMQ instance. This is a TCP based connection. Secondly, we need to have a virtual connection (channel) inside the TCP connection. Thirdly, we need to declare an exchange and queues, then bind them. Finally, messages can be send and receive over the channel. Below picture shows steps involved to send/receive a message from RabbitMQ.
What? Why TCP connection is not sufficient? Why do we need the channel? Let’s say, you don’t have a channel. In multi threading architecture you may need a separate connection per thread. That may leads to under utilisation of TCP connection, also it adds overhead to the operating system to establish as many TCP connection it requires during peak time of the network. Performance of the system could be drastically reduce. This is where the channel comes handy, it creates virtual connections inside a TCP connection. It straightaway reduces overhead of the OS, also it allows us to perform asynchronous operations in more faster, reliable and simultaneously way.
How channels help us to perform operations in parallel. Assume we have two apps ‘A’ and ‘B’ wants to talk to each other through a RabbitMQ instance with one TCP connection with one channel as shown in below figure. Let’s say, an app ‘A’ listening on a queue where it expects some messages from the app ‘B’, once it receives and it would start processing it. What if an app ‘A’ wants to send message to the app ‘B’ before acknowledging the current operation. No way, it has to wait until processing finishes for the previous transaction made in that channel.
We could improve the speed of execution by creating another channel (there is no limit on the count) as shown in below figure. So an app ‘A’ will consume message from the queue, at the same time it can publish a message too! in different channels.
This is how connections and channels are related in RabbitMQ. Hope it gives some thoughts on differences between the connection and the channel.
“A good teacher can inspire hope, ignite the imagination, and instills a love of learning.”-Brad Henry.
Thanks Manu Mathews, You’re one of a kind to me.