Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
D

ddd-hexagonal-cqrs-es-eda

> 后端框架
Open source

Complete working example of using Domain Driven Design (DDD), Hexagonal Architecture, CQRS, Event Sourcing (ES), Event Driven Architecture (EDA), Behaviour Driv

1.4K stars0 likes0 views
WebsiteGitHub

About

Complete working example of using Domain Driven Design (DDD), Hexagonal Architecture, CQRS, Event Sourcing (ES), Event Driven Architecture (EDA), Behaviour Driv

ddd-hexagonal-cqrs-es-eda

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.

Table of Contents

  • I. Introduction
    • Overview
    • Todo application business requirements
  • II. Technologies and Technical Features
    • Technical Features
    • Technologies Used - Overview
  • III. Quick start - running the ToDo App
    • Prerequisites
    • Running the app
  • IV. Design Process and Decisions
    • Design Process - Event Storming
    • Design Decisions
  • V. Running in development mode
    • A. Project Setup
    • B. Application Validation
    • C. Understanding the project structure
  • VI. Conclusion
    • ❓ Questions

I. Introduction

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.

Overview

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:

  • Hexagonal Architecture (or Ports and adapters)
  • Domain Driven Design (DDD) and its tactical patterns
  • Behaviour Driven Development (BDD)
  • Event Driven Architecture (EDA)
  • Command and Query Responsibility Segregation (CQRS)
  • Eventual consistency
  • Event Storming

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.

Todo application business requirements

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.

II. Technologies and Technical Features

Technical Features

  • Observability
  • Realtime client events
  • Logging
  • Tracing: Tracks requests that span through multiple modules/microservices
  • Easy switching between modular monolith and microservices
  • Authentication
  • Authorization (Even at the repository level)
  • OpenID Connect session renewal
  • Automatic client code generation using OpenAPI
  • Event-sourced Todo aggregate with a transactional outbox

Technologies Used - Overview

Here are listed some of the specific technologies used for the implementation of the project:

  • Authentication: Keycloak through OpenID Connect Authorization Code Flow with PKCE
  • Database - Persistence: PostgreSQL
  • Testing: JEST
  • External Communication Protocols: REST and server-sent events
  • Frameworks: ΝestJS
  • PubSub technology: NATS
  • Message Streaming Technology: JetStream by NATS
  • Container Technology: Docker
  • Tracing-Observability: Jaeger, Grafana
  • Metrics: Prometheus

Persistence and event delivery

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.

III. Quick start - running the ToDo App

Prerequisites

In order to run the application the following should have been installed on your local machine:

  • Docker should be installed (link)
  • docker-compose should be installed, if your docker installation does not install it automatically (link)

Running the app

In order to run the application you need to follow the steps below:

  • Run:
bash
 git clone https://github.com/bitloops/ddd-hexagonal-cqrs-es-eda.git
  • Navigate to the folder:
bash
cd ddd-hexagonal-cqrs-es-eda
  • Run:
bash
docker compose -p bitloops-todo-app up -d

from 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!)

IV. Design Process and Decisions

Design Process - Event Storming

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:

  • IAM: Has todo with the user registration and log in.
  • Todo: This is the core subdomain of our application (see DDD)
  • Marketing: This is a supporting subdomain (see DDD) of our Todo application.

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:

  • User Log In process (IAM Bounded Context)
  • User registration process (IAM Bounded Context)
  • Todo process (Todo Bounded Context)
  • Onboarding process (Marketing Bounded Context)

Design Decisions

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.

V. Running in development mode

A. Project Setup

Prerequisites

In order to run the application the following should have been installed on your local machine:

  • Node.js 24 LTS should be installed (link)
  • Docker should be installed (link)
  • Docker Compose should be installed, if your Docker installation does not include it (link)
  • Corepack should be enabled so the repository can use its pinned pnpm version

Running the app

After all the necessary have been installed on your local machine, you should follow the steps mentioned below to run the application:

  • Run:
bash
 git clone https://github.com/bitloops/ddd-hexagonal-cqrs-es-eda.git
  • Navigate to the folder:
bash
cd ddd-hexagonal-cqrs-es-eda
  • Install the workspace dependencies:

    bash
    corepack enable
    pnpm install --frozen-lockfile
  • Start Docker on your machine.

  • To run the complete application, run:

    bash
    docker compose -p bitloops-todo-app up -d

    The frontend is then available at http://localhost:4173 and the backend at http://localhost:8080.

  • For local backend development, start only its infrastructure dependencies:

Issues· 1 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

TypeScriptbddcqrsddddomain-driven-design

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架