rust-rdkafka

crates.io docs.rs Build Status Join the chat at https://gitter.im/rust-rdkafka/Lobby

A fully asynchronous, [futures]-based Kafka client library for Rust based on [librdkafka].

The library

rust-rdkafka provides a safe Rust interface to librdkafka. It is currently based on librdkafka 0.9.5.

Documentation

Features

The main features provided at the moment are:

Client types

rust-rdkafka provides low level and high level consumers and producers. Low level:

High level:

Warning: the library is under active development and the APIs are likely to change.

Asynchronous data processing with tokio-rs

[tokio-rs] 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-rs platform, and write asynchronous message processing code. Note that rust-rdkafka can be used without tokio-rs.

To see rust-rdkafka in action with tokio-rs, check out the [asynchronous processing example] in the examples folder.

At-least-once delivery

At-least-once delivery semantic is 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.

Installation

Add this to your Cargo.toml:

toml [dependencies] rdkafka = "^0.11.0"

This crate will compile librdkafka from sources and link it statically to your executable. To compile librdkafka you'll need:

To enable ssl and sasl, use the features field in Cargo.toml. Example:

toml [dependencies.rdkafka] version = "^0.11.0" features = ["ssl", "sasl"]

Compiling from sources

To compile from sources, you'll have to update the submodule containing librdkafka:

bash git submodule update --init

and then compile using cargo, selecting the features that you want. Example:

bash cargo build --features "ssl sasl"

Examples

You can find examples in the examples folder. To run them:

bash cargo run --example <example_name> -- <example_args>

Tests

Unit tests

The unit tests can run without a Kafka broker present:

bash cargo test --lib

Automatic testing

rust-rdkafka contains a suite of tests which is automatically executed by travis in docker-compose. Given the interaction with C code that rust-rdkafka has to do, tests are executed in valgrind to check eventual memory errors and leaks.

To run the full suite using docker-compose:

bash ./test_suite.sh

To run locally, instead:

bash KAFKA_HOST="kafka_server:9092" cargo test

In this case there is a broker expected to be running on KAFKA_HOST. The broker must be configured with default partition number 3 and topic autocreation in order for the tests to succeed.

rdkafka-sys

See rdkafka-sys.

Contributors

Thanks to: * Thijs Cadier - thijsc

Alternatives