Apache Kafka (image from Confluent site)

Apache Kafka is well known and widely used: this site reports many real world use cases. There are many client applications and libraries: here the full list. In this article I want to speak about KNet, because it uses a different approach to interact with Apache Kafka from .NET, it was firstly introduced in another article.

The approach

The most known client libraries are the one managed from Confluent: the libraries are wrappers around a common library written for C/C++ (librdkafka). The library is almost complete for most common purposes and I used it in many applications, however it rewrites the classes (e.g. producer, consumer, etc) and the communication protocol to connect to an Apache Kafka cluster; this implies that any change, improvement, bug fix on Apache Kafka implementation shall be applied to the client library, with extra coding cycles and tests to be done to validate it. This doubles, at least, the effort to have a stable solution.

Now I start to introduce to the reader KNet: contrary to libraries mentioned before, KNet does not rewrites again anything of Apache Kafka, instead it executes directly the Java code of the validated Apache Kafka distribution from .NET, rewriting the APIs only. So KNet has the same:

  • Kafka Improvement Proposals available in the underlying Apache Kafka distribution
  • API surface of the underlying Apache Kafka distribution
  • Cybersecurity
  • Documentation
  • Known bugs and limitations

What’s available?

With KNet everything is available in the official, JVM based, Apache Kafka distribution is available to .NET user too. An incomplete list is:

  • Producer and Consumer: classic APIs, plus KNet specific Producer and Consumer;
  • Apache Kafka Streams: the same APIs available in Java version; this functionality is not available with other products;
  • KNet Connect SDK: a framework, available only with KNet, to build connectors using .NET; without KNet, connectors shall be written using Java;
  • Full set of administrative APIs.

Beside the above APIs there are some tools and ready made containers:

  • KNetPS: a PowerShell module to interact with Apache Kafka using PowerShell;
  • KNetCLI: a command line interface to execute commands like you can do with the binary distribution; also available a ready made container;
  • KNetConnect: a command line interface to manage Apache Kafka Connect framework; also available a ready made container.

How to use KNet?

KNet offers multiple way to use Apache Kafka from .NET:

  • Classic: the approach follows exactly the same APIs used in Java
  • Specific: the approach adds new APIs more .NET aware

Classic

As stated in The approach chapter, KNet reproduces what’s available in the Apache Kafka distribution and you don’t need to learn other APIs. The same examples and code available on the net can be used from any .NET based application. Ad example this is the code that can be used to prepare producer configuration properties:

Properties props = new Properties(); props.Put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, serverToUse); props.Put(ProducerConfig.ACKS_CONFIG, "all"); props.Put(ProducerConfig.RETRIES_CONFIG, 0); props.Put(ProducerConfig.LINGER_MS_CONFIG, 1); props.Put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer"); props.Put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer"); 

Then the previous result can be used to create the Producer:

new KafkaProducer<string, string>(props); 

The previous code snippets are exactly the same of the one used in Java!

The same documentation available in Apache Kafka documentation can be used to identify the configuration properties and their meaning, the best API to use and so on.

One limitation with classic approach comes with the usable types. A KafkaProducer<TKey, TValue> can accept only primitive types (bool, byte, byte[], int, …) or types reflected from a concrete Java type: an user who wants to define its own structured type shall create it in Java and then reflect it in C#.

To solve the previous problem, and add many other features, KNet comes with some specific APIs.

Specific

Beside the classic approach, KNet comes with other features more comfortable for .NET users. Ad example the configuration properties can be built using the following approach:

Properties props = ProducerConfigBuilder.Create() .WithBootstrapServers(serverToUse) .WithAcks(ProducerConfig.Acks.All) .WithRetries(0) .WithLingerMs(1) .WithKeySerializerClass("org.apache.kafka.common.serialization.StringSerializer") .WithValueSerializerClass("org.apache.kafka.common.serialization.StringSerializer") .ToProperties(); 

As stated in What’s available? chapter, KNet offers other APIs and a Producer can be allocated using:

new KNetProducer<string, string>(props); 

To manage structured types defined in C# (without create them in Java) it is possible to define something like this:

var keySerDes = new JsonSerDes<MyKeyType>(); var valueSerDes = new JsonSerDes<MyValueType>(); new KNetProducer<MyKeyType, MyValueType>(props, keySerDes, valueSerDes); 

The previous code uses a JsonSerDes to manage serialization and deserialization of the user defined types MyKeyType and MyValueType.

Within the KNet suite there are multiple ready made serializer/deserializer which uses different technologies:

Other features are Apache Kafka Streams and KNet Connect SDK.

Performance

I spoke about the usage, however in real world applications one important element are the performances. Looking at the repository there are some tests which try to measure the performance and there is page reporting the performance results and the configuration parameters used.

Approach

The performances are measured using a compare approach and the reason is clear: too many conditions can impact the measures. The comparison is done against the most used .NET client library (Confluent.Kafka), the one managed from Confluent Inc.

The approach used try to minimize external side effects and concentrates only on the library comparison. So:

  • The communication, between client and server, is based on an high speed network
  • The data exchanged are array of bytes to avoid exogenous effects from serialization
  • There is no compression to avoid exogenous effects from different compression libraries
  • There is no ack and in-sync replica is set to 1 so the exogenous storage effects are reduced: records are dispatched to consumer without wait that the records are stored
  • The parameter used to setup producers and consumers of both libraries are the same where this is applicable
  • The effects from different threading models, and data enqueuing patterns, can impact the performances, but this is implicit because we are comparing them 😅

Benchmarks

The evaluation of the performances are done using two different kind of benchmarks:

  • Send-Receive: the benchmark first produces a set of records, then it consumes the same number of records previously produced. The measurements (produce and consume) are done alone on each direction varying the data length and the number of packets, the tests alternate between KNet and Confluent.Kafka; the measurements are repeated multiple times (with the same length and packets) to create a statistic set and reduce exogenous effects. Considering statistically distributed the impact of network and server, the benchmark measure the performance of the libraries. The final measures are reported as the ratio between the capability of KNet over Confluent.Kafka.
  • RoundTrip: this benchmark works in this way: while it produces records in main thread, it consumes them in an another thread to simulate a consumer in another process. Using a single process it is possible to synchronize, with the Operating System primitives, that the producer starts when the consumer is ready (it has received subscribe notification). The record value contains a byte array like in the previous benchmark, while the key of the record contains the machine ticks: when the record is produced the current ticks are stored as the record key, when the record is received the ticks in the record key are subtracted from current ticks: this is the measure of the time needed, from a ready consumer, to receive record from a producer, i.e. it contains the time (round trip) spent from the library, network and server. Considering statistically distributed the impact of network and server, the benchmark measure the performance of the libraries. The measurements are done varying the data length and the number of packets, the tests alternate between KNet and Confluent.Kafka; the measurements are repeated multiple times (with the same length and packets) to create a statistic set to reduce exogenous effects. The final measures are reported as the ratio between the capability of KNet over Confluent.Kafka.

Results

The results from previous tests, available in performance results page, are synthesized in:

  • Send-Receive: in this benchmark the test conditions impacts the result and it is not very clear which one (KNet or Confluent.Kafka) is the best choice;
  • RoundTrip: in this benchmark seems that KNet offers always the best performance, anyway the test results are few and maybe in other conditions Confluent.Kafka works better.

The configuration parameters used may impact on the results, because KNet and Confluent.Kafka uses different threading models and data enqueuing patterns, so it is not possible to take a final conclusion. Ad example, trying the same benchmarks on a cluster hosted in the cloud the performances of both libraries changes.

My personal consideration

In librdkafka repository (the library behind Confluent.Kafka) there is this sentence:

It was designed with message delivery reliability and high performance in mind, current figures exceed 1 million msgs/second for the producer and 3 million msgs/second for the consumer.

However I don’t find any reference on where and how the tests were done, I mean the information reported on performance results page which describes how the measures are done. When I used the library in some C++ projects the record exchanged varies from few bytes (90) to millions (2–3 Mb), however under heavy stress condition the limit reported above was never reached.

Conclusion

KNet, using the official Apache Kafka packages, offers the same APIs and, in general, the same performances of official Apache Kafka, plus shares the documentation and adds other features not available on .NET. Confluent.Kafka uses a different approach and it is based on librdkafka (C/C++ library): Confluent.Kafka is the reference library for the .NET developers.

KNet is aligned to latest KIPs and protocols, while Confluent.Kafka reports these limits (Kafka max is the one of KNet):

Both libraries give benefits and drawbacks, I’m not able to say which is the right choice: the reader knows its own project. What I can say is that the choice depends from many factors, some of them are:

  • Cybersecurity
  • Long and short term compatibility
  • Long term maintainability

Now the reader knows KNet too, I leave the choice to him 😜.