Skip to content

Producer Guide

Producers send messages to Danube topics. This guide covers creating producers, sending messages, partitions, and reliable dispatch.

Create a producer
use danube_client::DanubeClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = DanubeClient::builder()
.service_url("http://127.0.0.1:6650")
.build()
.await?;
let mut producer = client
.new_producer()
.with_topic("/default/my-topic")
.with_name("my-producer")
.build()?;
producer.create().await?;
Ok(())
}
  • Topic: destination for messages (e.g., /default/my-topic)
  • Name: unique producer identifier within the topic
  • Create: registers the producer with the broker before sending

Send a message
let message_id = producer
.send("Hello Danube!".as_bytes().to_vec(), None)
.await?;
println!("Sent message ID: {}", message_id);

Attach key-value metadata to messages for routing hints, custom headers, or tracing context:

Send with attributes
use std::collections::HashMap;
let mut attributes = HashMap::new();
attributes.insert("source".to_string(), "app-1".to_string());
attributes.insert("priority".to_string(), "high".to_string());
let message_id = producer
.send(b"Important message".to_vec(), Some(attributes))
.await?;

Partitions enable horizontal scaling by distributing messages across multiple brokers. Add .with_partitions(N) to the producer builder:

Partitioned producer
let mut producer = client
.new_producer()
.with_topic("/default/high-throughput")
.with_name("partitioned-producer")
.with_partitions(3)
.build()?;
producer.create().await?;

The topic splits into N internal partitions (e.g., -part-0, -part-1, -part-2). Messages are routed round-robin across partitions by default.


Reliable dispatch persists messages before acknowledging sends. Use it for critical events that must not be lost.

Reliable producer
let mut producer = client
.new_producer()
.with_topic("/default/critical-events")
.with_name("reliable-producer")
.with_reliable_dispatch()
.build()?;
producer.create().await?;

See Dispatch Strategies for details on reliable vs. non-reliable delivery.


Link producers to a schema subject for type-safe messaging:

Schema-linked producer
let mut producer = client
.new_producer()
.with_topic("/default/events")
.with_name("schema-producer")
.with_schema_subject("event-schema")
.build()?;
producer.create().await?;

For schema registration and versioning, see the Schema Registry guide.


Topics follow a namespace structure:

/namespace/topic-name

Examples:

  • /default/orders: orders in the default namespace
  • /production/user-events: user events in a production namespace
  • /staging/logs: logs in a staging namespace