Skip to content

A lightweight Kafka consumer microservice that listens to addition results from Service A and maintains a running total. Designed for scalability and accuracy in event-driven systems.

Notifications You must be signed in to change notification settings

GergesHany/Service-B

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Service B: The Aggregator

Service B is a resilient and scalable Java-based microservice responsible for consuming calculation results from Apache Kafka and maintaining a persistent, running total. It's designed with a clear separation of concerns for read and write operations using a Leader/Replica database pattern.

🚀 Core Functionality

  • Kafka Consumer: Listens to the user-events topic for incoming messages generated by Service A's outbox pattern.
  • Data Aggregation: Parses each message, extracts the calculated sum, and adds it to a running total.
  • Persistent State: Stores the running total in a PostgreSQL database, ensuring data durability.
  • Leader/Replica Pattern:
    • Write Operations: All updates to the running total are directed to a leader database (outbox_write), ensuring data consistency.
    • Read Operations: The API for retrieving the current total reads from a replica database (outbox_read), separating read and write workloads for better performance and availability.
  • REST API: Exposes a GET /api/summation endpoint to allow clients to fetch the current aggregated sum.
  • Observability: Integrated with Prometheus to export key metrics for monitoring service health and performance.

🛠️ Technology Stack

Category Technology
Framework Spring Boot 3.5
Language Java 21
Messaging Apache Kafka Consumer
Database PostgreSQL (with Leader/Replica pattern)
API REST (Spring Web)
Metrics Micrometer, Prometheus

⚙️ How It Works

  1. Message Consumption: The KafkaConsumerService listens for messages on the user-events topic.
  2. Summation Update: Upon receiving a message, it calls the SummationService to coordinate the update.
  3. Database Write: The DatabaseService updates the running total in the outbox table of the leader database.
  4. Database Replication: The change is asynchronously replicated to the replica database. (Note: The current implementation simulates this by writing to the replica after the leader write is complete).
  5. API Read: When a user hits the GET /api/summation endpoint, the SummationController fetches the value from the replica database, providing a fast, non-blocking read.

⚡ Load Testing

The service includes a K6 script to test the performance of its REST API.

  • Test Script: k6_http.js.
  • To run the test:
    1. Ensure the full application stack is running (make start in the Docker-Compose directory).
    2. Install K6.
    3. Navigate to the Service-B directory and execute the script:
    k6 run k6_http.js

About

A lightweight Kafka consumer microservice that listens to addition results from Service A and maintains a running total. Designed for scalability and accuracy in event-driven systems.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 95.1%
  • JavaScript 4.2%
  • Dockerfile 0.7%