Expand description
Apache Iggy is a high-performance, persistent message streaming platform written in
Rust, capable of processing millions of messages per second with ultra-low latency.
It is part of the Apache Software Foundation (ASF).
This library is the Apache Iggy SDK.
It exposes a low-level and a high-level API for the Apache Iggy message streaming
infrastructure for the Rust programming language.
SDKs for other programming languages can be found in foreign of the root
repository on GitHub.
The core of Iggy is the message streaming server.
In essence it is a persisted append-only log data structure concerned with making
reads and writes highly efficient.
For that, the server exposes commands that can be triggered to change its state,
such as adding users, setting permissions, adding new streams and topics or reading
and writing messages to and from the log.
A comprehensive overview of commands can be found in the schema spec on the
website, or in the server command enum within the source code.
The SDK provides tools to build production ready message-streaming applications. It exposes its functionality at two levels. The high-level API is transport-agnostic and already ships with useful features that a production application needs, such as message batching, retry policies and offset-tracking. The low-level API is the set of concrete transport clients that speak the wire protocol directly and that the high-level API is built on top of. It is recommended to start with the high-level API, and utilize the low-level API in case the high-level API cannot satisfy your requirements.
§High-level API
The high-level API is most likely what you are looking for, especially if you are new to building message-streaming applications with Iggy. High-level API clients already provide common message-streaming features that you would otherwise need to build yourself.
There are three client types:
IggyClientis the entry point and the full API surface. It owns the connection and implements every domain trait, includingMessageClientwith the rawsend_messagesandpoll_messagesprimitives. For both, each call ignores producer and consumer level policies, i.e. there is no batching, retries, offset tracking, or polling loop.IggyProducerexposes all configuration and functionality to produce (send) messages to a specific topic in a stream. It shares the connection from theIggyClient.IggyConsumerexposes all configuration and functionality to consume (read) messages from a specific topic and stream. It also shares the connection from theIggyClient.
You do not construct the producer and consumer independently. Spawn their builders
from an IggyClient with IggyClient::producer and
IggyClient::consumer so they share its connection.
§When to use each
Reach for IggyClient directly for administrative tasks such as
creating streams, topics, users, and consumer groups, reading or storing
offsets, or sending and polling a handful of messages in a script.
Reach for IggyProducer and IggyConsumer when producing and consuming messages.
The IggyProducer has two modes you can pick from when building it:
direct (DirectConfig), where a send goes out on the calling task, or
background (BackgroundConfig), where a send is buffered and sending
is offloaded to worker tasks.
Additionally it provides the following features:
- Retries with a configurable count and interval.
- A pluggable partitioning strategy (
Partitioner). - Encryption of message payloads before they leave the process.
- Auto-creation of the stream and topic if they do not exist yet.
Only in direct mode:
- Chunking splits an input larger than
batch_lengthinto several requests. - Spacing applies
linger_timebetween consecutive sends.
Only in background mode:
- Batching collects until the batch size in bytes, the number of sends, or the linger interval is reached, whichever comes first.
- Shard workers run several send loops in parallel (
num_shards), which helps when one producer writes to several streams or topics. - A sharding strategy (
Sharding) decides which worker a batch goes to:OrderedShardingkeeps messages for the same stream and topic in order,BalancedShardingspreads them round-robin for throughput. - Backpressure bounds the bytes buffered across all workers and the number
of in-flight batches. With a
BackpressureModeyou can decide whether a full buffer blocks, blocks with a timeout, or fails immediately. - Ordering control through
max_in_flight. - An error callback (
ErrorCallback) receives the messages a background send could not deliver, together with the confirmations that did commit. - Graceful shutdown flushes what is still buffered, which dropping the producer does not.
The IggyConsumer has:
- A
futures::Streamimplementation, so awhile let Some(message) = consumer.next().awaitloop drives the reads, the prefetch, and the retries of aPartitionReaderfor you. - A start (
PollingStrategy:next,offset,first,last, ortimestamp) for each partition without a stored offset. - Commits: the
CommitPolicyof the poll options. WithManual,commitstores the checkpoint of a yielded message. WithAuto, the consumer stores the latest message that counted, after it was processed, delivered or marked, on an interval or after each message, every n messages or each batch. WithCommitPolicy::OnRead, the server stores the offset of each read instead. A restart resumes from the last commit. - Manual offset control to store, read, or delete the offset of any partition yourself.
- A shared state handle (
IggyConsumerState) that another task can clone to read offsets or commit while the consuming loop holds the consumer. - Replay control drops messages this consumer already consumed, unless you
opt into replaying them with
allow_replay. - Consumer group membership:
init()joins the group, optionally creating it, the consumer joins again after a session loss, andshutdown()leaves it. - Reconnection handling pauses polling while the client is disconnected and resumes it after the reconnect and the rejoin have gone through.
- Init retries wait for the stream and topic to appear instead of failing right away when the consumer starts before they exist.
- Payload decryption.
- Graceful shutdown commits the last yielded positions under a client
commit policy, moves the offsets of
OnReadback to them, and leaves the consumer group.
For details on the specific behavior of each feature, reach for the type-level documentation.
§Stream builder API
The stream builder API is a convenient way to use the high-level API.
IggyStream, IggyStreamProducer, and IggyStreamConsumer construct
everything at once. You can pass an IggyClient (or just a connection string)
together with a config, and they hand back a ready, connected
IggyProducer / IggyConsumer.
Compared to the high-level API, it changes how you construct
producers and consumers, not what they can do. Instead of chaining an
IggyProducerBuilder / IggyConsumerBuilder and setting each option
with a method call, you describe the whole setup once in an
IggyStreamConfig (or in a single IggyProducerConfig /
IggyConsumerConfig when you only need one side) and build from it.
However, both provide a subset of available configurations only.
If you need full control use the builders instead.
§Low-level API
The low-level API is the set of concrete transport clients: TcpClient,
QuicClient, WebSocketClient, and HttpClient. Each one implements
Client, the supertrait that pulls in every domain-specific trait, so a
transport client on its own can already drive the full server API. The
high-level IggyClient is one more layer over exactly these types.
§Differences to the high-level API
- Transport is fixed at compile time. You name a concrete type
(
TcpClient,QuicClient, and so on) instead of configuring a transport-agnosticIggyClient. Swapping transports means swapping the type, not changing a connection-string scheme. - No producer or consumer helpers.
IggyProducerandIggyConsumerare spawned from anIggyClient, so a raw transport client gives you no background batching, retries, polling loop, commit policies, consumer-group membership, or payload encryption. You get the request-response primitives (send_messages,poll_messages) and nothing layered on top. - Raw wire access.
BinaryTransport::send_raw_with_responsesends an arbitrary command code and payload and returns the raw response bytes. The high-level equivalents areIggyClient::send_binary_requestandIggyClient::send_http_request. Either way you need to know the server command codes and the wire format.
§When to use it
Prefer the high-level API. Reach for the low-level API only when you need one
of the things it exposes that IggyClient deliberately hides:
- You want to own the connection lifecycle yourself, with custom pooling,
supervision, or a different heartbeat strategy, rather than let
IggyClientmanage it. - You are building your own abstraction on top of the SDK, for example a different producer or consumer, and want the primitives.
- You forked the server and need to issue a command the typed API does not recognize
and want the raw
send_raw_with_responseinstruction.
If none of these apply, the high-level API gives you the same reach with far less to get wrong.
§Async runtime
The SDK is async and runs on the Tokio runtime. Note that this is a hard
requirement and not optional. The SDK uses quinn (for QUIC), reqwest (for HTTP),
tokio-tungstenite (for WebSocket) and tokio-rustls (for TLS) which all build on
Tokio.
The SDK also spawns its own background work with tokio::spawn (the
IggyClient::connect heartbeat, and the IggyProducer and
IggyConsumer tasks) and drives timeouts, retries, and commit intervals with
tokio::time.
Note that dropping down to the low-level transport clients does not change this.
Thus, everything you do with the Rust SDK must happen inside a Tokio runtime.
use iggy::prelude::*;
use futures_util::StreamExt;
use std::error::Error;
use std::str::FromStr;
// `#[tokio::main]` starts the runtime the SDK requires.
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let client = IggyClient::from_connection_string(
"iggy://iggy:iggy@localhost:8090",
)?;
client.connect().await?;
let producer = client.producer("stream_name", "topic_name")?
.topic_durability(iggy::prelude::Durability::Persisted).build();
producer.init().await?;
producer
.send(vec![IggyMessage::from_str("some_message_payload")?])
.await?;
let mut consumer = client
.consumer("consumer_name", "stream_name", "topic_name")?
.partitions([1])
.build();
consumer.init().await?;
while let Some(message) = consumer.next().await {
let _message = message?;
break;
}
client.shutdown().await?;
Ok(())
}Modules§
- binary
- client_
provider - client_
wrappers - clients
- consumer_
ext - http
- prelude
- Prelude module for the Iggy SDK.
- quic
- session
- A logical session keeps its identity, bind secret, and request counter across transport reconnects. Registration retries recover the original committed session. A session reset, such as an explicit logout, starts a new identity and carries the request counter over, so a client never reuses a request id.
- stream_
builder - tcp
- websocket