Photo by Ferenc Almasi on Unsplash

In this article I don’t speak about usage of Entity Framework Core, there are tons of documentation about its usage, but I speak about back-ends. EFCore (Entity Framework Core) relies on providers to communicate with back-ends, the full list is available here.

The question is:

Can EFCore use Apache Kafka as back-end?

The short answer is yes and long answer is KEFCore: Entity Framework Core provider for Apache Kafka.

KEFCore is a provider able to use Apache Kafka as back-end for Entity Framework Core.

Model definition

The model used in the example is the same available here, the difference is in the provider requested: in the example of the repository it is used an Apache Kafka cluster inheriting from KafkaDbContext:

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; } } 

KafkaDbContext is an helper class which hides the standard way to declare the provider to be used, it overrides the OnConfiguring method of DbContext, checks the mandatory information and sets the required information.

Back-end storage fill and query

With the following code the back-end is filled in:

using (var context = new BloggingContext() { BootstrapServers = serverToUse, ApplicationId = "TestApplication", DbName = "TestDB", StreamsConfigBuilder = streamConfig, }) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); for (int i = 0; i < 1000; i++) { context.Add(new Blog { Url = "http://blogs.msdn.com/adonet" + i.ToString(), Posts = new List<Post>() { new Post() { Title = "title", Content = i.ToString() } }, Rating = i, }); } context.SaveChanges(); } 

And with the following code the data stored in the back-end can be queried:

using (var context = new BloggingContext() { BootstrapServers = serverToUse, ApplicationId = "TestApplication", DbName = "TestDB", StreamsConfigBuilder = streamConfig, }) { var post = context.Posts.Single(b => b.BlogId == 2); post = context.Posts.Single(b => b.BlogId == 1); var all = context.Posts.All((o) => true); var blog = context.Blogs!.Single(b => b.BlogId == 1); var value = context.Blogs.AsQueryable().ToQueryString(); } 

Context initialization

The information used in the example to initialize the context are:

  • BootstrapServers: a mandatory string containing a comma separated list of brokers, in the form host:port, to identify the Kafka cluster to be used
  • ApplicationId: an optional identifier used from Apache Kafka Streams to distinguish multiple applications, if not set it is automatically generated
  • DbName: a mandatory identifier used to distinguish, within Apache Kafka cluster, topics for a specific scope
  • StreamsConfigBuilder: an optional instance of StreamsConfigBuilder used to give specific settings to Apache Kafka Streams, it is used from the test

Other options available are:

  • ProducerConfigBuilder: an optional instance of ProducerConfigBuilder used to give specific settings to Apache Kafka Producer
  • TopicConfigBuilder: an optional instance of TopicConfigBuilder used to give specific settings to KEFCore when topics are created if not already available
  • UsePersistentStorage: set to true if Apache Kafka Streams shall use a persistent storage across application run, otherwise data are stored in memory

What’s in the back-end

In the previous application the provider will create two topics (see Apache Kafka site for explanation), one for Blogs and one for Posts. Using an application to read information from an Apache Kafka cluster it is possible to read what was written in and then read from the topics.

The topics are named in the following format: DbName.FullQualifiedDataClassName

Considering that the namespace in the example is MASES.EntityFrameworkCore.KNet.Test, the previous application creates two topics named:

  • TestDB.MASES.EntityFrameworkCore.KNet.Test.Blog
  • TestDB.MASES.EntityFrameworkCore.KNet.Test.Post

The provider stores the key information (BlogId and PostId) as single element, while the data, including the key itself, are stored as a JSON string which contains the .NET type identifier to be used during deserialization. A screenshot of the Apache Kafka client I used is the following:

Topics in Apache Kafka cluster

A value of Blog entry is the following:

{ "typeName": "MASES.EntityFrameworkCore.KNet.Test.Blog", "data": [ 1, 0, "http://blogs.msdn.com/adonet0" ] } 

A value of Post entry is the following:

{ "typeName": "MASES.EntityFrameworkCore.KNet.Test.Post", "data": [ 1, 1, "0", "title" ] } 

Looking at the previous JSONs, and knowing how the types are defined, it is possible to associate the data array in the JSON to the corresponding types and meaning. Using other kind of application based on Apache Kafka, consuming from the topics it is possible to use the data of the topics for other reasons.

How the provider works

