Event-driven microservices communicate by publishing events (facts that already happened, such as “order created”) to a message broker, and other services react to those events instead of being called directly.This is used because services become loosely coupled: the sender does not know who listens, a slow or stopped consumer does not block the sender, and new consumers can be added without changing the producer.Apache Kafka is the broker: it stores events in topics, keeps them for a configured time, and lets many independent consumer groups read the same events at their own speed.In this demo, an order is published as an event, the Inventory service reserves stock, the Order service updates its status from the result event, and the Notification service sends messages, all through Kafka.
Features
- Asynchronous, non-blocking communication between services.
- Loose coupling: the Order service never calls Inventory or Notification.
- Durable events: if a service is down, it processes the missed events when it starts again.
- Publish and subscribe: one event is read by several consumer groups.
- Partitions and message keys keep the events of one order in order and allow parallel processing.
- At-least-once delivery with idempotent consumers (duplicate events are ignored).
- Each service owns its own MySQL database.
- Eventual consistency: an order is
PENDINGfirst and becomesCONFIRMEDorREJECTEDa moment later.
Event-Driven Microservices Types
Event styles
| Style | Meaning | In this demo |
|---|---|---|
| Event notification | A small event says that something happened, and consumers ask for details if needed | Partly |
| Event-carried state transfer | The event carries the data the consumers need, so they never call back | Yes (customer and product data travel in the events) |
| Event sourcing | The state is stored as a sequence of events instead of a current record | No |
| CQRS | Separate models for writing and for reading | No |
Coordination styles
| Style | Meaning | In this demo |
|---|---|---|
| Choreography | Each service reacts to events and publishes new ones, with no central coordinator | Yes |
| Orchestration | A central service tells the others what to do | No (see the Saga demo) |
Kafka building blocks
| Concept | Meaning | In this demo |
|---|---|---|
| Broker | The Kafka server that stores the events | One broker in Docker |
| Topic | A named stream of events | order-created, inventory-result |
| Partition | A part of a topic that allows parallel reading and keeps order inside it | 3 partitions per topic |
| Key | Decides the partition, so equal keys stay in order | orderId |
| Producer | Sends events to a topic | Order service, Inventory service |
| Consumer | Reads events from a topic | All three services |
| Consumer group | Consumers that share the work of a topic, and each group gets all events | order-group, inventory-group, notification-group |
| Offset | The position of a consumer in a partition | Stored by Kafka for each group |
Events vs REST calls
| Point | Event with Kafka | REST call |
|---|---|---|
| Coupling | Loose | Tight |
| Waiting | The sender does not wait | The caller waits |
| If the receiver is down | The event waits in Kafka | The call fails |
| Result | Eventually consistent | Immediate |
Hands-on Experiment: Create an Online Order Processing System Using Apache Kafka
Aim: To create three event-driven Spring Boot microservices (Order, Inventory, and Notification) that communicate only through Apache Kafka topics, with the data stored in MySQL.
The following steps are involved in developing this hands-on experiment.
- Step 1: Generate the Project
- Step 2: Project Structure
- Step 3: Start Apache Kafka
- Step 4: Create Order Micro Service (Producer and Consumer)
- Step 5: Create Inventory Micro Service (Consumer and Producer)
- Step 6: Create Notification Micro Service (Consumer)
- Step 7: Run the Application
Step-by-Step Application Development
Step 1: Generate the Project
Use Spring Initializr with:
- Project: Maven
- Language: Java
- Spring Boot version: 3.3.x (latest stable)
- Group: com.example
- Artifact: kafka-order-processing-demo (parent), with modules
order-service,inventory-service, andnotification-service - Packaging: Jar
- Java version: 17
- Dependencies for all three services: Spring Web, Spring Data JPA, MySQL Driver, Spring for Apache Kafka
Download the generated ZIPs and place the three services as modules inside one parent folder kafka-order-processing-demo. The ready-made multi-module project is also in the zip.
Step 2: Project Structure

