Note
The rdkafka-ruby
gem was created and developed by AppSignal. Their impactful contributions have significantly shaped the Ruby Kafka and Karafka ecosystems. For robust monitoring, we highly recommend AppSignal.
The rdkafka
gem is a modern Kafka client library for Ruby based on
librdkafka.
It wraps the production-ready C client using the ffi
gem and targets Kafka 1.0+ and Ruby versions that are under security or
active maintenance. We remove Ruby version from our CI builds if they
become EOL.
rdkafka
was written because of the need for a reliable Ruby client for Kafka that supports modern Kafka at AppSignal. AppSignal runs it in production on very high-traffic systems.
The most important pieces of a Kafka client are implemented. We're working towards feature completeness, you can track that here: https://github.com/appsignal/rdkafka-ruby/milestone/1
This gem downloads and compiles librdkafka when it is installed. If you have any problems installing the gem please open an issue.
See the documentation for full details on how to use this gem. Two quick examples:
Subscribe to a topic and get messages. Kafka will automatically spread the available partitions over consumers with the same group id.
config = {
:"bootstrap.servers" => "localhost:9092",
:"group.id" => "ruby-test"
}
consumer = Rdkafka::Config.new(config).consumer
consumer.subscribe("ruby-test-topic")
consumer.each do |message|
puts "Message received: #{message}"
end
Produce a number of messages, put the delivery handles in an array and wait for them before exiting. This way the messages will be batched and sent to Kafka in an efficient way.
config = {:"bootstrap.servers" => "localhost:9092"}
producer = Rdkafka::Config.new(config).producer
delivery_handles = []
100.times do |i|
puts "Producing message #{i}"
delivery_handles << producer.produce(
topic: "ruby-test-topic",
payload: "Payload #{i}",
key: "Key #{i}"
)
end
delivery_handles.each(&:wait)
Note that creating a producer consumes some resources that will not be
released until it #close
is explicitly called, so be sure to call
Config#producer
only as necessary.
Currently, there are two actively developed frameworks based on rdkafka-ruby, that provide higher level API that can be used to work with Kafka messages and one library for publishing messages.
- Karafka - Ruby and Rails efficient Kafka processing framework.
- Racecar - A simple framework for Kafka consumers in Ruby
- WaterDrop – Standalone Karafka library for producing Kafka messages.
A Docker Compose file is included to run Kafka and Zookeeper. To run that:
docker-compose up
Run bundle
and cd ext && bundle exec rake && cd ..
to download and
compile librdkafka
.
You can then run bundle exec rspec
to run the tests. To see rdkafka
debug output:
DEBUG_PRODUCER=true bundle exec rspec
DEBUG_CONSUMER=true bundle exec rspec
After running the tests you can bring the cluster down to start with a clean slate:
docker-compose down
To see everything working run these in separate tabs:
bundle exec rake consume_messages
bundle exec rake produce_messages