VOOZH about

URL: https://www.javacodegeeks.com/2025/07/event-driven-microservices-with-kafka-and-spring-cloud-stream.html

โ‡ฑ Event-Driven Microservices with Kafka and Spring Cloud Stream - Java Code Geeks


In todayโ€™s world of distributed systems, event-driven architecture (EDA) has become the foundation for building scalable, resilient, and loosely coupled microservices. Apache Kafka, when paired with Spring Cloud Stream, offers a robust framework for enabling asynchronous communication between services.

This article dives into how you can design event-driven microservices using Kafka and Spring Cloud Stream, covering core concepts, code examples, and best practices for real-world applications.

Why Event-Driven Architecture?

Traditional request-response models (e.g., REST) are synchronous and can create tight coupling between services. In contrast, EDA allows services to emit and respond to events independently, improving:

  • Decoupling
  • Scalability
  • Fault tolerance
  • Observability

What is Spring Cloud Stream?

Spring Cloud Stream is a framework that simplifies the development of event-driven microservices by abstracting messaging platforms (like Kafka, RabbitMQ) behind a common programming model.

Instead of manually writing Kafka producers and consumers, you use declarative annotations and configuration to wire your services.

๐Ÿ“˜ Official Docs

Setting Up Kafka with Spring Cloud Stream

1. Add Dependencies (Maven)

<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-stream-kafka</artifactId>
</dependency>

Also, ensure your spring-cloud.version is managed in the dependency management:

<dependencyManagement>
 <dependencies>
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-dependencies</artifactId>
 <version>2023.0.1</version>
 <type>pom</type>
 <scope>import</scope>
 </dependency>
 </dependencies>
</dependencyManagement>

Creating an Event Model

Letโ€™s define a simple domain event:

public record OrderCreatedEvent(String orderId, String customerId, double totalAmount) {}

Creating an Event Model

Letโ€™s define a simple domain event:

javaCopyEditpublic record OrderCreatedEvent(String orderId, String customerId, double totalAmount) {}

Records are great for immutable event payloads. See our article on Java Record Classes for more.

Sending Events (Producer Service)

Spring Cloud Stream uses bindings to define output destinations.

application.yml

spring:
 cloud:
 stream:
 bindings:
 orderCreated-out-0:
 destination: order-events
 kafka:
 binder:
 brokers: localhost:9092

Producer Code

@EnableBinding
@RestController
public class OrderController {

 private final StreamBridge streamBridge;

 public OrderController(StreamBridge streamBridge) {
 this.streamBridge = streamBridge;
 }

 @PostMapping("/orders")
 public String createOrder(@RequestBody OrderCreatedEvent order) {
 streamBridge.send("orderCreated-out-0", order);
 return "Order event sent!";
 }
}

Receiving Events (Consumer Service)

application.yml

spring:
 cloud:
 stream:
 bindings:
 orderCreated-in-0:
 destination: order-events
 group: inventory-group

Listener Code

@EnableBinding
@SpringBootApplication
public class InventoryService {

 public static void main(String[] args) {
 SpringApplication.run(InventoryService.class, args);
 }

 @Bean
 public Consumer<OrderCreatedEvent> orderCreated() {
 return event -> {
 System.out.println("Received order: " + event.orderId());
 // Update inventory logic here
 };
 }
}

The message will only be delivered once per consumer group, supporting at-least-once delivery.

Real-World Use Case

E-Commerce Platform

  • Order Service emits OrderCreatedEvent
  • Inventory Service listens to it and reserves stock
  • Shipping Service listens and prepares delivery
  • Billing Service listens and triggers payment

This design enables independent scaling and failure isolation.

Testing Kafka Locally

For local development, you can run Kafka using Docker:

# docker-compose.yml
version: '3'
services:
 zookeeper:
 image: confluentinc/cp-zookeeper
 environment:
 ZOOKEEPER_CLIENT_PORT: 2181

 kafka:
 image: confluentinc/cp-kafka
 ports:
 - 9092:9092
 environment:
 KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
 KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
 KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1

Best Practices

โœ… Design Small, Self-Contained Events

Keep event payloads minimal and focused on conveying facts.

โœ… Use Schema Validation

Use tools like Avro or JSON Schema to enforce contract validation.

โœ… Handle Failures Gracefully

Set up dead letter topics (DLTs) and retry mechanisms for processing failures.

โœ… Decouple Services with Event-Driven Contracts

Use async API specs or event contract testing to maintain compatibility between teams.

โœ… Observe with Tracing and Metrics

Integrate Micrometer, Zipkin, or OpenTelemetry to track event flows.

Comparison: Kafka Native API vs Spring Cloud Stream

FeatureKafka Client APISpring Cloud Stream
BoilerplateHighLow
Portability (Kafka/Rabbit)Kafka onlyPluggable
Abstraction LevelLow-levelHigh-level
Easy Retry / DLTManualBuilt-in
Integration with Spring BootManualSeamless

References

Conclusion

By combining Apache Kafka with Spring Cloud Stream, you can design truly resilient, event-driven microservices with minimal boilerplate and strong messaging guarantees. Whether youโ€™re handling payments, processing orders, or managing inventory, this architectural pattern will improve your systemโ€™s scalability, maintainability, and fault tolerance.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

๐Ÿ‘ Photo of Eleftheria Drosopoulou
Eleftheria Drosopoulou
July 15th, 2025Last Updated: July 9th, 2025
0 410 2 minutes read

Eleftheria Drosopoulou

Eleftheria is an Experienced Business Analyst with a robust background in the computer software industry. Proficient in Computer Software Training, Digital Marketing, HTML Scripting, and Microsoft Office, they bring a wealth of technical skills to the table. Additionally, she has a love for writing articles on various tech subjects, showcasing a talent for translating complex concepts into accessible content.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz