Shaping the Future of Ruby and Kafka Together with rdkafka-ruby
Tags: ruby, apm
Published: 2023-12-06 11:12:49

_Hello there! My name is Maciej Mensfeld, and some of you might recognize me from my involvement in RubyGems Security, OSS commitments, or perhaps from Karafka: a multi-threaded, efficient Kafka processing framework tailored for Ruby and Rails._

_While I generally pen my thoughts on my personal blog, today's post is unique. This article results from a collaborative effort with the brilliant people over at AppSignal. To set the record straight, I don't work for AppSignal. However, I felt that AppSignal's blog would be the most appropriate platform for this piece due to its nature._

## Karafka Embraces `rdkafka` Gem

We're happy to stand behind the [`rdkafka-ruby`](https://github.com/appsignal/rdkafka-ruby) gem, a brainchild of [AppSignal](https://www.appsignal.com/). Their incredible contributions have not only transformed the landscape of Ruby Kafka but have also played a pivotal role in the evolution of the [Karafka](https://github.com/karafka) ecosystem.

This library is a beacon of modernity in the [Kafka](https://kafka.apache.org/) space for Ruby. It integrates with [`librdkafka`](https://github.com/confluentinc/librdkafka), the production-ready C client using the `ffi` gem that provides outstanding performance and stability.

Following the sunset of [`ruby-kafka`](https://github.com/zendesk/ruby-kafka), it rapidly became the go-to low-level driver for Kafka interactions in Ruby. The migration of [Karafka](https://github.com/karafka/) and [Racecar](https://github.com/zendesk/racecar) (leading Ruby Kafka frameworks) to `rdkafka-ruby` further solidified its dominance.

## AppSignal's Use Case and the OSS Community

While AppSignal deploys `rdkafka-ruby` on a vast scale, it's essential to recognize that its use cases might only encompass some of the needs of the Ruby OSS community. This is especially true in realms like data consumption and crafting higher-level functionalities.

Recognizing these gaps and driven by my passion for the Karafka ecosystem and Kafka, I introduced a fork named [`karafka-rdkafka`](https://github.com/karafka/karafka-rdkafka) to expand this library further. However, the vision of having two autonomous gems, each with its developmental journey, raised concerns about the overarching benefit to the Ruby Kafka OSS community. It was this realization that led me to extend an "adoption offer" to AppSignal's talented development team, particularly to [Thijs](https://blog.appsignal.com/authors/thijs-cadier).

## Merging Visions for a Unified Future

Today, I'm pleased to share that our discussions bore fruit, carving a path that supports the collective interests of the Ruby and Kafka open-source community. The `rdkafka-ruby` gem will now thrive under the care of the Karafka community, with some guiding principles:

- **Openness**: `rdkafka-ruby` will eternally stand as a fully open, MIT-licensed primary binding for `librdkafka`.

- **Low-level Commitment**: While it offers direct APIs based on `librdkafka`, `rdkafka-ruby` will maintain its "layer 1" stature, focusing on low-level functionalities and leaving high-level details to libraries built on top of it.

- **Alignment**: `rdkafka-ruby` and `karafka-rdkafka` will harmonize their developmental paths. Unless `karafka-rdkafka` requires any framework-specific elements that would not be configurable, both will be merged into one.

- **Maintainability** - `rdkafka-ruby` will consistently support every official version of both Kafka and Ruby, ensuring smooth operations for its users.

Such a strategic shift promises stability and continued evolution to the entire ecosystem. Whether you're a `rdkafka-ruby` enthusiast valuing its flexibility or someone invested in high-level frameworks like Karafka and WaterDrop, you can rest assured that `rdkafka-ruby` remains the heartbeat of these platforms.

## `rdkafka-ruby` and Karafka Instrumentation with AppSignal

While transitioning the `rdkafka-ruby` ownership, feedback from AppSignal users highlighted another need. It became clear that an official instrumentation and monitoring solution for the Karafka ecosystem was essential. So, in response to this demand, I took steps to provide just that. This wasn't just about meeting a need; it was about fortifying the Karafka community with robust tools to ensure efficiency, stability, and clarity in their operations.

In the realm of Kafka with Ruby and Rails, the choice of monitoring tools is crucial. AppSignal emerges as the premier choice for Karafka applications instrumentation, and here's why:

- AppSignal offers insights into the performance, stability, health, and resource utilization of Karafka consumers, ensuring they operate optimally.

- It provides comprehensive error notifications for both consumers and producers, adeptly capturing asynchronous errors related to their operations.

- With official support from Karafka's creator, AppSignal's integration promises continuous updates and refinements, ensuring it remains the go-to tool for Karafka monitoring.

- For developers seeking simplicity, AppSignal integration is a breeze. It comes packaged with Karafka, eliminating the need for extra gems or tedious setups.

Choosing AppSignal isn't just about monitoring; it's about optimizing and ensuring the reliability of your Karafka and Kafka-based applications.

## The Importance of Monitoring Your Consumers and Producers

Monitoring producers and consumers is not just a 'good-to-have' but a foundational pillar of any robust Kafka-based system. Here's an in-depth look into why this is so essential:

### Ensuring Data Consistency and Integrity

- **Accurate Data Flow**: Kafka's architecture is fundamentally about ensuring that messages are transported from producers to consumers. Effective monitoring verifies that this data flow is accurate and consistent.

- **Detecting Anomalies**: In a distributed system, there's a possibility of message loss, duplication, or even out-of-order processing. Monitoring tools can promptly detect such issues, allowing for timely interventions.

- **Audit and Compliance**: For sectors where data integrity is not just an operational requirement but a legal one (like finance or healthcare), monitoring ensures that systems remain compliant with regulatory standards.

### Identifying and Resolving Performance Bottlenecks

- **Understanding System Dynamics**: Monitoring offers insights into the dynamics between producers and consumers. By visualizing message flow rates, system administrators can pinpoint areas of congestion or under-utilization.

- **Proactive Issue Resolution**: Imagine a scenario where a producer continuously pushes messages, but the consumer lags in processing them. This disparity could go unnoticed without monitoring until it snowballs into a significant issue. AppSignal highlights these discrepancies and provides actionable intelligence to resolve them.

- **Optimizing Resources**: Resources, be they computational or human, are finite. Through monitoring, you can prevent wastage and ensure the highest levels of efficiency.

### Enhancing System Reliability and Availability

- **Immediate Failure Detection**: Kafka systems are often the backbone of critical business operations. Any downtime or failure can lead to substantial financial and reputational losses. Monitoring tools act as vigilant guards, immediately detecting failures or disruptions.

- **Insights into System Health**: Beyond immediate failures, monitoring provides a continuous pulse check on the Karafka ecosystem's health. Whether tracking memory usage, gauging disk I/O, or measuring network latency, these insights ensure the system remains in its prime operational state.

- **Future-Proofing**: By observing trends and patterns over time, monitoring can aid in capacity planning, ensuring that the Kafka setup is reliable today and geared up for future demands.
  In essence, neglecting to monitor Kafka's consumers and producers is more than just an operational oversight; it's a missed opportunity to harness the true power and efficiency of the Karafka and Kafka ecosystem.

## Getting Started with Karafka and AppSignal

The beauty of [integrating Karafka with AppSignal](https://www.appsignal.com/ruby) lies in its simplicity. Thanks to the AppSignal integration being inherently part of the Karafka gem, there's no need for extra installations or gems. All the necessary components are pre-packaged in Karafka. Remember one key detail: when configuring, ensure the errors listener is subscribed before the metrics listener for optimal monitoring accuracy.

```ruby
# Require both listeners
require 'karafka/instrumentation/vendors/appsignal/errors_listener'
require 'karafka/instrumentation/vendors/appsignal/metrics_listener'

# Create them
errors_listener = ::Karafka::Instrumentation::Vendors::Appsignal::ErrorsListener.new
metric_listener = ::Karafka::Instrumentation::Vendors::Appsignal::MetricsListener.new

# Subscribe with the error listener first to track producers and consumers errors
Karafka.monitor.subscribe(errors_listener)
Karafka.producer.monitor.subscribe(errors_listener)
# And subscribe to Karafka monitor to get all the exciting metrics
Karafka.monitor.subscribe(metric_listener)
```

## Monitoring Karafka and Kafka with AppSignal

Here are some of the key metrics and insights offered by the Karafka-AppSignal integration (the following graphs are automatically created for you when you set up the integration):

### Consumption Metrics

Understanding consumers' and producers' health and behavior is foundational to any Karafka setup. With AppSignal, you can monitor aspects such as message processing times, batch sizes, error rates, and more. This data allows developers to ensure that consumers and producers operate at their peak efficiency.

![obraz](https://blog.appsignal.com/images/blog/2023-11/consumed-messages.png)

### Message Throughput and Lag

The core function of Kafka is the flow of messages. AppSignal offers insights into message throughput, clearly showing the volume of messages being processed. Moreover, tracking message lag is crucial; it highlights the time difference between when a message is produced and when it's consumed. High lags can be indicative of potential issues in the pipeline.

![obraz](https://blog.appsignal.com/images/blog/2023-11/lags.png)

### Performance Monitoring of Consumers

AppSignal allows you to monitor the performance of each of the consumers' jobs. Tracking performance provides invaluable insights, ensuring that each consumer operates efficiently and processes data at optimal speeds.

![obraz](https://blog.appsignal.com/images/blog/2023-11/error-breakdown.png)

### Error and Exception Tracking

AppSignal's robust error tracking system ensures that any anomalies, whether application-level or system-level, are immediately flagged. This not only helps in prompt rectification but also aids in understanding patterns that lead to such issues.

![obraz](https://blog.appsignal.com/images/blog/2023-11/errors.png)

## Wrapping Up

In summary, the Karafka AppSignal integration isn't just a monitoring tool; it's a magnifying glass into the intricate world of Karafka.

While the aspects we've covered above are some of the core metrics and insights available, the platform offers much more. Dive deeper to understand how to best optimize and enhance your Karafka and Kafka operations.

Happy coding!

**P.S. If you'd like to read Ruby Magic posts as soon as they get off the press, [subscribe to our Ruby Magic newsletter and never miss a single post](https://blog.appsignal.com/ruby-magic)!**