Batch Send
Send multiple queue messages in a single batch operation
Overview
Batch send groups several queue messages into one call instead of sending them one at a time. Reach for it when publishing many related items together — importing records, fanning out a set of jobs, replaying a backlog — since sending each message individually pays a full round trip per message, while batching amortizes that cost across the whole set.
It works by building a Vec<QueueMessage> with QueueMessageBuilder, then passing the vector to client.send_queue_messages(messages) in a single gRPC call. The broker returns a Vec<QueueSendResult> in the input's order, one result per message, each carrying its own message_id and error.
Gotchas: validation happens up front — if any message in the batch fails validation, the whole batch is rejected, so a bad message can block delivery of otherwise-good ones; a batch is still one bounded request, so it doesn't help continuous, open-ended publishing (use a stream-based send for that); and very large batches raise the size and latency of that single call, so there's a practical ceiling before splitting into multiple batches pays off.
Prerequisites
- KubeMQ server running on
localhost:50000 - Rust SDK installed (
cargo add kubemq)
Code
use kubemq::prelude::*;
use kubemq::QueueMessageBuilder;
#[tokio::main]
async fn main() -> kubemq::Result<()> {
let client = KubemqClient::builder()
.host("localhost")
.port(50000)
.build()
.await?;
let channel = "rust-queues.batch-send";
let messages: Vec<QueueMessage> = (0..10)
.map(|i| {
QueueMessageBuilder::new()
.channel(channel)
.body(format!("batch-msg-{}", i).into_bytes())
.build()
})
.collect();
let results = client.send_queue_messages(messages).await?;
for r in &results {
println!("Sent: id={}, error={}", r.message_id, r.error);
}
println!("Batch sent {} messages", results.len());
client.close().await?;
Ok(())
}How It Works
send_queue_messagessends all messages in a single gRPC call.- Each message is validated independently; if any fails validation, the entire batch is rejected.
- Returns a
Vec<QueueSendResult>, one per message in the same order. - Review timeouts, channel names, and client IDs before running against shared environments.
- Run the program while the server from the prerequisites is available.
Related
Was this page helpful?