Batch Send
Send multiple KubeMQ Queue messages in a single batch with the C++ SDK to reduce round-trips and boost throughput.
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 std::vector<kubemq::QueueMessage> with kubemq::QueueMessage::Builder(), then passing the vector to client->SendQueueMessages(batch) in a single call. The broker enqueues each message independently and returns a vector of results — one per message, each with its own message_id and is_error — in the input's order.
Gotchas: batching isn't atomic — the broker can accept some messages and reject others in the same call, so always check is_error on every result rather than trusting the call as a whole; 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 - C++ SDK installed (vcpkg or CMake FetchContent)
- C++17 compiler (GCC 9+, Clang 9+, MSVC 2019+)
Code
// Example: queues/batch_send
//
// Demonstrates sending multiple queue messages in a single batch operation.
// Batch send reduces round trips for high-throughput scenarios.
//
// Channel: cpp-queues.batch-send
// Client ID: cpp-queues-batch-send-client
//
// Run with a KubeMQ server on localhost:50000
// (see https://docs.kubemq.io/deploy).
#include <kubemq/kubemq.h>
#include <iostream>
#include <string>
#include <vector>
int main() {
std::cout << "[1] Connecting to localhost:50000" << std::endl;
kubemq::ClientOptions options;
options.set_address("localhost", 50000);
options.set_client_id("cpp-queues-batch-send-client");
auto client_result = kubemq::Client::Create(options);
if (!client_result.ok()) {
std::cerr << "[ERROR] Failed to create client: " << client_result.status().message()
<< std::endl;
return 1;
}
auto& client = *client_result;
std::string channel = "cpp-queues.batch-send";
// Create a batch of queue messages.
std::cout << "[2] Building batch of 3 messages" << std::endl;
std::vector<kubemq::QueueMessage> batch;
for (int i = 1; i <= 3; i++) {
auto msg_result = kubemq::QueueMessage::Builder()
.SetChannel(channel)
.SetBody("batch-msg-" + std::to_string(i))
.Build();
if (!msg_result.ok()) {
std::cerr << "[ERROR] Build message " << i << ": " << msg_result.status().message()
<< std::endl;
return 1;
}
batch.push_back(std::move(*msg_result));
}
// Send all messages in a single batch operation.
std::cout << "[3] Sending batch" << std::endl;
auto results = client->SendQueueMessages(batch);
if (!results.ok()) {
std::cerr << "[ERROR] SendQueueMessages: " << results.status().message() << std::endl;
return 1;
}
for (size_t i = 0; i < results->size(); i++) {
std::cout << "[4] Batch[" << i << "]: id=" << (*results)[i].message_id
<< " error=" << std::boolalpha << (*results)[i].is_error << std::endl;
}
std::cout << "[5] Batch send complete" << std::endl;
// Close the client explicitly.
// Note: The Client destructor also calls Close(), but explicit
// cleanup is shown here for clarity and to match Go's defer pattern.
auto close_status = client->Close();
if (!close_status.ok()) {
std::cerr << "[ERROR] Close failed: " << close_status.message() << std::endl;
return 1;
}
std::cout << "[6] Client closed" << std::endl;
return 0;
}How It Works
- Builds multiple
QueueMessageinstances and sends them withSendQueueMessages(). - Returns a vector of results, one per message, with message IDs and status.
- Batch sending is more efficient than individual sends for bulk operations.
- Each message in the batch can target a different channel.
Related
Was this page helpful?