question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Microservices under the hood

See original GitHub issue

Explanation/Documentation request

I know that such type of questions need to ask in stackoverflow, but I think another people can’t explain such as it can do the creator 😉

Can you explain all topology of microservices ? Maybe in schemas, docs or text message. What is under the hood? How it works?

Does we need to create separate main.ts and microservice-la-la.ts for different instances ? And what the difference when we connect microservice to main application instance and have several independent instances of services? Does it create child process/cluster/worker/etc ? What will be if my connected microservice will block event loop? Will it affect the main application?

Can you share your vision of the best practices?

I think this is very-very powerful tool of nest, but the docs are scarce. It’s describe only exists transports and a very little part of concept.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
BrunnerLiviocommented, Aug 7, 2019

@ed-fruty Kamil is currently busy, so I try to answer some of your questions - but Kamil is definitely the person who can give you the best answer.

Does it create child process/cluster/worker/etc ?

We do not do that out of the box. We leave this task to the user, since it is too general on how you want to orchestrate your application.

Does we need to create separate main.ts and microservice-la-la.ts for different instances ? And what the difference when we connect microservice to main application instance and have several independent instances of services?

Combining your normal HTTP application with a microservice (NestApplication.connectMicroservice) brings on one hand the value of sharing common functionality between a microservice instance, but also allows to communicate using the microservice transport strategies (TCP, Redis, MQTT, etc.) from e.g. a HTTP Controller. Important to note is that this all runs in the same node instance. Read more about that in the Hybrid Application chapter.


Maybe an example I have recently used may give you more insights on how to structure your app. The following example is just how I use it. It definitely varies from your personal preferences and use-cases. My application consists of two main parts:

  • REST API Consists of normal crud operations. Also triggers a database synchronization using TCP through a route POST api/v1/sync.
  • A service for database synchronization Listen to TCP messages, which runs a synchronization task.

The REST API is being create using the “normal” NestFactory.create(). It also connects to a microservice and sets the TCP connection up using NestApplication.connectMicroservice({ tcpOptions }). I then inject the TCP Client into my sync controller. The @Post method of the controller will send then a message to my database sync service using TCP and waits for a response.

The database synchronization service is a microservice app using NestFactory.createMicroservice(). This is all in a dedicated main.ts file, totally separated from the REST APIs main.ts. This allows me to spawn a separated node process, so it is easier to manage using Docker / Kubernetes. The synchronization service application only consists of a @Controller which listens to TCP messages from the REST API. See here. The service will just run the task once a message is being received. The task itself could be refactored into an own worker application, which then could be executed as a child process from the service.

The best way to orchestrate this whole architecture would definitely be a monorepo setup. For each application you would have an own package. I use this boilerplate for my monorepo setups though you can also use nrwl/nx

packages
|- api
|  |- src
|  |  |- main.ts
|  |  |- Dockerfile
|- common
|  |- src
|  |  |- index.ts // Just common modules/functionalities, so no need to have a dedicated Nest app
|- sync
|  |- src
|  |  |- main.ts
|  |  |- Dockerfile
docker-compose.yml

This may give you a bit more guidance how the microservices architecture of Nest is meant to be used. As mentioned, it is hard to create a chapter on this topic, since it is quite opinionated. May a blog post would be feasible…

0reactions
lock[bot]commented, Nov 16, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Under the hood of Kubernetes and microservices
The main idea of microservices is to break down your business logic and data access layers into independent modules which are also known...
Read more >
Microservices Architecture – Explained in Plain English
61% of companies have been using microservices in the last year ... even if you don't understand all the technical details under the...
Read more >
Istio Service Mesh Pattern
Microservices communicate in 3 different ways. Ingress traffic (requests coming from external clients); Inter service traffic (requests ...
Read more >
Under the hood of the cCube black box there is a ...
Here, we propose a portable ML microservice framework Minerva (microservices container for applied ML) as an efficient way to modularize and deploy intelligent ......
Read more >
How to Set up a Microservices Architecture in Ruby - Toptal
Microservices are one of the latest trends in software design where multiple independent services communicate among themselves and have their own processes and ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found