A production-ready RabbitMQ worker template written in Rust, built on Tokio and Lapin, supporting both single-message and batch-based consumer patterns.
- Async Rust using Tokio (multi-threaded runtime)
- RabbitMQ consumer using Lapin
- Two consumer modes:
- Single-message processing
- Batch processing with QoS (prefetch)
- Explicit ACK / NACK semantics
- Backpressure-safe via RabbitMQ QoS
- Header access and decoding
- Configurable via environment variables
- Optional Docker and docker-compose support
- Clean, minimal structure
.
├── src-single/ # Single-message consumer
│ ├── main.rs
│ └── rabbitmq.rs
│
├── src-batch/ # Batch consumer
│ ├── main.rs
│ └── rabbitmq.rs
│
├── Cargo.toml
├── Dockerfile
├── docker-compose.yaml
├── README.md
└── LICENSE
-
Use
src-singleif:- Each message is processed independently
- Processing is fast and simple
-
Use
src-batchif:- You want to process messages in groups
- You are writing to DBs, external APIs, or doing vectorized work
- You need batch-level ACK semantics
-
Rename the chosen directory to
src/to use it as the main source. And delete the other one if you want.
All configuration is done via environment variables.
RUST_LOG=rust_rmq_worker=TRACERABBITMQ_HOST=amqp.nekonik.com
RABBITMQ_PORT=5672
RABBITMQ_USERNAME=nekonik
RABBITMQ_PASSWORD=NekoNik
RABBITMQ_VHOST=nekonik_vhostRABBITMQ_CONSUMER_TAG=rust_consumer
RABBITMQ_QUEUE_NAME=nekonik_queue
RABBITMQ_QUEUE_DURABLE=trueRABBITMQ_PREFETCH_COUNT=10
RABBITMQ_PREFETCH_WINDOW_ms=200-
RABBITMQ_PREFETCH_COUNT- RabbitMQ QoS prefetch
- Maximum number of unacked messages
-
RABBITMQ_PREFETCH_WINDOW_ms- Batch collection window
- Controls how long the worker waits to fill a batch (in milliseconds)
After picking either src-single or src-batch as your source directory, and setting up environment variables, you can run the worker using simple Cargo commands.
cargo runenvironment:
# Logging
RUST_LOG: rust_rmq_worker=TRACE
# RabbitMQ
RABBITMQ_HOST: amqp.nekonik.com
RABBITMQ_PORT: 5672
RABBITMQ_USERNAME: nekonik
RABBITMQ_PASSWORD: NekoNik
RABBITMQ_VHOST: nekonik_vhost
# Consumer
RABBITMQ_CONSUMER_TAG: rust_consumer
RABBITMQ_QUEUE_NAME: nekonik_queue
RABBITMQ_QUEUE_DURABLE: true
# Batch only
RABBITMQ_PREFETCH_COUNT: 10
RABBITMQ_PREFETCH_WINDOW_ms: 200- Each message is ACKed after successful processing
-
Messages are:
- Collected up to
PREFETCH_COUNT - Processed as a batch
- ACKed after batch success
- Collected up to
-
If batch processing fails:
- Messages should be NACKed (optionally requeued)
Contributions are welcome! If you'd like to contribute to Rust-RabbitMQ-Worker Template, please follow these steps:
- Fork the repository
- Create a new branch for your feature or bug fix
- Make your changes and commit them
- Push your changes to your fork
- Submit a pull request to the
mainbranch of the original repository
Please make sure to follow the existing code style and add tests for any new features or bug fixes.
Rust-RabbitMQ-Worker Template is released under the MIT License. You are free to use, modify, and distribute this template for any purpose.