0
0
NestJSframework~30 mins

Why microservices scale independently in NestJS - See It in Action

Choose your learning style9 modes available
Why microservices scale independently
📖 Scenario: You are building a simple NestJS microservices system for an online store. Each microservice handles a specific part: products and orders. You want to show how each microservice can scale independently by controlling the number of instances for each.
🎯 Goal: Create two NestJS microservices, ProductsService and OrdersService, with separate configurations for scaling. You will set up the basic services, add a configuration for the number of instances, and then apply that configuration to simulate independent scaling.
📋 What You'll Learn
Create a basic NestJS microservice for products
Create a basic NestJS microservice for orders
Add a configuration variable for the number of instances for each service
Use the configuration to simulate scaling by printing the instance count
💡 Why This Matters
🌍 Real World
Microservices allow different parts of an application to grow or shrink separately. For example, if product requests grow faster than orders, you can add more product service instances without touching orders.
💼 Career
Understanding how to configure and scale microservices independently is key for backend developers working with modern distributed systems and cloud deployments.
Progress0 / 4 steps
1
Create basic ProductsService microservice
Create a NestJS microservice class called ProductsService with a method getProducts() that returns an array with the strings 'Apple' and 'Banana'.
NestJS
Need a hint?

Use @Injectable() decorator and create a method that returns the exact array.

2
Create basic OrdersService microservice and add instance count config
Create a NestJS microservice class called OrdersService with a method getOrders() that returns an array with the strings 'Order1' and 'Order2'. Then create two constants: PRODUCTS_INSTANCES set to 3 and ORDERS_INSTANCES set to 5 to represent the number of instances for each service.
NestJS
Need a hint?

Use @Injectable() for OrdersService and create two constants with the exact names and values.

3
Simulate scaling by using instance count in a method
In ProductsService, add a method getInstanceCount() that returns the value of PRODUCTS_INSTANCES. In OrdersService, add a method getInstanceCount() that returns the value of ORDERS_INSTANCES.
NestJS
Need a hint?

Define getInstanceCount() methods that return the constants for each service.

4
Complete by exporting all services and constants
Export ProductsService, OrdersService, PRODUCTS_INSTANCES, and ORDERS_INSTANCES from the module so they can be used elsewhere.
NestJS
Need a hint?

Use a single export statement listing all four identifiers.