Upon first major release of Entity Framework Core provider for Apache Kafka I try to describe how it can be used in some operative conditions to unleash the power of Apache Kafka. However it is important to describe how it works to better understand some other concepts.
NOTE : In the following chapters sometime it is used the term back-end and sometime Apache Kafka cluster: they shall be considered the same thing int the Entity Framework Core provider for Apache Kafka context.
Basic concepts
Here below an image describing simple concepts:

Kafka image from https://www.qlik.com/us/streaming-data/apache-kafka
Simplifying, there are three active elements:
- Topics : storage of the records (the data), they are hosted in the Apache Kafka cluster and can be partitioned
- Producers : entities producing records to be stored in one or more topics
- Consumers : entities receiving records from the topics
When a producer send a record to Apache Kafka cluster, the record will be sent to the consumers subscribed to the topics the producer is producing on: this is a classic pub-sub pattern. Apache Kafka cluster adds the ability to store this information within the topic the producer has produced on, this feature guarantee that:
- an application consuming from the Apache Kafka cluster can hear only latest changes or reposition to a specific record in the past and start from that point to receive data
- the standard way to consume from Apache Kafka cluster is to start from the end (latest available record) or start from the beginning (first available record)
How Entity Framework Core provider for Apache Kafka works
An application based on Entity Framework Core provider for Apache Kafka is both a producer and a consumer at the same time:
- when an entity is created/updated/deleted (e.g. calling SaveChanges) the provider will invoke the right producer to store a new record in the right topic of the Apache Kafka cluster
- then the consumer subscribed will be informed about this new record and will store it back: this seems not useful till now, but it will be more clear later
Apache Kafka cluster becomes:
- a central routing for data changes in multiple Entity Framework Core based applications;
- a reliable storage because, when the application restarts, the data stored in the topics will be read back from the consumers so the local state will be aligned to the latest available.
Apache Kafka comes with topic compaction feature: this feature optimizes the point 2. Entity Framework Core provider for Apache Kafka is interested to store only the latest state of the entity and not the changes. Using the topic compaction, the combination of producer, consumer and Apache Kafka cluster can apply the CRUD operations on data:
- Create : a producer stores a new record with its unique key
- Read : a consumer retrieves records from topic
- Update : when a producer stores a new record with a previously stored unique key, the Apache Kafka cluster will discard the old records
- Delete : when a producer stores a new record with a value set to null, and a previously stored unique key, the Apache Kafka cluster will delete all records with that unique key
All CRUD operations are helped, behind the scene, from KNetCompactedReplicator or KNetProducer/ Apache Kafka Streams.
First-level cache
KNetCompactedReplicator or Apache Kafka Streams act as first-level cache of Entity Framework Core: data coming from the Apache Kafka cluster update their in-memory image while the system is running. The behavior is intrinsic and does not need any extra call to the back-end.
Data storage
Apache Kafka stores the information using records. It is important to convert entities in something usable from Apache Kafka. The conversion is done using serializers that converts the Entities (data in the model) into Apache Kafka records and viceversa: see serialization chapter for more info.
Entity Framework Core provider for Apache Kafka compared to other providers
In the previous chapter was described how Entity Framework Core provider for Apache Kafka permits to reproduce the CRUD operations. Starting from the model defined in the code, the data are stored in the topics and each topic can be seen as a table of a database filled in with the same data. From the point of view of an application, the use of Entity Framework Core provider for Apache Kafka is similar to the use of the InMemory provider.
A note on migrations
The current version of Entity Framework Core provider for Apache Kafka does not support migrations explicitly.
Use case: database
An use case can be coupled to a standard usage of Entity Framework Core, the same case when Entity Framework Core is used with database providers. In getting started is proposed a simple example following the online documentation. In the example the data within the model are stored in multiple Apache Kafka topics, each topic is correlated to the DbSet described from the DbContext.
The constraints are managed using OnModelCreating of DbContext.
Use case: user data in topics
Changing the mind about model, another use case can be coupled on how an Entity Framework Core model can be used; since the data within the model are stored in multiple Apache Kafka topics, if the model is written in a way it describes the data to be stored within the topics it is possible to define an uncorrelated model containing the data of interest:
public class ModelContext : KafkaDbContext { public DbSet<FirstData> FirstDatas { get; set; } public DbSet<SecondData> SecondDatas { get; set; } } public class FirstData { public int ItemId { get; set; } public string Data { get; set; } } public class SecondData { public int ItemId { get; set; } public string Data { get; set; } } Then using standard APIs of Entity Framework Core, an user interacting with Entity Framework Core can stores, or retrieves, data without any, or limited, knowledge of Apache Kafka.
Entity Framework Core provider for Apache Kafka features not available in other providers
Here a list of features Entity Framework Core provider for Apache Kafka gives to its user and useful in some use cases. The features below are strictly correlated with the consumers receiving back the record from Apache Kafka cluster described above.
Distributed cache
In the previous chapter was stated that consumers align the application data to the last topics information. The alignment is managed from KNetCompactedReplicator and/or Apache Kafka Streams, everything is driven from the Apache Kafka back-end. Considering two, or more, applications, sharing the same model and configuration, they always align to the latest state of the topics involved. This implies that, virtually, there is a distributed cache between the applications and the Apache Kafka back-end:
- Apache Kafka stores physically the cache (shared state) within the topics and routes changes to the subscribed applications
- Applications use latest cache version (local state) received from Apache Kafka back-end
If an application restarts it will be able to retrieve latest data (latest cache) and aligns to the shared state.
Changing the mind a model is written it is possible to define a set of classes which acts as storage for data we want to use as a cache. It is possible to build a new model like:
public class CachingContext : KafkaDbContext { public DbSet<Item> Items { get; set; } } public class Item { public int ItemId { get; set; } public string Data { get; set; } } Sharing it between multiple applications and allocating the CachingContext in each application, the cache is shared and the same data are available.
Events
Generally, an application based on Entity Framework Core, executes queries to the back-end to store, or retrieve, information on demand. The alignment (record consumed) can be considered a change event: so any change in the backend produces an event used in different mode:
- Mainly these change events are used from KNetCompactedReplicator and/or Apache Kafka Streams to align the local state;
- Moreover Entity Framework Core provider for Apache Kafka can inform, using callbacks and at zero cost, the registered application about these events.
The application can use the reported events in many modes:
- execute a query
- write something to disk
- execute a REST call
- and so on
IMPORTANT NOTE : the events are raised from external threads and this can lead to concurrent exceptions if the KafkaDbContext is used to retrieve information.
Continuing from the previous use case, using the events reported from Entity Framework Core provider for Apache Kafka it is possible to write a reactive application. When a change event is triggered the application can react to it and take an action.
The triggered distributed cache can be:
- seen as a Redis backend;
- used side-by-side with SignalR: combining Entity Framework Core provider for Apache Kafka and SignalR in an application, subscribing to the change events, it is possible to feed the connected applications to SignalR.
Applications not based on Entity Framework Core
Till now was spoken about applications based on Entity Framework Core, however this provider can be used to feed applications not based on Entity Framework Core. Entity Framework Core provider for Apache Kafka comes with ready-made helping classes to subscribe to any topic of the Apache Kafka cluster to retrieve the data stored from an application based on Entity Framework Core. Any application can use this feature to:
- read latest data stored in the topics from the application based on Entity Framework Core
- attach to the topics involved from the application based on Entity Framework Core and receive change events upon something was produced
The ready-made helping classes upon a record is received, deserialize it and returns back the filled Entity.
Use cases
The schema used to write the information in the topics are available, or can be defined from the user, so an external application can use the data in many mode:
- Using the feature to extract the entities stored in the topics outside the application based on Entity Framework Core
- Use some features of Apache Kafka like Apache Kafka Streams or Apache Kafka Connect.
An application, not based on Entity Framework Core, can subscribe to the topics to:
- store all change events to another medium
- analyze the data or the changes
- Use Apache Kafka Streams: Apache Kafka comes with the powerful Streams feature. An application based on Streams can analyze streams of data to extract some information or converts the data into something else. It is possible to build an application, based on Apache Kafka Streams, which hear on change events and produce something else or just sores them in another topic containing all events not only the latest (e.g. just like the transaction log of SQL Server does it).
- Use Apache Kafka Connect: Apache Kafka comes with another powerful feature called Connect: it comes with some ready-made connector which connect Apache Kafka with other systems (database, storage, etc).
NOTE : While Apache Kafka Streams is an application running alone, Apache Kafka Connect can allocate the connectors using its distributed behavior which load-balance and automatically restarts operation if something is going wrong.
Originally published at https://masesgroup.github.io.
Other articles on this topic are:



