This is a continuation of a previous article wrote on KEFCore. This article speaks about new improvements of KEFCore and try to analyze the performances compared to other providers.
New features and improvements
Before describe the performances I introduce the new improvements. The latest version of KEFCore comes with some improvements in these areas:
- A new data model to store data into the topics
- A new serialization approach
- KNetCompactedReplicator engine, beside Apache Kafka Streams, to store and retrieve data from the Apache Kafka cluster
Let’s start to describe them.
Data model
In the previous article the data stored in the topics were more or less raw info and the types depends from the .NET type written in the data model. Without the source code it was not possible to understand the corresponding type for each data in the array:
{ "typeName": "MASES.EntityFrameworkCore.KNet.Test.Blog", "data": [ 1, 0, "http://blogs.msdn.com/adonet0" ] } The new data model contains more information:
- both the index and the name of the property, so it is possible to reassign back, based on position or name, the value to the Entity of the defined model
- the type associated to the corresponding value, so it is possible to read the content of the data and report it back to the model; or analyze it with other tools without the knowledge of the original .NET type
An example is like the following one:
{ "EntityName": "MASES.EntityFrameworkCore.KNet.Test.Blog", "ClrType": "MASES.EntityFrameworkCore.KNet.Test.Blog", "Data": { "0": { "PropertyName": "BlogId", "ClrType": "System.Int32", "Value": 8 }, "1": { "PropertyName": "Rating", "ClrType": "System.Int32", "Value": 7 }, "2": { "PropertyName": "Url", "ClrType": "System.String", "Value": "http://blogs.msdn.com/adonet7" } } } Serialization approach
Contrary to the previous version, where data on topic was always a string (simple primary key type like integer were converted into string), the new approach consider the type and use, when available, the standard serializers of Apache Kafka:
- e.g. integer are reported in their raw format and operations done from other tools can use directly that value if needed
- structured values (both complex primary keys or the entity value itself) are managed using the JSON serialization available in .NET 6.
KNetCompactedReplicator engine
As concluded in previous article, KNet comes with some features over the standard one of Apache Kafka clients. KEFCore now supports KNetCompactedReplicator to manage data store and retrieve. Currently Apache Kafka Streams is used in its most simple way: more or less it reports the data available within the topic. This is the same that KNetCompactedReplicator does.
Performance
I briefly introduced the most important improvements made on KEFCore, however without a performance analysis they remains only words.
The performances of any Entity Framework Core provider are impacted from two different elements:
- Each provider connects to a back-end: better is the communication with the back-end better are the performances on retrieve or store data;
- When data are received from the back-end: better is the conversion to the EFCore Entity, or data enumeration, better are the performances to execute the EFCore program.
To measure the performances I need a reference since this kind of tests can be done only in comparison mode.
About point 1: it is complex to make a right comparison test because the back-end can introduce a great impact and any back-end has its own characteristics.
About point 2: if I consider that on KEFCore both Apache Kafka Streams and KNetCompactedReplicator approaches stores an in-memory local copy of the data, the comparison can be done against the EFCore InMemory provider: this provider is used to test and debug the model, it does not need any database engine and it works perfectly locally.
Approach
First of all I cloned the KEFCore repository at its head version.
The approach used to make the test is the following:
- Create a
KafkaDbContextwill manage the provider under test: overridingOnConfiguringofKafkaDbContextit is possible to choose another provider to use like the InMemory provider - Cleanup and recreate the back-end using
EnsureDeleted/EnsureCreated - Create a set of data and store them in the back-end using Entity Framework Core syntax and dispose the
KafkaDbContext - Create the same
KafkaDbContextof step 1 - Invoke a query which will retrieve the data from the back-end: the elapsed time is the sum of time needed to retrieve the data from back-end (using Apache Kafka the provider communicates with the Apache Kafka cluster) and the execution time of the query itself
- Invoke a second time the same query: the elapsed time is only the execution time of the query itself since the data are available locally
- Execute step 3 and 4 using different queries
- Dispose the
KafkaDbContextto cleanup everything and return to step
Model and queries
The model used in the test is the same of the previous article:
public class BloggingContext : KafkaDbContext { public DbSet<Blog> Blogs { get; set; } public DbSet<Post> Posts { get; set; } } public class Blog { public int BlogId { get; set; } public string Url { get; set; } public long Rating { get; set; } public List<Post> Posts { get; set; } } public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public int BlogId { get; set; } public Blog Blog { get; set; } } The code snippet with the queries is the following:
Stopwatch watch = new Stopwatch(); watch.Restart(); var post = context.Posts.Single(b => b.BlogId == 2); watch.Stop(); ReportString($"First execution of context.Posts.Single(b => b.BlogId == 2) takes {watch.Elapsed}. Result is {post}"); watch.Restart(); post = context.Posts.Single(b => b.BlogId == 2); watch.Stop(); ReportString($"Second execution of context.Posts.Single(b => b.BlogId == 2) takes {watch.Elapsed}. Result is {post}"); watch.Restart(); post = context.Posts.Single(b => b.BlogId == config.NumberOfElements - 1); watch.Stop(); ReportString($"Execution of context.Posts.Single(b => b.BlogId == {config.NumberOfElements - 1}) takes {watch.Elapsed}. Result is {post}"); watch.Restart(); var all = context.Posts.All((o) => true); watch.Stop(); ReportString($"Execution of context.Posts.All((o) => true) takes {watch.Elapsed}. Result is {all}"); Blog blog = null; watch.Restart(); blog = context.Blogs!.Single(b => b.BlogId == 1); watch.Stop(); ReportString($"First execution of context.Blogs!.Single(b => b.BlogId == 1) takes {watch.Elapsed}. Result is {blog}"); watch.Restart(); blog = context.Blogs!.Single(b => b.BlogId == 1); watch.Stop(); ReportString($"Second execution of context.Blogs!.Single(b => b.BlogId == 1) takes {watch.Elapsed}. Result is {blog}"); watch.Restart(); var result = (from op in context.Blogs join pg in context.Posts on op.BlogId equals pg.BlogId where pg.BlogId == op.BlogId select new { pg, op }).ToList(); watch.Stop(); ReportString($"Execution of first complex query takes {watch.Elapsed}. Result is {result.Count} element{(result.Count == 1 ? string.Empty : "s")}"); watch.Restart(); var result2 = (from op in context.Blogs join pg in context.Posts on op.BlogId equals pg.BlogId where op.Rating >= 100 select new { pg, op }).ToList(); watch.Stop(); ReportString($"Execution of second complex query takes {watch.Elapsed}. Result is {result2.Count} element{(result2.Count == 1 ? string.Empty : "s")}"); Results
I do not list here a set of number or graphs and I don’t want to wrote an hypercube matrix of tested cases, since the tests can be impacted from many conditions:
- machine (KEFCore and InMemory): CPU and available memory have impact
- Apache Kafka cluster speed and network congestion (only KEFCore): the cluster shall run on a Unix (Linux) machine and the test was executed on Windows
- configuration used for Producer/Consumer/Streams (only KEFCore): changing the parameters will change the test results
I want to summarize what I measured, everyone else can do the test using the project in KEFCore repository.
The InMemory provider surpasses everything, however it was expected.
Using KNetCompactedReplicator (enabling it with UseCompactedReplicator property):
- the first query takes more time to be executed than the second one and this was expected: the extra time depends from the network and configuration parameters (conditions 2 and 3) and time used to deserialize the data (condition 1)
- the second query, compared to InMemory, needs more time: this condition is related on how ValueBuffers are built; InMemory provider has ready-made data, KEFCore shall build them from the structured object stored in KNetCompactedReplicator, so the execution time depends from CPU speed (condition 1)
Using Apache Kafka Streams:
- the first query takes more time to be executed than the second one and this was expected: in this case the extra time (more than the KNetCompactedReplicator case) depends mainly from startup phases of allocated Streams and maybe from configuration parameters (condition 3)
- the second query, compared to KNetCompactedReplicator, needs more time: this condition is related on how ValueBuffers are built; KNetCompactedReplicator has executed the deserialization process while the data was received and needs to build only ValueBuffers, while with Apache Kafka Streams the data are stored as raw bytes in the JVM: KEFCore shall request the raw bytes, deserialize them and finally build the ValueBuffers. The execution time depends from CPU speed (condition 1).
Conclusion
Between KNetCompactedReplicator and Apache Kafka Streams surely I choose the first. The current implementation of KEFCore based Apache Kafka Streams is very poor and, as concluded in the previous article, an improvement on its use can come from the direct conversion of EFCore queries into flows of Apache Kafka Streams.
Some work was done on KEFCore to have a well working provider, other shall be done. If you like it you can test it, provide feedback, open issues.
Other articles on this topic are:

