Complete working example of using Domain Driven Design (DDD), Hexagonal Architecture, CQRS, Event Sourcing (ES), Event Driven Architecture (EDA), Behaviour Driv
Complete working example of using Domain Driven Design (DDD), Hexagonal Architecture, CQRS, Event Sourcing (ES), Event Driven Architecture (EDA), Behaviour Driv
Complete working example of using Domain Driven Design (DDD), Hexagonal Architecture, CQRS, Event Sourcing (ES), Event Driven Architecture (EDA), Behaviour Driven Development (BDD) using TypeScript and NestJS.
Current release: 1.0.2. See the changelog, the backend architecture, and the Keycloak IAM architecture.
Building complex software is really hard, and we learnt the hard way how important it is to design your software correctly from the beginning!
There is plenty of information out there on how to build resilient and maintainable software, but the difficulty is actually implementing it. So we went ahead and built a comprehensive example we wish we had when we started learning these concepts and technologies.
Our team has put a lot of effort into creating a clean, and modular code-base that comes as close as possible to production ready code, aiming to provide valuable insights into advanced software architecture concepts.
The objective of this project is to provide you a reference implementation on how to design and create maintainable and flexible software applications.
The code is written using Typescript and NodeJS, using the NEST framework, however, the concepts and patterns used are not bound to any specific technologies.
The project includes an over-engineered ToDo app that includes the patterns and principles that are necessary if you want your code to be easy to change, resilient and easy to maintain. Below we provide detailed instructions on how to run it
In addition, you will learn a great deal about software design and architecture patterns and principles such as:
There are many ways to implement these, so we're eager to get your feedback and open to answer any questions you may have. Join our Discord channel if you'd like to exchange some ideas on software design & development or if you have any questions.
The todo application, is basically a simple todo application, with some tweaks.
The users should be able to register to the todo app. After they register, they should be able to login. After logging in, they should be able to add todos, to complete a todo, to uncomplete a todo (in case they made it complete accidentally), as well as modify the todo title. In the whole process they should be able to view his todos.
When a todo is completed, if this is the first completed todo, an email should be sent to the user to congratulate him for completing his first todo. This operation has to do mostly with the needs marketing team.
Here are listed some of the specific technologies used for the implementation of the project:
PostgreSQL is the sole application database. IAM and Marketing use relational tables, while the Todo aggregate is rehydrated from an append-only event stream. Each Todo command writes its events, query projection, and outbox messages in one PostgreSQL transaction. The outbox relay then publishes domain events to NATS JetStream with retry and at-least-once delivery semantics. See the backend architecture for the transaction, concurrency, and consumer guarantees.
In order to run the application the following should have been installed on your local machine:
In order to run the application you need to follow the steps below:
git clone https://github.com/bitloops/ddd-hexagonal-cqrs-es-eda.gitcd ddd-hexagonal-cqrs-es-edadocker compose -p bitloops-todo-app up -dfrom the terminal inside the project in order to download and run the necessary containers.
Then the ReactJS front-end application will be visible at: http://localhost:4173.
The development realm includes [email protected] with password
Todo-Demo-2026!. Keycloak runs at http://localhost:8090; its development
admin credentials are admin / admin-development-only. These credentials
must never be used outside local development.
Frontend React JS application (new version using Vite and Redux!)
We have chosen the Event storming technique to document the functionality and business logic of the todo application.
In general, Event storming is a collaborating modelling technique used to model complex domains, in order to align the software produced with the actual business logic. It matches perfectly with Domain Driven Design (DDD) as well as Event Driven Architecture (EDA).
If you want to know more for this technique, you can check the Theoretical Review at the end.
As you can see after the collaborative discovery, we have identified the following bounded contexts:
In the process we have further split some bounded contexts (linguistic boundary), to more fine grained modules.
The processes of the system as were discovered are the following:
Some parts of the system need to have information which is located in other parts of the system.
More specifically, the Marketing bounded context, needs to know when a todo is completed and run its business logic to send an email when the first todo is completed.
Moreover the Marketing Bounded contexts needs to have information concerning the email of each specific user, in order to be able to send an email.
The problem in this case is that the email information belongs to the IAM bounded context. So in order for the marketing bounded context to have the knowledge of the specific users's email, either it can make a sync request to the IAM bounded context, or it can listen to integration events from the IAM bounded context, in order to hold a local email information (via eventual consistency).
In this project the decision was to keep a local repository in the Marketing bounded context, of the users and their emails, updated by listening to integration events from the IAM bounded contexts (user registered and user email changed).
Keycloak owns credentials and browser sessions, while the IAM bounded context owns the application's user identity. An anti-corruption layer validates OIDC claims and maps Keycloak's issuer/subject pair to an internal UUID. IAM registration and email changes reach Marketing through an application-owned transactional outbox, so downstream contexts do not depend on Keycloak. See the Keycloak IAM architecture.
In order to run the application the following should have been installed on your local machine:
After all the necessary have been installed on your local machine, you should follow the steps mentioned below to run the application:
git clone https://github.com/bitloops/ddd-hexagonal-cqrs-es-eda.gitcd ddd-hexagonal-cqrs-es-edaInstall the workspace dependencies:
corepack enable
pnpm install --frozen-lockfileStart Docker on your machine.
To run the complete application, run:
docker compose -p bitloops-todo-app up -dThe frontend is then available at http://localhost:4173 and the backend at http://localhost:8080.
For local backend development, start only its infrastructure dependencies:
No open issues yet, or sync has not completed.