Recently come across to Microservice architecture and CQRS (command query responsibility segregation), I can understand both concept, but I am confuse when 2 pattern come together, how can we implements this 2 pattern all together?
First of all, based on my understanding, in Layman terms,
Microservice is like break the different part in our app into different 'small app'. For example, our app have Product, Order, Payment, Invoice etc, each of this function can be break it down to separate Nestjs microservices app and host it into different docker container or different server may be? Which stated here https://docs.nestjs.com/microservices/basics we can defined the app like this:
import { NestFactory } from '@nestjs/core';
import { Transport, MicroserviceOptions } from '@nestjs/microservices';
import { ProductModule } from './product.module';
async function bootstrap() {
const app = await NestFactory.createMicroservice<MicroserviceOptions>(
ProductModule,
{
transport: Transport.TCP,
},
);
app.listen();
}
bootstrap();
Then inside this app, it only handle 1 operation, example Product.
Therefore after we have this Product microservice, I can have a Nestjs app named gateway which will redirect the traffic to Product service using MessagePattern, same goes to Order, Invoice, Payment etc. Between Order and Payment, then can communicate using EventPattern Which is basically look like this:
Question 1: So with the structure above, is it each of the service have to host in 5 server? Cause when all of them, lets say 5 docker container host in 1 server, how will this useful? Cause I didnt see the value of this so far.
Next, is about CQRS, which stated documentation here
I tried to implement this, then I will get the file structure like this:
Is basically inside 1 Nestjs app which do this following:
- We have all bunch of different folder here
account,order,users,paymentfor each domain. In picture on haveaccount. - Inside each of domain have 4 layers
domain,applicationinfrastructure,interface - Inside application will have
command,query,sagas. So will monitor the event insidesagasand trigger next actions.
Question 2: But right now, since all that is still inside 1 Nestjs app, so is this consider a Monolith application? Or let say account folder already consider 1 Microservices? I need to be clear for this first.
If the structure in the picture above, is a monolith, I am thinking to break it down to Microservice using Nestjs monorepo mode,
apps
- productService # --> this is 1 microservices
-- src
--- main.ts // as stated here
--- product.controller.ts
--- product.module.ts
--- product.service.ts
- orderService # --> this is 1 microservices
- paymentService # --> this is 1 microservices
libs
- productLibs
-- domain // basically this is the 2nd picture
-- infrastructure
-- application
- orderLibs
- paymentLibs
So when deploy, I will build the docker image like this:
- OrderDockerImage:
apps/orderServiceandlibs - ProductDockerImage:
apps/productServiceandlibs - PaymentDockerImage:
apps/paymentServiceandlibs
Ok, by doing this, I can use the command, query, event of each domainLibs in all different domainService. But right now have a problem:
Question 3: How can I trigger orderService from productService? Let say in the command handler like below:
// Inside Product service in libs
@CommandHandler(ProductItemCommand)
export class ProductItemHandler
implements ICommandHandler<DropAncientItemCommand> {
constructor(
private readonly repository: HeroRepository,
private readonly publisher: EventPublisher,
) {}
async execute(command: DropAncientItemCommand) {
// here how to trigger OrderService?
// with EventPattern or MessagePattern?
// But this is inside `libs` folder
}
}
Question 5: Do we need to use Microservices when we already implement CQRS in an app? If yes, how each microservice share command, query, event with each other? Let say ProductDomain at some point need a command from OrderDomain?
Question 6: Can use CQRS and Microservice together? If yes, what is the benefit on this? In what use cases?
Question 7: What is the different and relationship between Microservice and CQRS? So it is a Microservices vs CQRS relationship or Microservice + CQRS relationship?
Just document all my thinking based on my research. Please point me to a right direction, like industry standard AKA how you all normally do things.

