Overview

Chapter 8. Event-driven architecture with Spring Cloud Stream

Event-driven architecture is presented as a natural fit for microservices because real-world systems do not behave like neat request-response chains; they constantly react to changing conditions through messages. The chapter uses the relationship between an organization service and a licensing service to show why asynchronous communication is valuable, especially when one service needs to know that data in another service has changed. Instead of tightly coupling services through direct REST callbacks, the design shifts toward publishing events that represent state changes, letting other services react independently and without needing to know who sent the event.

The chapter then explains how Spring Cloud Stream simplifies this style of communication by hiding the details of the underlying messaging platform. It introduces the key abstractions used to publish and consume messages: sources for sending, sinks for receiving, channels for logical message paths, and binders for connecting to systems like Kafka. Using these tools, the organization service can publish change events whenever organization data is created, updated, or deleted, and the licensing service can listen for those events and respond automatically. The examples emphasize sending compact event data, such as an organization ID, action type, and correlation ID, so that consumers can track and process changes reliably.

A major use case in the chapter is distributed caching. The licensing service first checks Redis for organization data and only calls the organization service if the cache misses, then stores the result for later use. When the organization service publishes an update or delete event to Kafka, the licensing service consumes that message and evicts the stale cache entry, keeping data reasonably fresh without constant synchronous calls. The chapter also shows how to define custom channels, configure consumer groups, and wire message handlers so that cache invalidation happens automatically. Along the way, it warns that messaging brings its own complexity, including ordering concerns, visibility across services, and debugging challenges, but argues that the benefits in decoupling, durability, scalability, and extensibility make it a powerful approach for microservices.

Figure 8.1. In a synchronous request-response model, tightly coupled services introduce complexity and brittleness.

Figure 8.2. As organization state changes, messages will be written to a message queue that sits between the two services.

Figure 8.3. As a message is published and consumed, it flows through a series of Spring Cloud Stream components that abstract away the underlying messaging platform.

Figure 8.4. When organization service data changes it will publish a message to Kafka.

Figure 8.5. When a message comes into the Kafka orgChangeTopic, the licensing service will respond.

Figure 8.6. The consumer group guarantees a message will only be processed once by a group of service instances.

Figure 8.7. Updating the contact phone number using the organization service

Figure 8.8. The console shows the message from the organization service being sent and then received.

Summary

  • Asynchronous communication with messaging is a critical part of microservices architecture.
  • Using messaging within your applications allows your services to scale and become more fault tolerant.
  • Spring Cloud Stream simplifies the production and consumption of messages by using simple annotations and abstracting away platform-specific details of the underlying message platform.
  • A Spring Cloud Stream message source is an annotated Java method that’s used to publish messages to a message broker’s queue.
  • A Spring Cloud Stream message sink is an annotated Java method that receives messages off a message broker’s queue.
  • Redis is a key-value store that can be used as both a database and cache.

FAQ

What is event-driven architecture, and why is it useful in microservices?Event-driven architecture uses asynchronous messages to communicate changes in state between services. In microservices, it helps keep services loosely coupled so they can react to events without needing direct, tightly bound service calls.
Why is synchronous request-response communication a poor fit for sharing state changes between services?Synchronous communication creates tight coupling, makes services brittle when endpoints change or fail, and makes it harder to add new consumers without modifying and redeploying the original service.
What are the main benefits of using messaging between the organization and licensing services?Messaging provides loose coupling, durability, scalability, and flexibility. The sender does not need to know who consumes the message, messages can wait in the queue if a consumer is down, consumers can scale horizontally, and new consumers can be added easily.
What are the downsides of a messaging-based architecture?Messaging can be more complex because you must think about message ordering, retries and failures, message visibility, correlation IDs, and debugging workflows that may happen out of order across multiple services.
What is Spring Cloud Stream?Spring Cloud Stream is an annotation-driven Spring project that simplifies building message publishers and consumers. It abstracts away the details of the underlying messaging platform, such as Kafka or RabbitMQ.
What are the core Spring Cloud Stream components used for message flow?The main components are Source, Channel, Binder, and Sink. Source publishes a message, Channel represents the messaging channel, Binder connects Spring Cloud Stream to the messaging platform, and Sink consumes incoming messages.
How does the organization service publish an organization change event?The organization service injects a Source, builds an OrganizationChangeModel object, and sends it through the Source output channel. The message includes the action, organization ID, and correlation ID.
How is the licensing service configured to consume organization change events?The licensing service uses @EnableBinding(Sink.class) and @StreamListener(Sink.INPUT) to listen for messages. Its configuration maps the input channel to the Kafka topic and assigns a consumer group name.
Why is a consumer group important in Spring Cloud Stream?A consumer group ensures that when multiple instances of a service are listening to the same topic, only one instance in that group processes a given message. This supports consume-once semantics for the service group.
How does distributed caching work in this chapter’s example?The licensing service first checks Redis for organization data. If the data is missing, it calls the organization service, stores the result in Redis, and returns it. When the organization service publishes an update or delete event, the licensing service consumes the message and removes the stale data from Redis.