Kafka Compact Topic read all events and move onto next action #195
-
Hi We have a use case where we are using Kafka compact topics. Can this be achieved in Silverback? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
We have such a scenario in which we use Kafka as a database i.e. the service starts and we have to wait for all records to be consumed. Conceptually, you have to ensure that you also compact in memory to ensure you have the current view onto the topic. The reason is that Kafka does not guarantee a point in time for compaction, means you can still receive two messages with the same Kafka key (i.e. for the same entity) on startup. However, there is ofc also the scenario that you receive a new message after the startup. We have
How this works together: Kubernetes will not expose the API and does show the deployment in progress as long as the readiness check isn't healthy. As soon as the application starts, the partitions of the topic are consumed from the beginning. As long as the end of partition message isn't sent by Kafka, the health check returns unhealthy. The message are consumed from the Subscriber as if they would in a normal scenario and the in memory DB (actually a concurrent dictionary) is populated. When all message were consumed Kafka sends the end of partition message and the corresponding handler sets the flag on the repository. When this happened for all topics, the health check will return healthy and traffic is routed to the API. If you don't use Kubernetes and thus the health check isn't an option you have to get a bit more creative. I would do something like this: Do the same expect point 2 (the health check). Instead exposing a flag from the repositories, expose a task. Then, inject the repository in that place you want to wait for completion. The method call on the handler completes the task instead of setting a flag to true. Voilà. @BEagle1984 we could massively simplify that. E.g. on the IBroker we already expose the consumers. There could be such a task that is populated when |
Beta Was this translation helpful? Give feedback.
-
@cookie-bytes you can use the EOF callback as explained by @msallin (thanks bro!): see Kafka Events and specifically IKafkaPartitionEofCallback. The idea is that you get notified when you consumed the whole partition, but it's up to you to build the rest of the logic (i.e. triggering the next step only when all partitions reach the EOF etc.). Yes @msallin, in the future I'd like to offer a better abstraction for this, but I'm not sure about the API. I would probably just build a sort of "topic EOF" callback that fires when all partitions reach EOF, for homogeneity. |
Beta Was this translation helpful? Give feedback.
-
Thank you both |
Beta Was this translation helpful? Give feedback.
We have such a scenario in which we use Kafka as a database i.e. the service starts and we have to wait for all records to be consumed. Conceptually, you have to ensure that you also compact in memory to ensure you have the current view onto the topic. The reason is that Kafka does not guarantee a point in time for compaction, means you can still receive two messages with the same Kafka key (i.e. for the same entity) on startup. However, there is ofc also the scenario that you receive a new message after the startup.
The good news is that you can just unify this to one concept and you are able to perform the initial load and then also have a constantly updated view to the topic.
We have