How to Build Microservices with Node.js, Step by Step
Building microservices with Node.js means splitting an app into small services, each with its own process and database. Here is how to design the split.
On this page
Short answer
Building microservices with Node.js means splitting an application into small, independent services, each running its own Node.js process, owning its own data, and talking to the other services over HTTP or a message queue. Each service does one job well and can be deployed, scaled and replaced on its own.
What does building microservices with Node.js mean?
Building microservices with Node.js means splitting an application into small, independent services, each running its own Node.js process, owning its own data, and talking to the other services over HTTP or a message queue. Each service does one job well and can be deployed, scaled and replaced on its own.
Node.js fits this shape because it is light and fast to start, it handles many simultaneous connections with a single-threaded event loop, and the same language runs on the front end and the back end. A team can ship a service as a small Express or NestJS application, put it in a container, and scale it by running more copies.
The trade is complexity. A single application has one codebase and one database; a microservices system has many of both, plus the network between them. That is why the first decision is not which framework to use, but whether the system needs microservices at all. For the definition, see what microservices are.
When do microservices fit, and when do they not?
Microservices fit when a system has parts that change at different speeds, scale at different rates, or belong to different teams. They do not fit a small product where one team ships one codebase and the whole system changes together.
| Situation | Microservices fit? | Why |
|---|---|---|
| Large product, several teams | Yes | Teams ship independently without blocking each other |
| One part needs heavy scale | Yes | Scale only the hot service, not the whole app |
| Different parts need different tech | Yes | Each service picks its own stack |
| Small team, one product | No | The network and operations cost outweighs the benefit |
| Unclear boundaries | No | Wrong splits create chatty services and shared data |
| Need for strong consistency | Careful | Cross-service transactions are hard to get right |
A useful test is the team test: if one team owns the whole system and deploys it together, a modular monolith is usually faster. If three teams step on each other's deploys, splitting the most independent part into a service is a reasonable first move. For the monolith comparison, see microservices vs monolithic architecture.
How do you design the service boundaries?
You design service boundaries around business capabilities, not around technical layers. A service should own one capability end to end: its API, its logic and its data. The test is whether the service can change without forcing a change in another service.
Start from the domain. List the capabilities the product needs, such as users, orders, payments, inventory and notifications. Group the capabilities that change together and split the ones that change apart. Each group becomes a service candidate, and each candidate gets its own database, so no service reads another service's tables directly.
Keep the boundaries coarse at first. Two or three services are easier to run than ten, and a service that is too small becomes a remote procedure call in disguise. A common first split is to pull out the part that changes most often or scales hardest, such as notifications or search, and leave the rest in the core application.
How to build microservices with Node.js, step by step
Building microservices with Node.js follows a steady sequence: define the boundary, build one service well, then add the platform around it. The steps below assume a small system of two or three services.
- Pick the first service. Choose the most independent capability, such as notifications, and define its API contract first.
- Scaffold the service. Use Express for a small service or NestJS for a larger one, with TypeScript, structured logging and a health endpoint.
- Give it its own database. One service, one database. Other services read its data only through its API.
- Add service discovery. Register services so callers find them by name, not by hard-coded address, and add retries with backoff.
- Put a gateway in front. Route external traffic through an API gateway that handles authentication, rate limits and routing.
- Add observability. Give every request a correlation id, collect logs centrally, and trace calls across services.
- Containerise and deploy. Ship each service as a container image and deploy it independently, with its own pipeline.
Communication between services usually starts with synchronous HTTP calls and moves to asynchronous messaging as the system grows. A message queue, such as RabbitMQ or Kafka, lets a service publish an event and carry on, while other services react in their own time. That decoupling is what makes the system resilient when one service is slow or down.
How do services talk to each other and share data?
Services talk to each other through APIs and events, and they share data by publishing it, not by sharing a database. The two patterns cover different needs, and most systems use both.
Synchronous calls over HTTP are the simplest pattern: one service calls another's endpoint and waits for the answer. They fit requests that need an immediate answer, such as checking stock before an order is placed. Their weakness is coupling: if the called service is down, the caller fails too, so keep the call graph shallow and add timeouts and circuit breakers.
Asynchronous events are the resilient pattern: a service publishes an event, such as an order being placed, and other services react to it. The order service does not wait for the notification service, so a slow notification cannot block checkout. Events also give the system a history, which helps debugging and lets new services replay past events to build their state. For the runtime side, see what Node.js is.
Example (hypothetical): an online store splits its checkout
Example (hypothetical): an online store runs one Node.js application that handles the catalogue, the cart, orders and notifications. The team decides to split out notifications first, because it changes often and the marketing team wants to ship it without waiting on the core team.
The team builds a notifications service with Express and TypeScript, gives it its own database, and defines its API: one endpoint to send a message and one to list a user's messages. The order service publishes an order-placed event to a queue, and the notifications service reads the queue and sends the email. The core application no longer waits for email delivery, so checkout gets faster.
The team measures the split. Before the split, a checkout made 4 calls in sequence, each taking 25 ms, for a total of 100 ms. After the split, the order service publishes one event and returns, and the notification happens in the background, so the user-facing part takes about 25 ms. Over 1,000 checkouts a minute, that saves 1,000 x 75 ms = 75 seconds of user-facing waiting per minute.
What are the common mistakes?
The most common mistakes with Node.js microservices are splitting too early, sharing a database, and letting services call each other in deep chains.
- Splitting too early. A small product with unclear boundaries becomes many services that change together, which is the worst of both worlds.
- Sharing a database. Two services reading the same tables are one service with extra network hops. Each service needs its own data.
- Deep call chains. A request that crosses four services in sequence fails when any one of them is slow. Keep the graph shallow.
- No timeouts or retries. A service without a timeout waits forever. Set timeouts, retry with backoff, and break the circuit after repeated failures.
- Skipping observability. Without a correlation id and central logs, a failure across three services is nearly impossible to trace.
- One deploy for all services. If services must deploy together, they are not independent. Give each service its own pipeline.
For the wider build decision, see custom software development for how teams usually scope this work.
Key takeaways
- Split by business capability, not by technical layer
- One service, one database; share data through APIs and events
- Start with the most independent service, not the whole system
- Use an API gateway for auth, routing and rate limits
- Give every request a correlation id and trace it across services
- A change needing a joint deploy means the boundary is wrong
Frequently asked questions
What is a microservice?
A microservice is a small, independent service that does one job, runs its own process, owns its own data and talks to other services over a network. A system built this way is a set of services rather than one large application.
Why use Node.js for microservices?
Node.js is light and fast to start, handles many simultaneous connections with a single-threaded event loop, and shares a language with the front end. A service can be a small Express or NestJS application shipped as a container.
Should I use Express or NestJS?
Use Express for a small service where you want control and few opinions. Use NestJS for a larger service where structure, dependency injection and built-in patterns for messaging and validation save time as the team grows.
How do microservices communicate?
Through synchronous HTTP calls for requests that need an immediate answer, and through asynchronous events on a message queue for work that can happen in its own time. Most systems use both patterns.
Do microservices share a database?
No. Each service owns its own database, and other services read its data only through its API. Sharing tables couples services together and removes the independence that microservices exist to provide.
When should I not use microservices?
When one small team owns the whole system and deploys it together, a modular monolith is usually faster. Split only when teams step on each other's deploys, or when one part needs to scale far beyond the rest.