This application demonstrates two key patterns:
The application consists of three services:
-
Order Service
- manages orders -
Customer Service
- manages customers -
Order History Service
- maintains the order history
All services are implemented using Node.js and the Eventuate Tram framework, which provides transactional publish/subscribe.
The Order Service
uses a choreography-based saga to enforce the customer’s credit limit when creating orders.
The Order History Service
implements a CQRS view and subscribes to domain events published by the Order Service
and Customer Service
Sagas are a mechanism for maintaining data consistency in a microservice architecture. A saga is a sequence of transactions, each of which is local to a service.
There are two main ways to coordinate sagas: orchestration and choreography. This example uses choreography-based sagas, which use domain events for coordination. Each step of a saga updates the local database and publishes a domain event. The domain event is processed by an event handler, which performs the next local transaction.
To learn more about why you need sagas if you are using microservices:
-
Look at the Orchestration-based saga example
-
Read the Saga pattern
-
Look at MicroCPH 2019 presentation
-
Read about sagas in my Microservices patterns book
The saga for creating an Order
consists of the follow steps:
-
The Order Service creates an
Order
in aPENDING
state and publishes anOrderCreated
event -
The
Customer Service
receives the event attempts to reserve credit for thatOrder
. It publishes either aCredit Reserved
event or aCreditLimitExceeded
event. -
The
Order Service
receives the event and changes the state of the order to eitherAPPROVED
orREJECTED
.
The CQRS pattern implements queries that retrieves data from multiple services. It maintains a queryable replica of the data by subscribing to domain events published by the services that own the data.
In this example, the Order History Service
maintains a CQRS view in MongoDB by subscribing to domain events published by the Order Service
and Customer Service
.
The CQRS view stores each customer as a MongoDB document that contains information the customer and their orders.
To learn more about why you need CQRS if you are using microservices:
-
Read the CQRS pattern
-
Look at GOTO Chicago 2019 presentation
-
Read about CQRS in my Microservices patterns book
The services uses the Eventuate Tram framework to communicate asynchronously using events. The flow for publishing a domain event using Eventuate Tram is as follows:
-
Eventuate Tram inserts events into the
MESSAGE
table as part of the ACID transaction that updates the JPA entity. -
The Eventuate Tram CDC service tracks inserts into the
MESSAGE
table using the MySQL binlog (or Postgres WAL) and publishes messages to Apache Kafka. -
A service subscribes to the events, updates its database, and possibly publishes more events.
The following diagram shows the architecture of the Customers and Orders application.
The application consists of three services: Customer Service
, Order Service
, and Order History Service
The Customer Service
implements a REST API for managing customers.
The service persists the Customer
JPA entity in a MySQL/Postgres database.
Using Eventuate Tram
, it publishes Customer
domain events that are consumed by the Order Service
.
The Order Service
implements REST API for managing orders.
The service persists the Order
in MySQL database.
Using Eventuate Tram
, it publishes Order
domain events that are consumed by the Customer Service
.
First, install Node.js modules
npm install
Next, launch the services using Docker Compose:
export DOCKER_HOST_IP=...
./run-services.sh
Note: You need to set DOCKER_HOST_IP
before running Docker Compose.
This must be an IP address or resolvable hostname.
It cannot be localhost
.
See this guide to setting DOCKER_HOST_IP
for more information.
You can use curl
to interact with the services.
First, let’s create a customer:
$ curl -X POST --header "Content-Type: application/json" -d '{
"creditLimit": {
"amount": 5
},
"name": "Jane Doe"
}' http://${DOCKER_HOST_IP}:8082/customers
HTTP/1.1 200
Content-Type: application/json;charset=UTF-8
{
"customerId": 1
}
Next, create an order:
$ curl -X POST --header "Content-Type: application/json" -d '{
"customerId": 1,
"orderTotal": {
"amount": 4
}
}' http://${DOCKER_HOST_IP}:8081/orders
HTTP/1.1 200
Content-Type: application/json;charset=UTF-8
{
"orderId": 1
}
Next, check the status of the Order
in the Order Service
:
$ curl -X GET http://${DOCKER_HOST_IP}:8081/orders/1
HTTP/1.1 200
Content-Type: application/json;charset=UTF-8
{
"orderId": 1,
"orderState": "APPROVED"
}
Finally, look at the customer’s order history in the Order History Service
:
$ curl -X GET --header "Accept: */*" "http://${DOCKER_HOST_IP}:8083/customers/1"
HTTP/1.1 200
Content-Type: application/json;charset=UTF-8
{
"id": 1,
"orders": {
"1": {
"state": "APPROVED",
"orderTotal": {
"amount": 4
}
}
},
"name": "Chris",
"creditLimit": {
"amount": 100
}
}
Don’t hesitate to create an issue or see