Overview of microservices in NestJS

Table of contents

Introduction to microservices.

Microservices - concept is not new. Each service is self-contained and should implement a single business function (service isolation). This design principle is a differs from monolithic architecture, where all services of an app are tightly integrated and deployed as a single unit. Although I've seen a movement in recent years to get back to monolithic architecture. The old is new again. Now let's review the advantages and disadvantages of micro services.

Advantages of microservices.

  • Scalability - If one service experiences higher traffic or system load, that service can be scaled independently without impacting the rest of the application. The service is containerized so it can be scaled easily by kubernetes or serverless environment. You can check out guidelines on how to run your application on Google Cloud Run here.
  • Resilience - If one of your services is down, then it doesn't bring the whole app down.
  • Technology agnostic - services can be built with any stack and deployed into production rather easily. This generally considered an advantage, but most of the time you'd find that managers seldom buy-in to this due to business risk of talent churn to maintain the service later on.
  • Organizational alignment - microservices can be aligned with organizational structure, allowing small, autonomous teams to be responsible for specific services. Generally this works and makes a worth while in a larger organization.
  • Maintainability - Smaller, focused code bases are easier to understand and maintain. This can improve productivity and quality of the code. They are great until cross repository code reviews are required.

Disadvantages of microservices.

  • Requires DevOps culture - all though it's been a buzz word for a while some teams still struggle with DevOps principles.
  • Network latency - this is an extra problem you have to account for when engineering high performing application. Human perception of fast application is around 200ms. Database, 3rd party api, internal service api calls it adds up.
  • Observability - it can be harder to setup proper observability for a each micro service. Even harder (and more costly) to have a birds eye view across services and requests.

The architecture of microservices offers a compelling approach to building scalable, resilient, and maintainable applications, especially for large organizations with complex systems, despite disadvantages it offers.

Challenges of reusing services across projects in microservice architecture.

The micro service achitecture comes with a set of challenges should be kept in mind before making this choice.

  • API stability and versioning - When services are reused across multiple projects, managing versions and other changes becomes challenging. Rolled out changes must not break existing clients. This problem can be prevented by utilizing integration (e2e) tests.
  • Availability - Since services are reused it can be hard to gauge the need to the service and impact if it goes down.
  • Code reviews (assuming the each service code is in separate git repository) - It can be hard to cross review multiple services for changes and roll out changes simultaneously. This can result in a project disruption and cause partial downtime.
  • Service granularity - it can also be challenging to decide for service granularity. Too granular service will become a burden to maintain, too broad - will end up doing many things.
  • Architecting it "right" - sometimes microservice projects end up as monolith just sewn together through HTTP.
  • Configuration management - service configuration, environment variables, versions of environment variables etc. This usually is solved by using some kind of secret manager.
  • Code/configuration duplication - container files (dockerfiles) will be duplicated, there is boiler plate code that comes with each service as a default (eg. framework, dependency versions).

Bird's eye view of NestJS

NestJS is a progressive Node.js framework designed for building efficient, reliable, and scalable server-side applications. NestJS provides a level of abstraction above Node.js, while still allowing developers the freedom to access the underlying platform feature. It's architecture is inspired by Angular and it uses modules, services, and decorators, making it a familiar for developers with an Angular background.

The framework's modular structure is one of its defining characteristics, enabling developers to organize code into separate modules that can encapsulate a related set of capabilities.

Significant feature of NestJS is its support for different types of communication protocols. Out of the box, it allows developers to create REST APIs, GraphQL APIs, and even microservices with various transport layers, such as TCP, Redis, and RabbitMQ. This means that NestJS is suitable for server-side casual tasks, from serving web applications to handling back-end services and microservices architecture.

NestJS stands out as a comprehensive framework for building server-side applications with Node.js. It crossed 1.5M downloads in NPM.

Conclusion


In conclusion - we explored the concept of microservices, their advantages, and the challenges associated with reusing them across projects. We emphasized that microservices offer scalability, resilience, technological flexibility, organizational alignment, and maintainability, but also come with challenges like requiring a DevOps culture, network latency, observability, and complexities in API stability, service granularity, and configuration management. As well walked briefly what NestJS is. Its modular structure, inspired by Angular, allows for the efficient organization of code into encapsulated modules. NestJS's support for various communication protocols, including REST, GraphQL, and several transport layers, makes it ideal for building scalable server-side applications. NestsJS became quite popular over the recent years with it's 1.5M weekly downloads on NPM.