title: Consume messages in batches - RabbitMQ
body: <p>I was able to consume multiple messages that are sent by multiple producers to the same exchange with different routing key using the above code and was able to insert each message to database.</p>
  
  <p>But this will consume too much of resources as messages will be inserted into DB one after the other. So I decided to go for batch insert and I found I can set <code>BasicQos</code></p>
  
  <p>After setting the message limit to 10 in BasicQos, my expectation is the <code>Console.WriteLine</code> must write 10 messages, but it is not as expected.</p>
  
  <p>My expectation is to consume N number messages from the queue and do bulk insert and on successful send ACK else No ACK</p>
  
  <p>Here is the piece of code I use.</p>
  
  <pre><code>using (var connection = factory.CreateConnection())
  {
      using (var channel = connection.CreateModel())
      {
          channel.QueueBind(queue: "queueName", exchange: "exchangeName", routingKey: "Producer_A");
          channel.QueueBind(queue: "queueName", exchange: "exchangeName", routingKey: "Producer_B");
  
          channel.BasicQos(0, 10, false);
  
          var consumer = new EventingBasicConsumer(channel);
          channel.BasicConsume(queue: "queueName", noAck: false, consumer: consumer);
  
          consumer.Received += (model, ea) =&gt;
          {
              try
              {
                  var body = ea.Body;
                  var message = Encoding.UTF8.GetString(body);
  
                  // Insert into Database
  
                  channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
                  Console.WriteLine(" Recevier Ack  " + ea.DeliveryTag);
              }
              catch (Exception e)
              {
                  channel.BasicNack(deliveryTag: ea.DeliveryTag, multiple: false, requeue: true);
                  Console.WriteLine(" Recevier No Ack  " + ea.DeliveryTag);
              }
          };
  
          Console.ReadLine();
      }
  }
  </code></pre>