RabbitMQ: consume messages in batches and ack them all at once
Codemia Knowledge Hub ·
To consume RabbitMQ messages in batches and acknowledge them together, fetch messages with manual acknowledgment, keep their delivery tags, and after the batch is processed call basic_ack with the highest delivery tag and multiple=True. That single ack confirms every message up to and including that tag.
How acknowledgments work
Producers send messages to a queue and consumers read them asynchronously. Normally each message is processed on its own and an acknowledgment (ack) tells RabbitMQ it was handled. If a consumer fails before acking, the message is redelivered, so nothing is lost.
Processing one message at a time suits many workloads, but in high-throughput systems it can become a bottleneck. With batching, the consumer pulls several messages, processes them together, and sends one acknowledgment for the whole batch. The benefits:
- Reduced Network Overhead: Less frequent acknowledgments reduce network traffic.
- Higher Throughput: More messages are processed in the same amount of time.
- Less Frequent Context Switching: Improves CPU usage efficiency in multi-threaded scenarios.
Batch consume and ack with pika
This simplified example uses pika, a Python RabbitMQ client, and assumes a queue that already has messages published to it. Install pika:
Then consume a batch:
basic_get fetches one message at a time with auto_ack = False. The script collects delivery tags until it has 10, then sends a single basic_ack. The multiple=True parameter means "acknowledge all messages up to and including the one with this delivery tag." Note that the loop keeps polling until it has a full batch, so on an empty queue it prints "No messages returned" repeatedly.
Choosing a batch size and handling failures
| Factor | Description | Impact |
| Batch size | Larger sizes reduce how often you ack, but a failure before the ack means more messages are redelivered and processed again. | Balance needed based on risk tolerance and throughput requirements. |
| Message processing time | Longer processing times might delay acks, affecting message redelivery times if unacknowledged messages are assumed lost. | Monitor and adjust batch sizes accordingly. |
| Error handling | Handling failures while processing batches can be complex as it may require re-consuming the entire batch. | Implement robust error recovery mechanisms. |
Batching pays off most when the work can be parallelized or is a bulk operation, such as bulk inserts or updates in a database where several messages need to succeed or fail together.
Related reading
- Rabbitmq consumer_timeout behavior not working as expected?
- RabbitMQ CreateConnection issues - works in one app but not in another
- RabbitMQ creating queues and bindings from command line
- RabbitMQ dead letter exchange never getting messages
- RabbitMQ Declare Exchange from Terminal - Access refused /api/exchanges/
- RabbitMQ def callback(ch, method, properties, body)
- RabbitMQ difference between exclusive and auto-delete?
- RabbitMQ dropping messages when no consumers are connected
System design interview problems on this topic
- Design a Distributed Messaging System: solution and walkthrough
- Design a Log Collection and Analysis System: solution and walkthrough
- Design an Ad Click Aggregation System: solution and walkthrough
- Design a Webhook Notification Service: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.