Senior
What should be done if a second event source (second Kafka topic), also changing post ratings, appears?
sobes.tech AI
Answer from AI
If a second event source (a second Kafka topic) appears, which also changes the post ratings, it is necessary to ensure consistency and correct merging of changes from both sources.
Solution options:
-
Processing events from both topics in a single service
- The service subscribes to both topics and applies changes to the rating sequentially.
- A queue or internal buffer can be used to order events.
-
Idempotency and conflict handling
- Make rating updates idempotent so that reprocessing does not affect the result.
- Use versions or timestamps to resolve conflicts.
-
Using an aggregating topic
- Create a third topic where both sources publish aggregated changes.
- The service reads only from the aggregating topic, simplifying logic.
-
Event Sourcing and CQRS
- Store all events in a log.
- Build a separate projection of the rating, applying events from both topics in the correct order.
-
Ensuring processing order
- If possible, synchronize events by key (e.g., by post ID) and use Kafka partitioning to guarantee order.
It is important to carefully consider the merging logic and conflict handling when multiple event sources are involved, to keep the rating consistent and correct.