|
| 1 | +# Transactional Outbox Pattern |
| 2 | + |
| 3 | +An example of the transactional outbox pattern. The project was implemented as part of my blog post [here](), where you |
| 4 | +can find more details about the pattern and implementation. |
| 5 | + |
| 6 | +## Project structure |
| 7 | + |
| 8 | +The project consists of the following modules: |
| 9 | +1) [domain-events](./domain-events). Domain events shared between different services. |
| 10 | +2) [employee-service](./employee-service). Produces domain events reliably using the transactional outbox pattern |
| 11 | + and then forwards them to an SNS FIFO topic. Uses employees as an example. |
| 12 | +3) [consumer-service](./consumer-service). It reads the published domain events by polling its own SQS FIFO queue. This |
| 13 | + could be any service interested in the events published by the `employee-service`. |
| 14 | + |
| 15 | +## Architecture |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +* Employee Service inserts, updates or deletes employee entities and inserts generated domain events in the outbox table in a transaction. |
| 20 | +* Employee Service polls the outbox table and forwards events to an SNS topic. I assumed that the order of events matters, so |
| 21 | + I used an SNS FIFO topic. Also, no two instances can read the outbox table at the same time. |
| 22 | +* Each consumer service has its own SQS FIFO. |
| 23 | +* SQS FIFO subscribes to the employee SNS FIFO. A filter can be used to only subscribe to specific event types. |
| 24 | +* Consumer Service polls its own queue and uses the domain events. |
| 25 | + |
| 26 | +## Running locally |
| 27 | + |
| 28 | +You can run the project locally using docker. The configuration provided matches the |
| 29 | +above architecture diagram. I used localstack, so no need for an AWS account to run the project. |
| 30 | + |
| 31 | +You first need to compile and package all modules. |
| 32 | +```shell |
| 33 | +mvn clean package -Dmaven.test.skip=true |
| 34 | +``` |
| 35 | + |
| 36 | +Then you can build and start services in docker. |
| 37 | +```shell |
| 38 | +docker-compose up --build |
| 39 | +``` |
| 40 | + |
| 41 | +All services should be up and running. |
| 42 | + |
| 43 | +## Testing locally |
| 44 | + |
| 45 | +Now that all services are running locally we can do some manual testing. |
| 46 | + |
| 47 | +We can create an employee. |
| 48 | +```shell |
| 49 | +curl --header "Content-Type: application/json" \ |
| 50 | + --request POST \ |
| 51 | + --data '{ "firstName": "Ioannis", "lastName": "Ioannou", "email": "[email protected]" }' \ |
| 52 | + http://localhost:8001/employee |
| 53 | +``` |
| 54 | + |
| 55 | +The `consumer-service-1` logs that it received the domain event `EmployeeCreated`. |
| 56 | + |
| 57 | +Now if we delete the employee using the UUID from the response. |
| 58 | +```shell |
| 59 | +curl --request DELETE http://localhost:8001/employee/8741749a-43f0-4463-a662-2ee693de749e |
| 60 | +``` |
| 61 | + |
| 62 | +The `consumer-service-2` logs that it received the domain event `EmployeeDeleted`. |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
0 commit comments