Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rabbitmq connection best practices do we maintain persistent connection in the publisher

Generally, the best practices for SQL connection is to open the connection, execute the query and dispose the connection. However, what is the recommended practice for AMQP based queue servers like RabbitMQ. Does the application need to maintain a persistent connection to the RabbitMQ server or open and close the connection for each message being sent on the publisher side.

public static void Main(string[] args)
    {
        var factory = new ConnectionFactory() { HostName = "localhost" };
        using(var connection = factory.CreateConnection())
        using(var channel = connection.CreateModel())
        {
            channel.ExchangeDeclare(exchange: "logs", type: "fanout");

            var message = GetMessage(args);
            var body = Encoding.UTF8.GetBytes(message);
            channel.BasicPublish(exchange: "logs",
                                 routingKey: "",
                                 basicProperties: null,
                                 body: body);
            Console.WriteLine(" [x] Sent {0}", message);
        }

        Console.WriteLine(" Press [enter] to exit.");
        Console.ReadLine();
    }

    private static string GetMessage(string[] args)
    {
        return ((args.Length > 0)
               ? string.Join(" ", args)
               : "info: Hello World!");
    }
like image 287
TrustyCoder Avatar asked Aug 22 '16 04:08

TrustyCoder


1 Answers

in RMQ, connections are considered "expensive" - they take up a TCP/IP port, require handshake / negotiation, etc. While this may seem trivial in SQL Server land, when you're talking about sending 100K+ messages per second in RabbitMQ, that overhead becomes unworkable.

So, with RMQ, the general best practice is to open a single connection per application instance and keep it open as long as possible - for the life of the application instance, if you can.

Within the app instance, you create channels on top of the RMQ connection. And you can create these extremely fast. Most apps use a single channel for a single thing in RMQ. Message producer? open a channel. Consuming from a queue? Open a channel. Redefining a queue? Open a channel, etc.

Also - if you're working in a language that has threads, like C#, you must limit your channel to a single thread. Do not re-use channels across threads. Very bad things will happen if you try to do that.

like image 124
Derick Bailey Avatar answered Sep 28 '22 10:09

Derick Bailey