Microservices.io is brought to you by Chris Richardson. Experienced software architect, author of POJOs in Action and the creator of the original CloudFoundry.com. His latest startup is eventuate.io, a microservices application platform.
Chris offers a comprehensive set of resources for learning about microservices including articles, an O'Reilly training video, and example code. Learn more
Chris offers a comprehensive consulting services, workshops and hands on training classes to help you use microservices effectively. Get advice
Want to see an example? Check out Chris Richardson's example applications. See code
Join the microservices google group
You have applied the Saga pattern. In order to be reliable, services must atomically publish events whenever their state changes. It is not viable to use a distributed transaction that spans the database and the message broker.
How to reliably/atomically publish events whenever state changes?
A good solution to this problem is to use event sourcing. Event sourcing persists the state of a business entity such an Order or a Customer as a sequence of state-changing events. Whenever the state of a business entity changes, a new event is appended to the list of events. Since saving an event is a single operation, it is inherently atomic. The application reconstructs an entity’s current state by replaying the events.
Applications persist events in an event store, which is a database of events. The store has an API for adding and retrieving an entity’s events. The event store also behaves like a message broker. It provides an API that enables services to subscribe to events. When a service saves an event in the event store, it is delivered to all interested subscribers.
Some entities, such as a Customer, can have a large number of events. In order to optimize loading, an application can periodically save a snapshot of an entity’s current state. To reconstruct the current state, the application finds the most recent snapshot and the events that have occurred since that snapshot. As a result, there are fewer events to replay.
Customers and Orders is an example of an application that is built using Event Sourcing and CQRS. The application is written in Java, and uses Spring Boot. It is built using Eventuate, which is an application platform based on event sourcing and CQRS.
The following diagram shows how it persist orders.

Instead of simply storing the current state of each order as a row in an ORDERS table, the application persists each Order as a sequence of events.
The CustomerService can subscribe to the order events and update its own state.
Here is the Order aggregate:
public class Order extends ReflectiveMutableCommandProcessingAggregate<Order, OrderCommand> {
private OrderState state;
private String customerId;
public OrderState getState() {
return state;
}
public List<Event> process(CreateOrderCommand cmd) {
return EventUtil.events(new OrderCreatedEvent(cmd.getCustomerId(), cmd.getOrderTotal()));
}
public List<Event> process(ApproveOrderCommand cmd) {
return EventUtil.events(new OrderApprovedEvent(customerId));
}
public List<Event> process(RejectOrderCommand cmd) {
return EventUtil.events(new OrderRejectedEvent(customerId));
}
public void apply(OrderCreatedEvent event) {
this.state = OrderState.CREATED;
this.customerId = event.getCustomerId();
}
public void apply(OrderApprovedEvent event) {
this.state = OrderState.APPROVED;
}
public void apply(OrderRejectedEvent event) {
this.state = OrderState.REJECTED;
}
Here is an example of an event handler in the CustomerService that subscribes to Order events:
@EventSubscriber(id = "customerWorkflow")
public class CustomerWorkflow {
@EventHandlerMethod
public CompletableFuture<EntityWithIdAndVersion<Customer>> reserveCredit(
EventHandlerContext<OrderCreatedEvent> ctx) {
OrderCreatedEvent event = ctx.getEvent();
Money orderTotal = event.getOrderTotal();
String customerId = event.getCustomerId();
String orderId = ctx.getEntityId();
return ctx.update(Customer.class, customerId, new ReserveCreditCommand(orderTotal, orderId));
}
}
It processes an OrderCreated event by attempting to reserve credit for the orders customer.
There are several example applications that illustrate how to use event sourcing.
Event sourcing has several benefits:
Event sourcing also has several drawbacks:
Application architecture patterns
Decomposition
Deployment patterns
Cross cutting concerns
Communication style
External API
Transactional messaging
Service discovery
Reliability
Data management
Security
Testing
Observability
UI patterns