In an previous article I spoke about KNet, a new way to access the features of Apache Kafka from .NET, and I introduced the ability of KNet to use Apache Kafka Streams. KEFCore relies on KNet and specifically on Apache Kafka Streams. Now I’m going to speak more about KEFCore, firstly introducing Apache Kafka Streams.

Apache Kafka Streams

This article don’t want to speak about Apache Kafka Streams in deep, for a complete introduction the reader can surf the following link:

I want to concentrate on two concepts behind Apache Kafka Streams:

Apache Kafka stores information within topics using records of key-value pair ordered by their produce sequence: in the article I consider there is a stream of key-value pair records stored in a container named topic. Surf Apache Kafka site to understand more on this.

KStream represent exactly the stream of key-value pair records within the topic. While KTable represents the same data in a table forms of records built by two columns: key and value. There is a duality between KStream and KTable, KEFCore uses KTable.

Database equivalency

A database (SQL and/or NoSQL) is a set of data record, each record can be viewed as:

  • Primary Key: a single identifier, or a tuple of identifiers, that distinguish the single record; the single identifier can be equivalent to the key of a Kafka record, if the primary key has multiple elements the corresponding Kafka key can be a structure containing the identifiers
  • Values: a set of items containing the data of the record; if the set has a single item it is equivalent to the value of a Kafka record, if the set has multiple items the corresponding Kafka value can be a structure containing the set of items

What I described is a possible equivalence between a database back-end and Apache Kafka back-end. In RDBMS the primary key can be unique and in this case Apache Kafka comes with a special feature named compacted topic: without entering in details, from the point of view of the final result, if we update the topic using the same key the oldest values with the same key are discarded and only the latest value will be available.

KEFCore uses compacted topics to reduce the size of the data stored in the Apache Kafka cluster.

Both together

With the previous considerations an entire database could be converted in many Kafka topics: each SQL database table can be stored in a topic, or many NoSQL documents can be stored in Kafka topics. With this consideration, I can recall a previous chapter where I shown the content of the Apache Kafka topics of the example, the data managed from EFCore application are stored in the Apache Kafka cluster; with a migration application, not available in KEFCore, any database storing EFCore data can be converted into Apache Kafka topics.

Apache Kafka as distributed cache

Without thinking to an equivalence to databases, from the point of view of the data, it is possible to state that:

  • data are stored uniquely, within the associated topics, with their key-value pair
  • data are persisted and are available to any client requesting them from the topics
  • in a distributed environment any modification stored within the associated topics is sent to any client subscribed to the topics

The above sentences make me thinking to a distributed cache able to sent real-time updates to the subscribed clients, simplifying it is possible to think to something similar to Redis. Using KEFCore, an application based on EFCore, can take advantages of Apache Kafka and use it instead of Redis.

Conclusion

Update — 2026: Three years have passed since this article was published. KEFCore has evolved and most of the following are available. If you want to see where the project stands today, read the follow-up: KEFCore re-engineered: Apache Kafka™ as an Entity Framework Core provider, three years later.

This article just introduced KEFCore as an alternative provider for Entity Framework Core. As stated at the beginning of the article it is still in development however the test code is working and anyone can test it. What is possible to highlight using the example is:

  • The data are stored in the topics as string, simple types are converted in string too; the back conversion is connected to the data type retrieved from the application assembly: if the type change the data stored cannot be converted back
  • The structure used to store the data is more or less fixed; the schema of the JSON is fixed and the type, within the JSON, is retrieved from the application assembly
  • At startup, the initialization takes a lot of time and the first query is done slowly; trying to avoid context.Database.EnsureDeleted(); and using UsePersintentStorage=true , the application seems to work better

Some possible improvements can be:

  1. Create and publish the JSON schema of involved data types, this can help external applications, consuming from or producing to the topics, to manage the data without retrieve the .NET data type stored in the assembly
  2. KNet comes with the [KNetCompactedReplicator](https://github.com/masesgroup/KNet/blob/master/src/net/KNet/Specific/KNetCompactedReplicator.cs) class which can be used to retrieve a compacted topic and manage the data as a .NET dictionary avoiding Apache Kafka Streams; this can help the condition where Apache Kafka is used as a distributed cache
  3. Use Apache Kafka Streams when there are some relationship between tables in queries which can be resolved using features of Apache Kafka Streams: convert queries of EFCore into flows of Apache Kafka Streams, this can speed-up the responsiveness of the applications

Some work shall be done on KEFCore to have a well working provider, however it is working and anyone can test it. If you like it you can test it, provide feedback, open issues.

Other articles on this topic are: