┌─────────────────────────┐ ┌──────────┐ ┌──────────────────────┐
│ Main Application │ ───────> │ Kafka │────────>│ Analytics Microservice│
│ (Producer and Consumer) │ Events │ │ Events │ (Consumer) │
└─────────────────────────┘ └──────────┘ └──────────────────────┘
▲ │
│ │
│ ┌──────────┐ │
└──────────────────│ Kafka │<───────────────────────────┘
Aggregated │ │ Aggregated data
data └──────────┘ (every 1 minute)
Main Application sends events to Kafka when users perform actions:
| Action | Controller/Service | Topic | Partitioning Key |
|---|---|---|---|
| View book |
BookController.getBookById()
|
book.views
|
bookId
|
| Download book |
BookFileController.downloadBook()
|
book.downloads
|
userId
|
| Purchase book |
StripeService.handlePaymentSuccess()
|
book.purchases
|
userId
|
| Create/update review |
ReviewController
|
book.reviews
|
bookId
|
| Create/update rating |
RatingController
|
book.ratings
|
bookId
|
Analytics Microservice subscribes to topics and actively requests (polls) events from Kafka:
How it works:
Important: Kafka does not send data automatically - the
microservice requests it itself through the poll() method
Important: These are two different processes in the analytics microservice:
Types of aggregated data:
BOOK_STATS - statistics for each bookSYSTEM_OVERVIEW - overall system statisticsPOPULAR_BOOKS - list of popular books
Main Application also uses Kafka Consumer and
actively requests (polls) aggregated data from the
analytics.aggregated-stats topic:
How it works:
analytics.aggregated-stats topic
book_analytics, system_analytics)
Why: Parallel processing and guaranteed order for related events
How it works:
partition = hash(key) % numberOfPartitions
Example:
Topic book.views (3 partitions):
Partition 0: [bookId=3] [bookId=6]
Partition 1: [bookId=1] [bookId=1] ← All bookId=1 events here
Partition 2: [bookId=2] [bookId=5]
Why: Load distribution between multiple service instances
How it works:
Example:
Consumer Group: analytics-service-group
- Consumer 1 → processes Partition 0
- Consumer 2 → processes Partition 1
- Consumer 3 → processes Partition 2
Why: Tracking reading position to avoid losing messages
How it works:
analytics.aggregated-stats topic)
analytics.aggregated-stats topic
and saves it to the database
| Topic | Partitions | Key | Purpose |
|---|---|---|---|
book.views
|
3 |
bookId
|
Book view events |
book.downloads
|
3 |
userId
|
Book download events |
book.purchases
|
2 |
userId
|
Book purchase events |
book.reviews
|
2 |
bookId
|
Review creation/update events |
book.ratings
|
2 |
bookId
|
Rating creation/update events |
analytics.aggregated-stats
|
2 |
aggregationType
|
Aggregated statistics (reverse flow) |
Kafka UI (http://localhost:8089) (admin, admin):