Sobes.tech
Senior

Design task: The Like service writes likes to Kafka, and the Dashboard service should calculate the post ratings. How to establish interaction?

sobes.tech AI

Answer from AI

To facilitate interaction between the Like service and the Dashboard service via Kafka, the following approach can be used:

  1. The Like service publishes like events to a Kafka topic with a key, for example, the post ID.
  2. The Dashboard service subscribes to this topic and processes events in streaming mode, accumulating the number of likes for each post.
  3. To calculate the rating, Kafka Streams or a separate consumer can be used to update aggregated data in a database or cache.

Example in Java using Kafka Streams:

KStream<String, LikeEvent> likes = builder.stream("likes-topic");
KTable<String, Long> likeCounts = likes
    .groupByKey()
    .count(Materialized.as("like-counts-store"));

likeCounts.toStream().to("post-ratings-topic", Produced.with(Serdes.String(), Serdes.Long()));

This approach ensures scalability, resilience, and up-to-date rating data in the Dashboard service.

Design task: The Like service writes likes to Kafka… - sobes.tech