Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 37 ยท AMQP and Architecture

Connections and Channels

Understand why RabbitMQ has both connections and channels, how multiplexing works, what closes a channel, and how many of each you should open.

New RabbitMQ users often ask: why do I open a connection and then a channel? The answer is cost. A TCP connection (with TLS and authentication) is expensive to create and keep, but programs often need many independent streams of work. AMQP solves this with channels: lightweight virtual connections that share one real TCP connection.

The pika snippets are read-only examples for a real broker. The runnable blocks are Python stand-ins that illustrate multiplexing and error handling without any network.

One connection, many channels

flowchart LR
    subgraph App["Application process"]
      T1["Thread 1"] --- C1["Channel 1"]
      T2["Thread 2"] --- C2["Channel 2"]
      T3["Thread 3"] --- C3["Channel 3"]
    end
    C1 --- TCP(("one TCP connection"))
    C2 --- TCP
    C3 --- TCP
    TCP --- B["RabbitMQ"]

Each channel is an independent conversation; frames carry the channel number so the broker can tell them apart.

Every frame on the wire has a channel number (The AMQP 0-9-1 Model showed the format). The broker reads a stream of frames from one socket and uses the number to hand each to the right channel. All AMQP commands (queue.declare, basic.publish, basic.consume, ...) happen on a channel.

Here is the idea in code. The stand-in interleaves frames from three channels on one "wire" and the receiving side sorts them back out.

Python Multiplexing channels over one connection (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Opening them with pika

Python
# real pika code, read-only
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters("localhost"))

publish_channel = connection.channel()
consume_channel = connection.channel()   # a second, independent channel

print(publish_channel.channel_number, consume_channel.channel_number)  # e.g. 1 and 2

connection.close()

Closing the connection closes all of its channels. You can also close one channel on its own.

Rules of thumb

  • Few connections, more channels. Open one connection per application process (or per role, for example one for publishing and one for consuming) and as many channels as you need.
  • One channel per thread. Channels are generally not thread-safe in most clients. pika's BlockingConnection is not thread-safe at all: use it from one thread, or use separate connections per thread. In Java and .NET clients, sharing a connection and giving each thread its own channel is the standard pattern.
  • Do not open a channel per message. Opening and closing channels is cheaper than connections but still costs a round trip. Keep channels open for a long time.
  • Separate publishing and consuming. Publishing can be throttled by the broker when it is under memory pressure (the connection is blocked); a consumer sharing that connection would stall too. Using a different connection for consuming avoids that.
  • Do not leak. A program that opens a channel in a loop and never closes it eventually hits the channel limit and drains memory on both sides. A default limit of 2047 channels per connection is common, negotiated in connection.tune.

What closes a channel

Most mistakes are channel errors: they close only the channel, not the connection. Examples:

  • Declaring a queue that exists with different arguments (PRECONDITION_FAILED).
  • Consuming from or deleting a queue that does not exist (NOT_FOUND).
  • Using an exclusive queue owned by another connection (RESOURCE_LOCKED).
  • Acknowledging an unknown delivery tag.

After a channel error the channel object is dead; you must open a new one. Notably, a channel error also discards that channel's unacknowledged state: delivered but unacked messages go back to the queue. Connection errors (bad frame, authentication failure, too many bytes) close the whole connection.

The stand-in below mimics this isolation: one channel hits an error and dies while its neighbors carry on.

Python A channel error does not kill the connection (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Heartbeats, blocking and timeouts

A connection is long-lived, and silent failures (a cable pulled, a firewall dropping idle flows) are hard to notice. Two mechanisms help:

  • Heartbeats: each side promises to send some frame (or a heartbeat frame) at a regular interval. If two intervals pass in silence, the peer is declared dead and the connection is closed. The default is 60 seconds; some clients let you set heartbeat in connection parameters. Very long-running work in a single thread can miss heartbeats, which is a classic reason for random disconnects with a blocking Python client.
  • Connection blocked notifications: when the broker hits a memory or disk alarm it stops reading from publishing connections and tells clients with connection.blocked (and later connection.unblocked). Well-behaved publishers watch for these.

Recovering after a failure

Connections will drop: broker restart, network glitch, deployment. A robust client:

  1. Detects the failure (an exception, a close callback).
  2. Waits, with exponential backoff and jitter.
  3. Reconnects, reopens channels, redeclares its topology and re-registers consumers.

Many client libraries have an automatic recovery feature; others leave it to you. Decide deliberately, and test by restarting the broker while the app runs.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does AMQP have channels in addition to connections?

    Channels multiplex many sessions over one TCP connection, avoiding the cost of many sockets.
  2. 2. What is the usual recommendation for threads and channels?

    Channels are generally not thread-safe, so give each thread its own, usually over a shared connection.
  3. 3. What does redeclaring an existing queue with different arguments do?

    Queue arguments cannot be changed by redeclaring. The broker raises a channel error and closes that channel.
  4. 4. Why might a publisher see "connection blocked"?

    Under memory or disk pressure RabbitMQ stops reading from publishing connections and notifies them with connection.blocked.

Summary

  • A connection is one TCP link; channels are cheap virtual links multiplexed on it by channel number.
  • Keep connections few and long-lived, use one channel per thread, and separate publishing from consuming.
  • Channel errors close only the channel; build a new one and continue.
  • Heartbeats and blocked notifications keep dead or overloaded connections visible.
  • Reconnect with backoff and redeclare topology.

Next: Message Properties and Anatomy