CodeBlanks Logo Icon CodeBlanks

What are microservices?

Learn the microservice architecture.

What is a microservice?

A microservice is a small, independent service that fulfills a single business capability. Instead of one big application (monolith), each business area becomes its own service: a user service, an order service, a payment service. Each service has its own lifecycle.

In a microservice architecture each service runs as a separate process and communicates via HTTP or a message queue. Advantages: independent deployment, separate scaling, technology choice flexibility. Disadvantages: distributed complexity, network latency, operational burden. Node.js fits microservices well with lightweight HTTP servers and an asynchronous nature. In this group you will learn the core patterns: communication, API gateway, service discovery and circuit breaker.

Monolith vs microservice

A monolith is an architecture where all functions live in a single application. In a microservice, each function is a separate service. A monolith is simple at first, but as it grows, deployment and scaling become harder.

Example: an e-commerce monolith holds user, order and payment code in one application. The microservice equivalent is three separate services. In a microservice each service deploys independently; a change in the order service does not affect the user service. But communication between services happens over the network, which adds latency and failure risk. This is why patterns like circuit breaker, retry and service discovery are needed. Good design balances monolith and microservice.

Practice Write a user service skeleton

Import the express module. Create app. Define app.get("/users", (req, res) => res.json({ servis: "user-service" })). Print "Microservice ready" with console.log.

Starter code

index.js
// Import express and create app
// write here

// Define the "/users" route
// write here

// Print "Microservice ready"
// write here

Sample solution

index.js
const express = require("express");
const app = express();
app.get("/users", (req, res) => res.json({ servis: "user-service" }));
console.log("Microservice ready");

Expected output

Microservice ready

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 How do services communicate in a microservice architecture?

index.js
service A → HTTP → service B
  • Directly via shared memory
  • Via HTTP or a message queue Correct answer
  • Only by writing files
  • Through a shared database

Explanation: Services communicate via HTTP or a message queue.

2 Complete the service communication method

Question
Services communicate via ____ or a message queue.
Answer
Services communicate via HTTP or a message queue.

Explanation: HTTP is common for service communication.

3 Match each concept to its description

Term Meaning
microservice Small service doing one job
monolith Single big application
HTTP Service communication protocol
message queue Async communication channel

All lessons in Advanced