C# RabbitMQ.Client: reuse IModel for few EventingBasicConsumer

1,574 views
Skip to first unread message

Tkachenko Maxim

unread,
May 30, 2018, 4:04:24 AM5/30/18
to rabbitmq-users
I have 20 queues in rabbitmq instance. If I create IModel per queue consumer like this:

    for(int i = 0; i < 20; i++)
   
{
       
var model = _connection.CreateModel();
       
var cns = new EventingBasicConsumer(model);
       ch
.BasicConsume("queue_" + i, false, cns);
       
...
   
}

Application uses 37 threads. 

If I reuse the same IModel for all consumers:

    var model = _connection.CreateModel();
   
for(int i = 0; i < 20; i++)
   
{  
       
var cns = new EventingBasicConsumer(model);
       ch
.BasicConsume("queue_" + i, false, cns);
       
...
   
}

Application uses 18 threads. 

Is it a safe and good approach to decrease the number of application threads?

I count the number of threads using Process.GetCurrentProcess().Threads.Count

I checked threads in dottrace: in the 1st case there are 20 threads created by RabbitMQ.Client with name like WorkPool-Session#1:Connection(guid-here),amqp://1.1.1.1:5672 , in the 2nd case - only one such thread.

Michael Klishin

unread,
May 30, 2018, 7:56:14 AM5/30/18
to rabbitm...@googlegroups.com
Channels (IModel implementation instances) in most clients do not impact the number of threads used, however,
specifically in the .NET consumer dispatch services do use a thread per channel, see [1][2] for most relevant PRs.

You can use an async dispatcher which should use the standard runtime/TPL task scheduler by default.

Reusing a channel for consumer instances may be safe. The client does not support IModel instance reuse between threads
(something that has been discussed many times on this list before) but specifically for consumption only, and if your consumers
do not coordinate between each other, this may be safe in practice.

I'd look into using an async dispatcher [3] first.


--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
MK

Staff Software Engineer, Pivotal/RabbitMQ
Reply all
Reply to author
Forward
0 new messages