KubeMQ
Client SDKsRubyHow-to guidesQueues

Batch Send

Send multiple KubeMQ queue messages in a single batch with the Ruby SDK to improve throughput and reduce round trips.

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 an array of KubeMQ::Queues::QueueMessage objects, then passing the array to client.send_queue_messages_batch(messages) in a single RPC call. The broker enqueues each message independently and returns one result per message — each exposing id and 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 error? on every result rather than trusting the batch 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
  • Ruby SDK installed (gem install kubemq)

Code

main.rb
require 'kubemq'

address = ENV.fetch('KUBEMQ_ADDRESS', 'localhost:50000')
channel = 'ruby-queues.batch-send'

begin
  client = KubeMQ::QueuesClient.new(address: address, client_id: 'qs-batch-example')
  puts "Connected to #{address}"

  messages = 5.times.map do |i|
    KubeMQ::Queues::QueueMessage.new(
      channel: channel,
      metadata: "batch-item-#{i}",
      body: "payload-#{i}"
    )
  end

  results = client.send_queue_messages_batch(messages)
  puts "Batch sent #{results.size} messages:"
  results.each_with_index do |r, i|
    puts "  [#{i}] id=#{r.id}, error?=#{r.error?}"
  end

  received = client.receive_queue_messages(channel: channel, max_messages: 10, wait_timeout_seconds: 5)
  puts "Received #{received.size} messages"
rescue KubeMQ::Error => e
  puts "KubeMQ error: #{e.message}"
ensure
  client&.close
  puts 'Done'
end

How It Works

  • send_queue_messages_batch sends an array of messages in a single RPC call.
  • Returns per-message results with id and error? for error checking.
  • Review timeouts, channel names, and client IDs before running against shared environments.

Was this page helpful?

On this page