Step 3: Start Apache Kafka
services:
kafka:
image: apache/kafka:3.8.0
container_name: kafka
ports:
- "9092:9092"
environment:
KAFKA_NODE_ID: 1
KAFKA_PROCESS_ROLES: broker,controller
KAFKA_LISTENERS: PLAINTEXT://:9092,CONTROLLER://:9093
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
KAFKA_CONTROLLER_QUORUM_VOTERS: 1@localhost:9093
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0docker compose up -d
docker ps
after running above commands
Kafka is now available at localhost:9092.Code language: CSS (css)
Step 4: Create Order Micro Service (Producer and Consumer)
package com.example.orderservice.entity;
import jakarta.persistence.*;
import java.math.BigDecimal;
import java.time.LocalDateTime;
// status: PENDING | CONFIRMED | REJECTED | EVENT_PUBLISH_FAILED
@Entity
@Table(name = "orders")
public class CustomerOrder {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String customerName;
@Column(nullable = false)
private String customerEmail;
@Column(nullable = false)
private Long productId;
@Column(nullable = false)
private Integer quantity;
private String productName;
@Column(precision = 12, scale = 2)
private BigDecimal unitPrice;
@Column(precision = 12, scale = 2)
private BigDecimal totalAmount;
private String status;
@Column(length = 500)
private String failureReason;
private LocalDateTime createdAt;
public CustomerOrder() {
}
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getCustomerName() { return customerName; }
public void setCustomerName(String customerName) { this.customerName = customerName; }
public String getCustomerEmail() { return customerEmail; }
public void setCustomerEmail(String customerEmail) { this.customerEmail = customerEmail; }
public Long getProductId() { return productId; }
public void setProductId(Long productId) { this.productId = productId; }
public Integer getQuantity() { return quantity; }
public void setQuantity(Integer quantity) { this.quantity = quantity; }
public String getProductName() { return productName; }
public void setProductName(String productName) { this.productName = productName; }
public BigDecimal getUnitPrice() { return unitPrice; }
public void setUnitPrice(BigDecimal unitPrice) { this.unitPrice = unitPrice; }
public BigDecimal getTotalAmount() { return totalAmount; }
public void setTotalAmount(BigDecimal totalAmount) { this.totalAmount = totalAmount; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
public String getFailureReason() { return failureReason; }
public void setFailureReason(String failureReason) { this.failureReason = failureReason; }
public LocalDateTime getCreatedAt() { return createdAt; }
public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; }
}
package com.example.orderservice.repository;
import com.example.orderservice.entity.CustomerOrder;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CustomerOrderRepository extends JpaRepository<CustomerOrder, Long> {
}
package com.example.orderservice.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import java.util.Map;
// CONSUMER: listens to the topic where inventory-service publishes its result
@Component
public class OrderEventListener {
private static final Logger log = LoggerFactory.getLogger(OrderEventListener.class);
private final OrderService orderService;
private final ObjectMapper objectMapper;
public OrderEventListener(OrderService orderService, ObjectMapper objectMapper) {
this.orderService = orderService;
this.objectMapper = objectMapper;
}
@KafkaListener(topics = "${app.topics.inventory-result}")
public void onInventoryResult(String message) {
Map<String, Object> event;
try {
event = objectMapper.readValue(message, new TypeReference<Map<String, Object>>() {
});
} catch (JsonProcessingException e) {
// A broken message can never be processed, so it is skipped (otherwise it would block the partition)
log.error("Invalid event skipped: {}", message, e);
return;
}
log.info("Received {} for order {}", event.get("eventType"), event.get("orderId"));
orderService.handleInventoryResult(event);
}
}
package com.example.orderservice.service;
import com.example.orderservice.entity.CustomerOrder;
import com.example.orderservice.repository.CustomerOrderRepository;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
@Service
public class OrderService {
private static final Logger log = LoggerFactory.getLogger(OrderService.class);
private final CustomerOrderRepository orderRepository;
private final KafkaTemplate<String, String> kafkaTemplate;
private final ObjectMapper objectMapper;
private final String orderCreatedTopic;
public OrderService(CustomerOrderRepository orderRepository,
KafkaTemplate<String, String> kafkaTemplate,
ObjectMapper objectMapper,
@Value("${app.topics.order-created}") String orderCreatedTopic) {
this.orderRepository = orderRepository;
this.kafkaTemplate = kafkaTemplate;
this.objectMapper = objectMapper;
this.orderCreatedTopic = orderCreatedTopic;
}
// ======================= Read operations =======================
public List<CustomerOrder> getAllOrders() {
return orderRepository.findAll();
}
public Optional<CustomerOrder> getOrderById(Long id) {
return orderRepository.findById(id);
}
// ======================= PRODUCER: create the order and publish ORDER_CREATED =======================
public CustomerOrder createOrder(CustomerOrder request) {
if (request.getCustomerName() == null || request.getCustomerName().isBlank()
|| request.getCustomerEmail() == null || request.getCustomerEmail().isBlank()
|| request.getProductId() == null
|| request.getQuantity() == null || request.getQuantity() <= 0) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
"customerName, customerEmail, productId and a positive quantity are required");
}
// 1. Save the order in our own database. It stays PENDING until the stock check result arrives.
CustomerOrder order = new CustomerOrder();
order.setCustomerName(request.getCustomerName());
order.setCustomerEmail(request.getCustomerEmail());
order.setProductId(request.getProductId());
order.setQuantity(request.getQuantity());
order.setStatus("PENDING");
order.setCreatedAt(LocalDateTime.now());
order = orderRepository.save(order);
// 2. Publish the event. Nobody is called directly: inventory and notification react to the event.
Long orderId = order.getId();
String payload = toJson(buildOrderCreatedEvent(order));
try {
// key = orderId, so all events of one order go to the same partition (kept in order)
kafkaTemplate.send(orderCreatedTopic, String.valueOf(orderId), payload)
.whenComplete((result, ex) -> {
if (ex != null) {
log.error("Could not publish ORDER_CREATED for order {}", orderId, ex);
markPublishFailed(orderId, ex.getMessage());
} else {
log.info("Published ORDER_CREATED for order {} (partition {}, offset {})", orderId,
result.getRecordMetadata().partition(), result.getRecordMetadata().offset());
}
});
} catch (RuntimeException e) {
log.error("Kafka is not reachable, order {} was not published", orderId, e);
markPublishFailed(orderId, "Kafka is not reachable");
throw new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE,
"Event broker is unavailable. Order " + orderId + " was saved with status EVENT_PUBLISH_FAILED");
}
return order;
}
// ======================= CONSUMER side: apply the result of the stock check =======================
public void handleInventoryResult(Map<String, Object> event) {
Long orderId = Long.valueOf(String.valueOf(event.get("orderId")));
String eventType = String.valueOf(event.get("eventType"));
Optional<CustomerOrder> found = orderRepository.findById(orderId);
if (found.isEmpty()) {
log.warn("Result received for unknown order {}, ignored", orderId);
return;
}
CustomerOrder order = found.get();
// Idempotent: a duplicate event must not change an order that is already decided
if (!"PENDING".equals(order.getStatus())) {
log.info("Order {} is already {}, duplicate {} ignored", orderId, order.getStatus(), eventType);
return;
}
if ("STOCK_RESERVED".equals(eventType)) {
order.setProductName(String.valueOf(event.get("productName")));
order.setUnitPrice(new BigDecimal(String.valueOf(event.get("unitPrice"))));
order.setTotalAmount(new BigDecimal(String.valueOf(event.get("totalAmount"))));
order.setStatus("CONFIRMED");
} else {
order.setStatus("REJECTED");
order.setFailureReason(String.valueOf(event.get("reason")));
}
orderRepository.save(order);
log.info("Order {} is now {}", orderId, order.getStatus());
}
// ======================= Helpers =======================
private Map<String, Object> buildOrderCreatedEvent(CustomerOrder order) {
Map<String, Object> event = new LinkedHashMap<>();
event.put("eventId", UUID.randomUUID().toString());
event.put("eventType", "ORDER_CREATED");
event.put("orderId", order.getId());
event.put("customerName", order.getCustomerName());
event.put("customerEmail", order.getCustomerEmail());
event.put("productId", order.getProductId());
event.put("quantity", order.getQuantity());
event.put("occurredAt", LocalDateTime.now().toString());
return event;
}
private void markPublishFailed(Long orderId, String reason) {
orderRepository.findById(orderId).ifPresent(order -> {
order.setStatus("EVENT_PUBLISH_FAILED");
order.setFailureReason(reason);
orderRepository.save(order);
});
}
private String toJson(Map<String, Object> event) {
try {
return objectMapper.writeValueAsString(event);
} catch (JsonProcessingException e) {
throw new IllegalStateException("Could not convert the event to JSON", e);
}
}
}
package com.example.orderservice.controller;
import com.example.orderservice.entity.CustomerOrder;
import com.example.orderservice.service.OrderService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/orders")
public class OrderController {
private final OrderService orderService;
public OrderController(OrderService orderService) {
this.orderService = orderService;
}
// POST /api/orders -> 202 Accepted: the order is PENDING, the result arrives later through Kafka
@PostMapping
public ResponseEntity<CustomerOrder> createOrder(@RequestBody CustomerOrder order) {
return ResponseEntity.accepted().body(orderService.createOrder(order));
}
// GET /api/orders
@GetMapping
public List<CustomerOrder> getAllOrders() {
return orderService.getAllOrders();
}
// GET /api/orders/{id}
@GetMapping("/{id}")
public ResponseEntity<CustomerOrder> getOrderById(@PathVariable Long id) {
return orderService.getOrderById(id)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
}
package com.example.orderservice;
import org.apache.kafka.clients.admin.NewTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.kafka.config.TopicBuilder;
@SpringBootApplication
public class OrderServiceApplication {
// The service that PRODUCES a topic creates it: 3 partitions, 1 replica (single local broker)
@Bean
public NewTopic orderCreatedTopic(@Value("${app.topics.order-created}") String topicName) {
return TopicBuilder.name(topicName).partitions(3).replicas(1).build();
}
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
}
}
spring.application.name=order-service
server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/kafka_order_db?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=false
server.error.include-message=always
# ---------- Kafka ----------
spring.kafka.bootstrap-servers=localhost:9092
# Producer: events are sent as JSON text. acks=all = wait until the broker has stored the event safely.
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.acks=all
spring.kafka.producer.properties.max.block.ms=5000
# Consumer: order-service reads the result of the stock check
spring.kafka.consumer.group-id=order-group
spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
# Topic names
app.topics.order-created=order-created
app.topics.inventory-result=inventory-resultCode language: PHP (php)
Step 5: Create Inventory Micro Service (Consumer and Producer)
package com.example.inventoryservice.entity;
import jakarta.persistence.*;
import java.math.BigDecimal;
@Entity
@Table(name = "inventory_items")
public class InventoryItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String productName;
@Column(nullable = false, precision = 12, scale = 2)
private BigDecimal unitPrice;
@Column(nullable = false)
private Integer availableQuantity;
public InventoryItem() {
}
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getProductName() { return productName; }
public void setProductName(String productName) { this.productName = productName; }
public BigDecimal getUnitPrice() { return unitPrice; }
public void setUnitPrice(BigDecimal unitPrice) { this.unitPrice = unitPrice; }
public Integer getAvailableQuantity() { return availableQuantity; }
public void setAvailableQuantity(Integer availableQuantity) { this.availableQuantity = availableQuantity; }
}
package com.example.inventoryservice.entity;
import jakarta.persistence.*;
import java.time.LocalDateTime;
// One reservation per order. The unique orderId also protects against processing the same event twice.
@Entity
@Table(name = "reservations")
public class Reservation {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true)
private Long orderId;
@Column(nullable = false)
private Long productId;
@Column(nullable = false)
private Integer quantity;
private LocalDateTime createdAt;
public Reservation() {
}
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public Long getOrderId() { return orderId; }
public void setOrderId(Long orderId) { this.orderId = orderId; }
public Long getProductId() { return productId; }
public void setProductId(Long productId) { this.productId = productId; }
public Integer getQuantity() { return quantity; }
public void setQuantity(Integer quantity) { this.quantity = quantity; }
public LocalDateTime getCreatedAt() { return createdAt; }
public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; }
}
package com.example.inventoryservice.repository;
import com.example.inventoryservice.entity.InventoryItem;
import jakarta.persistence.LockModeType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface InventoryItemRepository extends JpaRepository<InventoryItem, Long> {
// Locks the row, so two listener threads cannot sell the same last item twice
@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("select i from InventoryItem i where i.id = :id")
Optional<InventoryItem> findByIdForUpdate(@Param("id") Long id);
}
package com.example.inventoryservice.repository;
import com.example.inventoryservice.entity.Reservation;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ReservationRepository extends JpaRepository<Reservation, Long> {
boolean existsByOrderId(Long orderId);
}
package com.example.inventoryservice.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import java.util.Map;
// CONSUMER: listens to new orders
@Component
public class InventoryEventListener {
private static final Logger log = LoggerFactory.getLogger(InventoryEventListener.class);
private final InventoryService inventoryService;
private final ObjectMapper objectMapper;
public InventoryEventListener(InventoryService inventoryService, ObjectMapper objectMapper) {
this.inventoryService = inventoryService;
this.objectMapper = objectMapper;
}
@KafkaListener(topics = "${app.topics.order-created}")
public void onOrderCreated(String message) {
Map<String, Object> event;
try {
event = objectMapper.readValue(message, new TypeReference<Map<String, Object>>() {
});
} catch (JsonProcessingException e) {
// A broken message can never be processed, so it is skipped (otherwise it would block the partition)
log.error("Invalid event skipped: {}", message, e);
return;
}
log.info("Received {} for order {}", event.get("eventType"), event.get("orderId"));
// Not inside the try block on purpose: if the processing fails, the exception goes back to Kafka,
// which tries again, so the event is not lost.
inventoryService.processOrder(event);
}
}
package com.example.inventoryservice.service;
import com.example.inventoryservice.entity.InventoryItem;
import com.example.inventoryservice.entity.Reservation;
import com.example.inventoryservice.repository.InventoryItemRepository;
import com.example.inventoryservice.repository.ReservationRepository;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@Service
public class InventoryService {
private static final Logger log = LoggerFactory.getLogger(InventoryService.class);
private final InventoryItemRepository itemRepository;
private final ReservationRepository reservationRepository;
private final KafkaTemplate<String, String> kafkaTemplate;
private final ObjectMapper objectMapper;
private final String inventoryResultTopic;
public InventoryService(InventoryItemRepository itemRepository,
ReservationRepository reservationRepository,
KafkaTemplate<String, String> kafkaTemplate,
ObjectMapper objectMapper,
@Value("${app.topics.inventory-result}") String inventoryResultTopic) {
this.itemRepository = itemRepository;
this.reservationRepository = reservationRepository;
this.kafkaTemplate = kafkaTemplate;
this.objectMapper = objectMapper;
this.inventoryResultTopic = inventoryResultTopic;
}
// ======================= REST operations =======================
public InventoryItem createItem(InventoryItem item) {
if (item.getProductName() == null || item.getProductName().isBlank()
|| item.getUnitPrice() == null || item.getUnitPrice().compareTo(BigDecimal.ZERO) <= 0
|| item.getAvailableQuantity() == null || item.getAvailableQuantity() < 0) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
"productName, unitPrice (greater than 0) and availableQuantity (0 or more) are required");
}
item.setId(null);
return itemRepository.save(item);
}
public List<InventoryItem> getAllItems() {
return itemRepository.findAll();
}
public Optional<InventoryItem> getItemById(Long id) {
return itemRepository.findById(id);
}
public List<Reservation> getAllReservations() {
return reservationRepository.findAll();
}
// ======================= CONSUMER + PRODUCER: handle ORDER_CREATED, publish the result =======================
// One local database transaction. If anything fails (including publishing), the stock change is rolled back
// and Kafka delivers the event again.
@Transactional
public void processOrder(Map<String, Object> orderEvent) {
Long orderId = Long.valueOf(String.valueOf(orderEvent.get("orderId")));
Long productId = Long.valueOf(String.valueOf(orderEvent.get("productId")));
int quantity = Integer.parseInt(String.valueOf(orderEvent.get("quantity")));
// Idempotent: Kafka can deliver an event more than once (at-least-once delivery)
if (reservationRepository.existsByOrderId(orderId)) {
log.info("Order {} was already processed, duplicate event ignored", orderId);
return;
}
Map<String, Object> result = baseResult(orderEvent);
Optional<InventoryItem> found = itemRepository.findByIdForUpdate(productId);
if (found.isEmpty()) {
reject(result, "Product not found with id " + productId);
} else if (found.get().getAvailableQuantity() < quantity) {
reject(result, "Insufficient stock for '" + found.get().getProductName()
+ "'. Available: " + found.get().getAvailableQuantity());
} else {
InventoryItem item = found.get();
item.setAvailableQuantity(item.getAvailableQuantity() - quantity);
itemRepository.save(item);
Reservation reservation = new Reservation();
reservation.setOrderId(orderId);
reservation.setProductId(productId);
reservation.setQuantity(quantity);
reservation.setCreatedAt(LocalDateTime.now());
reservationRepository.save(reservation);
result.put("eventType", "STOCK_RESERVED");
result.put("productName", item.getProductName());
result.put("unitPrice", item.getUnitPrice());
result.put("totalAmount", item.getUnitPrice().multiply(BigDecimal.valueOf(quantity)));
}
publish(orderId, result);
}
// ======================= Helpers =======================
private Map<String, Object> baseResult(Map<String, Object> orderEvent) {
Map<String, Object> result = new LinkedHashMap<>();
result.put("eventId", UUID.randomUUID().toString());
result.put("eventType", "STOCK_RESERVED");
result.put("orderId", orderEvent.get("orderId"));
result.put("customerName", orderEvent.get("customerName"));
result.put("customerEmail", orderEvent.get("customerEmail"));
result.put("productId", orderEvent.get("productId"));
result.put("quantity", orderEvent.get("quantity"));
result.put("occurredAt", LocalDateTime.now().toString());
return result;
}
private void reject(Map<String, Object> result, String reason) {
result.put("eventType", "STOCK_REJECTED");
result.put("reason", reason);
}
private void publish(Long orderId, Map<String, Object> result) {
try {
String payload = objectMapper.writeValueAsString(result);
// wait (max 5 s) until Kafka confirms, so a failure rolls the transaction back
kafkaTemplate.send(inventoryResultTopic, String.valueOf(orderId), payload).get(5, TimeUnit.SECONDS);
log.info("Published {} for order {}", result.get("eventType"), orderId);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException("Interrupted while publishing the result of order " + orderId, e);
} catch (JsonProcessingException | java.util.concurrent.ExecutionException
| java.util.concurrent.TimeoutException e) {
throw new IllegalStateException("Could not publish the result of order " + orderId, e);
}
}
}
package com.example.inventoryservice.controller;
import com.example.inventoryservice.entity.InventoryItem;
import com.example.inventoryservice.entity.Reservation;
import com.example.inventoryservice.service.InventoryService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/inventory")
public class InventoryController {
private final InventoryService inventoryService;
public InventoryController(InventoryService inventoryService) {
this.inventoryService = inventoryService;
}
// POST /api/inventory/items
@PostMapping("/items")
public ResponseEntity<InventoryItem> createItem(@RequestBody InventoryItem item) {
return ResponseEntity.status(HttpStatus.CREATED).body(inventoryService.createItem(item));
}
// GET /api/inventory/items
@GetMapping("/items")
public List<InventoryItem> getAllItems() {
return inventoryService.getAllItems();
}
// GET /api/inventory/items/{id}
@GetMapping("/items/{id}")
public ResponseEntity<InventoryItem> getItemById(@PathVariable Long id) {
return inventoryService.getItemById(id)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
// GET /api/inventory/reservations
@GetMapping("/reservations")
public List<Reservation> getAllReservations() {
return inventoryService.getAllReservations();
}
}
package com.example.inventoryservice;
import org.apache.kafka.clients.admin.NewTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.kafka.config.TopicBuilder;
@SpringBootApplication
public class InventoryServiceApplication {
// inventory-service PRODUCES the result topic, so it creates it
@Bean
public NewTopic inventoryResultTopic(@Value("${app.topics.inventory-result}") String topicName) {
return TopicBuilder.name(topicName).partitions(3).replicas(1).build();
}
public static void main(String[] args) {
SpringApplication.run(InventoryServiceApplication.class, args);
}
}
spring.application.name=inventory-service
server.port=8082
spring.datasource.url=jdbc:mysql://localhost:3306/kafka_inventory_db?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=false
server.error.include-message=always
# ---------- Kafka ----------
spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.acks=all
spring.kafka.producer.properties.max.block.ms=5000
# Consumer: inventory-service reads new orders. 3 listener threads = 3 partitions processed in parallel.
spring.kafka.consumer.group-id=inventory-group
spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.listener.concurrency=3
app.topics.order-created=order-created
app.topics.inventory-result=inventory-resultCode language: PHP (php)
Step 6: Create Notification Micro Service (Consumer)
package com.example.notificationservice.entity;
import jakarta.persistence.*;
import java.time.LocalDateTime;
// type: ORDER_RECEIVED | ORDER_CONFIRMED | ORDER_REJECTED
@Entity
@Table(name = "notifications")
public class Notification {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
// id of the Kafka event that caused this notification (unique: the same event is never handled twice)
@Column(nullable = false, unique = true)
private String eventId;
@Column(nullable = false)
private Long orderId;
private String customerName;
private String customerEmail;
private String type;
@Column(length = 500)
private String message;
private LocalDateTime createdAt;
public Notification() {
}
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getEventId() { return eventId; }
public void setEventId(String eventId) { this.eventId = eventId; }
public Long getOrderId() { return orderId; }
public void setOrderId(Long orderId) { this.orderId = orderId; }
public String getCustomerName() { return customerName; }
public void setCustomerName(String customerName) { this.customerName = customerName; }
public String getCustomerEmail() { return customerEmail; }
public void setCustomerEmail(String customerEmail) { this.customerEmail = customerEmail; }
public String getType() { return type; }
public void setType(String type) { this.type = type; }
public String getMessage() { return message; }
public void setMessage(String message) { this.message = message; }
public LocalDateTime getCreatedAt() { return createdAt; }
public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; }
}
package com.example.notificationservice.repository;
import com.example.notificationservice.entity.Notification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface NotificationRepository extends JpaRepository<Notification, Long> {
boolean existsByEventId(String eventId);
List<Notification> findByOrderIdOrderByIdAsc(Long orderId);
}
package com.example.notificationservice.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import java.util.Map;
// CONSUMER only: notification-service never calls another service. It just reacts to events.
// Both topics are also read by other services, because every consumer GROUP gets its own copy of each event.
@Component
public class NotificationEventListener {
private static final Logger log = LoggerFactory.getLogger(NotificationEventListener.class);
private final NotificationService notificationService;
private final ObjectMapper objectMapper;
public NotificationEventListener(NotificationService notificationService, ObjectMapper objectMapper) {
this.notificationService = notificationService;
this.objectMapper = objectMapper;
}
@KafkaListener(topics = "${app.topics.order-created}")
public void onOrderCreated(String message) {
handle(message);
}
@KafkaListener(topics = "${app.topics.inventory-result}")
public void onInventoryResult(String message) {
handle(message);
}
private void handle(String message) {
Map<String, Object> event;
try {
event = objectMapper.readValue(message, new TypeReference<Map<String, Object>>() {
});
} catch (JsonProcessingException e) {
log.error("Invalid event skipped: {}", message, e);
return;
}
log.info("Received {} for order {}", event.get("eventType"), event.get("orderId"));
notificationService.handleEvent(event);
}
}
package com.example.notificationservice.service;
import com.example.notificationservice.entity.Notification;
import com.example.notificationservice.repository.NotificationRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
@Service
public class NotificationService {
private static final Logger log = LoggerFactory.getLogger(NotificationService.class);
private final NotificationRepository notificationRepository;
public NotificationService(NotificationRepository notificationRepository) {
this.notificationRepository = notificationRepository;
}
public List<Notification> getAllNotifications() {
return notificationRepository.findAll();
}
public List<Notification> getNotificationsByOrder(Long orderId) {
return notificationRepository.findByOrderIdOrderByIdAsc(orderId);
}
// Handles ORDER_CREATED, STOCK_RESERVED and STOCK_REJECTED events
public void handleEvent(Map<String, Object> event) {
String eventId = String.valueOf(event.get("eventId"));
// Idempotent: the same event never creates two notifications
if (notificationRepository.existsByEventId(eventId)) {
log.info("Event {} was already handled, ignored", eventId);
return;
}
String eventType = String.valueOf(event.get("eventType"));
String name = String.valueOf(event.get("customerName"));
Object orderId = event.get("orderId");
String type;
String message;
switch (eventType) {
case "ORDER_CREATED":
type = "ORDER_RECEIVED";
message = "Hi " + name + ", we received your order #" + orderId + ". We are checking the stock.";
break;
case "STOCK_RESERVED":
type = "ORDER_CONFIRMED";
message = "Hi " + name + ", your order #" + orderId + " is confirmed: "
+ event.get("quantity") + " x " + event.get("productName")
+ ". Total: " + event.get("totalAmount");
break;
case "STOCK_REJECTED":
type = "ORDER_REJECTED";
message = "Hi " + name + ", sorry, your order #" + orderId + " could not be completed. "
+ event.get("reason");
break;
default:
log.warn("Unknown event type {}, ignored", eventType);
return;
}
Notification notification = new Notification();
notification.setEventId(eventId);
notification.setOrderId(Long.valueOf(String.valueOf(orderId)));
notification.setCustomerName(name);
notification.setCustomerEmail(String.valueOf(event.get("customerEmail")));
notification.setType(type);
notification.setMessage(message);
notification.setCreatedAt(LocalDateTime.now());
notificationRepository.save(notification);
// A real system would send an e-mail or SMS here
log.info("Sending EMAIL to {}: {}", notification.getCustomerEmail(), message);
}
}
package com.example.notificationservice.controller;
import com.example.notificationservice.entity.Notification;
import com.example.notificationservice.service.NotificationService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/notifications")
public class NotificationController {
private final NotificationService notificationService;
public NotificationController(NotificationService notificationService) {
this.notificationService = notificationService;
}
// GET /api/notifications
@GetMapping
public List<Notification> getAllNotifications() {
return notificationService.getAllNotifications();
}
// GET /api/notifications/order/{orderId}
@GetMapping("/order/{orderId}")
public List<Notification> getNotificationsByOrder(@PathVariable Long orderId) {
return notificationService.getNotificationsByOrder(orderId);
}
}
package com.example.notificationservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class NotificationServiceApplication {
public static void main(String[] args) {
SpringApplication.run(NotificationServiceApplication.class, args);
}
}
spring.application.name=notification-service
server.port=8083
spring.datasource.url=jdbc:mysql://localhost:3306/kafka_notification_db?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=false
server.error.include-message=always
# ---------- Kafka ----------
spring.kafka.bootstrap-servers=localhost:9092
# Consumer only. Its OWN group id, so it receives every event independently of inventory-service and order-service.
spring.kafka.consumer.group-id=notification-group
spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
app.topics.order-created=order-created
app.topics.inventory-result=inventory-resultCode language: PHP (php)
Step 7: Run the services one by one
i. Order service
ii. Inventory service
iii. Notification service


