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.
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.