An e-commerce platform with a large product catalog and a significant number of view events wants to implement a system to display a ranking of the most viewed products to highlight popular items on the homepage.
- High volume of events.
- There is an application that sends purchase and view events to a Kafka topic.
- The processing must disregard purchase events.
- It is not possible to modify the application that produces the events.
- Eventual consistency is acceptable.
- The event producer sends events to the Kafka topic.
- An application filters only the view events using Kafka Streams.
- The view events are published to a dedicated topic. Here, we achieve flow isolation, as each event can be sent to its respective topic.
- Another consumer, using Kafka Streams, groups the events by product. This significantly reduces the load on the database.
- The grouped events are sent to an aggregation topic.
- An application consumes the aggregated events topic.
- It saves the data in a database. In this case, we are using a Redis instance dedicated solely to the ranking.
Due to grouping process, there will only be one operation per product (within the aggregation window), significantly reducing the number of database operations.
1 . Run the necessary containers for the application
$ docker-compose up -d
- Access Kafdrop and register the necessary topics
http://localhost:19000
Create the topics:
- product-events-topic
- product-view-events-topic
- product-view-events-aggregated-topic
-
Run the application through the Main.java file
-
Connect to Redis and list the Top 5 most visualized products
127.0.0.1:6379> ZREVRANGE products:events:view:ranking 0 4 WITHSCORES
# Return example
1) "4"
2) "59695"
3) "7"
4) "59464"
5) "12"
6) "58839"
7) "9"
8) "58538"
9) "17"
10) "58508"