Crates.io | rdkafka |
lib.rs | rdkafka |
version | 0.37.0 |
source | src |
created_at | 2016-10-29 14:48:37.99122 |
updated_at | 2024-11-25 11:55:54.585839 |
description | Rust wrapper for librdkafka |
homepage | |
repository | https://github.com/fede1024/rust-rdkafka |
max_upload_size | |
id | 7038 |
size | 608,672 |
A fully asynchronous, futures-enabled Apache Kafka client library for Rust based on librdkafka.
rust-rdkafka
provides a safe Rust interface to librdkafka. This version
is compatible with librdkafka v1.9.2+.
The main features provided at the moment are:
rust-rdkafka
is designed to be easy and safe to use thanks to the
abstraction layer written in Rust, while at the same time being extremely
fast thanks to the librdkafka C library.
Here are some benchmark results using the BaseProducer
,
sending data to a single Kafka 0.11 process running in localhost (default
configuration, 3 partitions). Hardware: Dell laptop, with Intel Core
i7-4712HQ @ 2.30GHz.
Scenario: produce 5 million messages, 10 bytes each, wait for all of them to be acked
Scenario: produce 100000 messages, 10 KB each, wait for all of them to be acked
For more numbers, check out the kafka-benchmark project.
rust-rdkafka
provides low level and high level consumers and producers.
Low level:
BaseConsumer
: a simple wrapper around the librdkafka consumer. It
must be periodically poll()
ed in order to execute callbacks, rebalances
and to receive messages.BaseProducer
: a simple wrapper around the librdkafka producer. As in
the consumer case, the user must call poll()
periodically to execute
delivery callbacks.ThreadedProducer
: a BaseProducer
with a separate thread dedicated to
polling the producer.High level:
StreamConsumer
: a Stream
of messages that takes care of
polling the consumer automatically.FutureProducer
: a Future
that will be completed once
the message is delivered to Kafka (or failed).For more information about consumers and producers, refer to their module-level documentation.
Warning: the library is under active development and the APIs are likely to change.
Tokio is a platform for fast processing of asynchronous events in Rust.
The interfaces exposed by the StreamConsumer
and the FutureProducer
allow rust-rdkafka users to easily integrate Kafka consumers and producers
within the Tokio platform, and write asynchronous message processing code.
Note that rust-rdkafka can be used without Tokio.
To see rust-rdkafka in action with Tokio, check out the asynchronous processing example in the examples folder.
At-least-once delivery semantics are common in many streaming applications: every message is guaranteed to be processed at least once; in case of temporary failure, the message can be re-processed and/or re-delivered, but no message will be lost.
In order to implement at-least-once delivery the stream processing application has to carefully commit the offset only once the message has been processed. Committing the offset too early, instead, might cause message loss, since upon recovery the consumer will start from the next message, skipping the one where the failure occurred.
To see how to implement at-least-once delivery with rdkafka
, check out the
at-least-once delivery example in the examples folder. To know more about
delivery semantics, check the message delivery semantics chapter in the
Kafka documentation.
Exactly-once semantics (EOS) can be achieved using transactional producers,
which allow produced records and consumer offsets to be committed or aborted
atomically. Consumers that set their isolation.level
to read_committed
will only observe committed messages.
EOS is useful in read-process-write scenarios that require messages to be processed exactly once.
To learn more about using transactions in rust-rdkafka, see the Transactions section of the producer documentation.
Here are some of the projects using rust-rdkafka:
If you are using rust-rdkafka, please let us know!
Add this to your Cargo.toml
:
[dependencies]
rdkafka = { version = "0.25", features = ["cmake-build"] }
This crate will compile librdkafka from sources and link it statically to your executable. To compile librdkafka you'll need:
make
pthreads
zlib
: optional, but included by default (feature: libz
)cmake
: optional, not included by default (feature: cmake-build
)libssl-dev
: optional, not included by default (feature: ssl
)libsasl2-dev
: optional, not included by default (feature: gssapi
)libzstd-dev
: optional, not included by default (feature: zstd-pkg-config
)Note that using the CMake build system, via the cmake-build
feature, is
encouraged if you can take the dependency on CMake.
By default a submodule with the librdkafka sources pinned to a specific
commit will be used to compile and statically link the library. The
dynamic-linking
feature can be used to instead dynamically link rdkafka to
the system's version of librdkafka. Example:
[dependencies]
rdkafka = { version = "0.25", features = ["dynamic-linking"] }
For a full listing of features, consult the rdkafka-sys crate's documentation. All of rdkafka-sys features are re-exported as rdkafka features.
The current minimum supported Rust version (MSRV) is 1.70.0. Note that bumping the MSRV is not considered a breaking change. Any release of rust-rdkafka may bump the MSRV.
Some features of the StreamConsumer
and FutureProducer
depend on
Tokio, which can be a heavyweight dependency for users who only intend to
use the low-level consumers and producers. The Tokio integration is
enabled by default, but can be disabled by turning off default features:
[dependencies]
rdkafka = { version = "0.25", default-features = false }
If you would like to use an asynchronous runtime besides Tokio, you can
integrate it with rust-rdkafka by providing a shim that implements the
AsyncRuntime
trait. See the following examples for details:
You can find examples in the examples
folder. To run them:
cargo run --example <example_name> -- <example_args>
rust-rdkafka uses the log
crate to handle logging.
Optionally, enable the tracing
feature to emit tracing
events as opposed to log
records.
In test and examples, rust-rdkafka uses the env_logger
crate
to format logs. In those contexts, logging can be enabled
using the RUST_LOG
environment variable, for example:
RUST_LOG="librdkafka=trace,rdkafka::client=debug" cargo test
This will configure the logging level of librdkafka to trace, and the level
of the client module of the Rust client to debug. To actually receive logs
from librdkafka, you also have to set the debug
option in the producer or
consumer configuration (see librdkafka
configuration).
To enable debugging in your project, make sure you initialize the logger
with env_logger::init()
, or the equivalent for any log
-compatible
logging framework.
See rdkafka-sys.
Thanks to: