Home | RU | EN

Kafka Interaction - Overview

Interaction Architecture

┌─────────────────────────┐           ┌──────────┐         ┌──────────────────────┐
│  Main Application       │ ───────>  │  Kafka   │────────>│ Analytics Microservice│
│ (Producer and Consumer) │ Events    │          │ Events  │    (Consumer)        │
└─────────────────────────┘           └──────────┘         └──────────────────────┘
         ▲                                                          │
         │                                                          │
         │                  ┌──────────┐                            │
         └──────────────────│  Kafka   │<───────────────────────────┘
          Aggregated        │          │   Aggregated data
             data           └──────────┘    (every 1 minute)

Forward Flow: Main Application → Kafka → Analytics Microservice

1. Sending Events (Producer)

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

2. Processing Events (Consumer)

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

Reverse Flow: Analytics Microservice → Kafka → Main Application

1. Data Aggregation

Important: These are two different processes in the analytics microservice:

  1. Continuous event reading (see "Processing Events" section above):
    • The microservice continuously requests (polls) events from Kafka
    • Each event is processed immediately and updates statistics in memory
    • This happens in real-time, not once per minute
  2. Periodic aggregation (every 1 minute):
    • A separate scheduled task runs once per minute
    • Aggregates already accumulated data in memory
    • Sends aggregated results back to Kafka

Types of aggregated data:

2. Receiving and Saving (Consumer)

Main Application also uses Kafka Consumer and actively requests (polls) aggregated data from the analytics.aggregated-stats topic:

How it works:

Key Concepts

Partitioning

Why: Parallel processing and guaranteed order for related events

How it works:

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]

Consumer Groups

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

Offset

Why: Tracking reading position to avoid losing messages

How it works:

Full Data Cycle

  1. User performs an action (view, purchase, etc.)
  2. Main Application sends an event to Kafka (this is called asynchronous, as after sending, the main application does not wait for the analytics microservice to process the sent message)
  3. Kafka saves the event in the corresponding topic and partition
  4. Analytics Microservice continuously requests (polls) new events from Kafka, receives the event and immediately updates statistics in memory (in this case, it has no database, it stores everything in RAM)
  5. Every 1 minute a separate scheduled task aggregates already accumulated data in memory and sends aggregated results back to Kafka (to the analytics.aggregated-stats topic)
  6. Main Application continuously requests (polls) aggregated data from the analytics.aggregated-stats topic and saves it to the database
  7. Admin Panel receives statistics via REST API from the database

Kafka Topics

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)

Advantages of the Approach

Monitoring

Kafka UI (http://localhost:8089) (admin, admin):