AMQP Connections and Channels

Connections and channels enable services to communicate within the AMQ Protocol. As for any conversation, parties greet each other, exchange verbal banter, and eventually continue on their way. A similar form of communication occurs over low-level TCP connections exposing lightweight channels in LavinMQ.

What is a connection?

A connection (TCP) is a link between the client and the broker, that performs underlying networking tasks including initial authentication, IP resolution, and networking. LavinMQ connection start

What is a channel?

A channel acts as a virtual connection inside a TCP connection. LavinMQ channel

What benefits do connections and channels provide?

Every AMQP protocol-related operation occurs over a channel. A channel reuses a connection, forgoing the need to reauthorize and open a new TCP stream. Channels allow you to use resources more efficiently.

How should I use connections and channels?

Connections and channels are vital parts of services using AMQP. A service usually needs to have multiple connections communicating with the broker, but instead of having many connections, a service can reuse the connection by creating and deleting channels.

Connection

A connection (TCP/IP) is a link between the client and the broker, that performs underlying networking tasks including initial authentication, IP resolution, and networking.

LavinMQ channel

A connection is created by: 1. Opening a physical TCP connection to the target server. 2. The client resolves the hostname to one or more IP addresses. 3. The receiving server authenticates the client. 4. A connection is now established.

LavinMQ supports both IPv4 and IPv6 connections. Connections also have TLS (SSL) support meaning that it is possible to encrypt connections using TLS. In most clients it is easy to use TLS, just replace amqp:// with amqps:// in the URL.

Connections are needed before establishing a channel through a client, to be able to send a message or manage queues. Channels can not be used without the underlying TCP connection. Each connection can maintain a set of underlying channels.

Closing a connection closes all associated channels.

LavinMQ connection

Channel

A channel is needed for crucial communication with the broker. Every AMQP protocol-related operation occurs over a channel, like sending messages or handling queue creation and maintenance. AMQP provides the possibility for one single TCP/IP connection to multiplex into several “light-weight” channels. A channel operates inside a TCP connection.

The channel packages the messages and handles protocol operations. Channels are resource-efficient since they reuse existing channels and minimize the need for resource-heavy openings and closings of new TCP channels.

AMQP commands like declaring queues and declaring exchanges are all sent over a channel. Messages are sent from a client via the channel’s publish method. Remember, closing a connection closes all associated channels.

Code examples:

connection = pika.BlockingConnection(url_params) # Connect to LavinMQ

A channel can be opened right after successfully opening a connection.

channel = connection.channel() # start a channel

Important takeaways (best practice)

Discovering how to optimize the use of channels and connections in your infrastructure and understanding the benefits of reusing channels instead of opening new connections can impact the performance and resources needed to keep applications desirable configured.

Ready to take the next steps? Here are some things you should keep in mind:

Managed LavinMQ instance on CloudAMQP

LavinMQ has been built with performance and ease of use in mind - we've benchmarked a throughput of about 1,000,000 messages/sec. You can try LavinMQ without any installation hassle by creating a free instance on CloudAMQP. Signing up is a breeze.

Help and feedback

We welcome your feedback and are eager to address any questions you may have about this piece or using LavinMQ. Join our Slack channel to connect with us directly. You can also find LavinMQ on GitHub.