简体   繁体   中英

Improve performance of RabbitMQ publish (RabbitMQ C# client)

I'm using the following code to publish messages to a RabbitMQ queue:

ConnectionFactory factory = new ConnectionFactory {
    HostName = hostName,
    Port = port,
    UserName = userName,
    Password = password,
    VirtualHost = "/",
    Protocol = Protocols.DefaultProtocol
};
connection = factory.CreateConnection();
channel = connection.CreateModel();
channel.QueueDeclare(queue, true, false, false, null);
foreach (string message in messages) {
    byte[] body = Encoding.UTF8.GetBytes(message);
    channel.BasicPublish("", queue, null, body);
}

While publishing the messages to a local RabbitMQ server I get a message rate of up to 10,000 messages per second. The cpu load of the system (2x3.16 GHz) is at almost 100%. Is there a way to increase this message rate? My first idea was to use a bulk publish operation, but there doesn't seem to be anything like that in RabbitMQ. My second idea was using a Parallel.ForEach instead of the foreach, but that didn't change the message rate.

Defining a queue as "Durable" has the added benefit of surviving a rabbit or server restart. The downside is that to accomplish this it writes the data to disk, which is costly.

If your greatest concern is throughput and it's not a problem that you'd drop a few messages in the event of a failure than setting "Durable=false" would increase your throughput.

Note : Even with durable=false if the queue length becomes long enough it will dump them to disk (after all, there is only so much memory on the machine).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM