A service commvà typically needs lớn update the database and skết thúc messages/events.For example, a service that participates in a saga needs to lớn atomically update the database and sends messages/events.Similarly, a service that publishes a tên miền sự kiện must atomically update an aggregate và publish an sự kiện.The database update & sending of the message must be atomic in order khổng lồ avoid data inconsistencies and bugs.However, it is not viable to use a distributed transaction that spans the database & the message broker to atomically update the database và publish messages/events.

Bạn đang xem: Event sourcing là gì


How to reliably/atomically update the database và publish messages/events?


2PC is not an option


A good solution to lớn this problem is to use sự kiện 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 sự kiện is appended khổng lồ the danh sách 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 & retrieving an entity’s events.The sự kiện store also behaves like a message broker.It provides an API that enables services to subscribe to events.When a service saves an sự kiện in the event store, it is delivered to lớn all interested subscribers.

Some entities, such as a Customer, can have a large number of events.In order to lớn optimize loading, an application can periodically save sầu 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 sầu occurred since that snapshot.As a result, there are fewer events to lớn replay.


Customers và Orders is an example of an application that is built using Event Sourcing và CQRS.The application is written in Java, and uses Spring Boot.It is built using Eventuate, which is an application platsize based on sự kiện sourcing & CQRS.

Xem thêm: Hướng Dẫn Sửa Lỗi Garena Has Stopped Working Khi Chơi Liên Minh

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 & update its own state.

Here is the Order aggregate:

public class Order extends ReflectiveMutableCommandProcessingAggregateOrder, OrderCommand> { private OrderState state; private String customerId; public OrderState getState() return state; public ListEvent> process(CreateOrderCommvà cmd) return EventUtil.events(new OrderCreatedEvent(cmd.getCustomerId(), cmd.getOrderTotal())); public ListEvent> process(ApproveOrderCommvà cmd) return EventUtil.events(new OrderApprovedEvent(customerId)); public ListEvent> process(RejectOrderComm& 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;
It processes an OrderCreated sự kiện by attempting lớn reserve credit for the orders customer.

There are several example applications that illustrate how lớn use event sourcing.

Resulting context

Event sourcing has several benefits:

It solves one of the key problems in implementing an event-driven architecture và makes it possible to reliably publish events whenever state changes. Because it persists events rather than domain objects, it mostly avoids the object‑relational impedance mismatch problem. It makes it possible to implement temporal queries that determine the state of an entity at any point in time. Event sourcing-based business xúc tích và ngắn gọn consists of loosely coupled business entities that exchange events. This makes it a lot easier to lớn migrate from a monolithic application to a microservice architecture.

Event sourcing also has several drawbacks:

It is a different & unfamiliar style of programming và so there is a learning curve.

Related patterns

See also