user982599
user982599

Reputation: 975

Is it better to have a connection stay open for RabbitMQ or only open when necessary?

I have an application that loops through a series of tests. On a failed test it publishes a message to a RabbitMQ queue that other applications monitor and pick up from. There are hundreds of tests and it can take several minutes to run through them all. The tests get repeated since the applications runs on a periodic basis.

My question is, would it better to open a connection once and keep it open for publishing only closing after looping through all tests? Or is it better to establish a connection only when I need to publish a message and close the connection once the message is sent?

Also, if my queue exists already will calling queue_declare again cause RabbitMQ/pika to try to recreate the queue or overwrite it?

This way:

message_con = pika.BlockingConnection(pika.URLParameters(app.conf['pika_url']))
channel = message_con.channel()
channel.queue_declare(queue='outage', durable=True, auto_delete=False, exclusive=False)

for test in tests:    
    # Do tests and stuff....
    if test_failed:
        channel.basic_publish(exchange='', routing_key='outage', body=json.dumps(message), properties=pika.BasicProperties(delivery_mode=2))

message_con.close()

Or this way:

for test in tests:
    # Do tests and stuff....
    if test_failed:
        message_con = pika.BlockingConnection(pika.URLParameters(app.conf['pika_url']))
        channel = message_con.channel()
        channel.queue_declare(queue='outage', durable=True, auto_delete=False, exclusive=False)
        channel.basic_publish(exchange='', routing_key='outage', body=json.dumps(message), properties=pika.BasicProperties(delivery_mode=2))
        message_con.close()

Upvotes: 2

Views: 2750

Answers (1)

bumblebee
bumblebee

Reputation: 1841

Client connections in RabbitMQ are meant to be long-lived. Channels are also long-lived, but any error/exception from the client may lead to the closure of a channel. Hence, the lifespan of a channel is shorter than a connection. Opening a connection per operation is highly discouraged. Opening a connection involves a lot of network operation and overhead. You can open more than one channel from a single connection.

Reference - https://www.rabbitmq.com/api-guide.html#connection-and-channel-lifspan

Upvotes: 1

Related Questions