RabbitMQ -Connection vs Channel

Sakthi Saravanan
Jul 15 · 3 min read

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.

Steps to connect/send/receive (to/from) RabbitMQ instance

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.

Faun

The Must-Read Publication for Aspiring Developers & DevOps Enthusiasts

Sakthi Saravanan

Written by

Software Developer

Faun

Faun

The Must-Read Publication for Aspiring Developers & DevOps Enthusiasts

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade