Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
75 lines (46 sloc) 2.85 KB

Outbox Pattern

This example demonstrates the "outbox pattern", an approach for letting services communicate in an asynchronous and reliable fashion. It accompanies https://debezium.io/blog/2019/02/19/reliable-microservices-data-exchange-with-the-outbox-pattern/[this post] on the Debezium blog.

The sending service ("order-service") produces events in an "outbox" event table within its own local database. Debezium captures the additions to this table and streams the events to consumers via Apache Kafka. The receiving service ("shipment-service") receives these events (and would apply some processing based on them), excluding any duplicate messages by comparing incoming event ids with already successfully consumed ids.

Update, March 3, 2019: Another variant of the receiving service has been added, "shipment-service-quarkus", which is functionally the same as the original one, but is implemented using the https://quarkus.io[Quarkus] stack. This allows to build a native binary of that service, resulting in significantly less memory usage and faster start-up than the classic version (based on Thorntail).

Execution

Prepare the Java components:

mvn clean install -Pnative -Dnative-image.docker-build=true

Start all components:

export DEBEZIUM_VERSION=0.9
docker-compose up --build

Register the Debezium Postgres connector:

cat register-postgres.json  | http POST http://localhost:8083/connectors/

Place a "create order" request with the order service:

cat resources/data/create-order-request.json | http POST http://localhost:8080/order-service/rest/orders

Cancel one of the two order lines:

cat resources/data/cancel-order-line-request.json | http PUT http://localhost:8080/order-service/rest/orders/1/lines/2

Examine the events produced by the service via the Apache Kafka console consumer:

docker run --tty --rm \
    --network outbox_default \
    debezium/tooling:1.0 \
    kafkacat -b kafka:9092 -C -o beginning -q \
    -t OrderEvents | jq .

Examine that the receiving service processes the events:

docker-compose logs -f shipment-service

(Look for "Processing '{OrderCreated|OrderLineUpdated}' event" messages in the log)

Useful Commands

Getting a session in the Postgres DB of the "order" service:

docker run --tty --rm -i \
    --network outbox_default \
    debezium/tooling:1.0 \
    bash -c 'pgcli postgresql://postgresuser:postgrespw@order-db:5432/orderdb'

E.g. to query for all purchase orders:

select * from inventory.purchaseorder po, inventory.orderline ol where ol.order_id = po.id;

Getting a session in the MySQL DB of the "shipment" service:

docker run --tty --rm -i \
    --network outbox_default \
    debezium/tooling:1.0 \
    bash -c 'mycli mysql://mysqluser:mysqlpw@shipment-db:3306/inventory'

E.g. to query for all shipments:

select * from Shipment;
You can’t perform that action at this time.