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.
- 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.
- Write Operations: All updates to the running total are directed to a leader database (
- 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.
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 |
- Message Consumption: The
KafkaConsumerService
listens for messages on theuser-events
topic. - Summation Update: Upon receiving a message, it calls the
SummationService
to coordinate the update. - Database Write: The
DatabaseService
updates the running total in theoutbox
table of the leader database. - 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).
- API Read: When a user hits the
GET /api/summation
endpoint, theSummationController
fetches the value from the replica database, providing a fast, non-blocking read.
The service includes a K6 script to test the performance of its REST API.
- Test Script:
k6_http.js
. - To run the test:
- Ensure the full application stack is running (
make start
in theDocker-Compose
directory). - Install K6.
- Navigate to the
Service-B
directory and execute the script:
k6 run k6_http.js
- Ensure the full application stack is running (