Microservices Architecture

Building Scalable REST APIs with Node.js

A comprehensive guide to designing and implementing high-performance microservices using modern development practices and containerization.

System Architecture

🌐 API Gateway

Request routing, authentication, and rate limiting

⚡ Microservices

Independent, scalable business logic containers

🗄️ Database Layer

MongoDB, Redis, and PostgreSQL data persistence

📨 Message Queue

Async communication via RabbitMQ and Kafka

🔍 Monitoring

ELK stack for logging and Prometheus metrics

🔒 Security

JWT authentication and OAuth2 integration

Express.js Service Example

user-service/index.js
const express = require('express');
const mongoose = require('mongoose');
const User = require('./models/User');

const app = express();
app.use(express.json());

// Create new user endpoint
app.post('/api/users', async (req, res) => {
  try {
    const user = new User(req.body);
    await user.save();
    res.status(201).json(user);
  } catch (error) {
    res.status(400).json({ error: error.message });
  }
});

app.listen(3001, () => console.log('User service running'));

REST API Endpoints

GET /api/users
Retrieve all users with pagination
POST /api/users
Create a new user account
GET /api/users/:id
Get user details by ID
PUT /api/users/:id
Update user information
DELETE /api/users/:id
Delete user account
Node.js
Express.js
MongoDB
Docker
Kubernetes

Container Deployment

$ docker build -t user-service .
Step 1/8 : FROM node:16-alpine
Step 2/8 : WORKDIR /app
Step 3/8 : COPY package*.json ./
Successfully built 2a8f9c7e1b34
$ kubectl apply -f deployment.yaml
deployment.apps/user-service created
service/user-service created
$ kubectl get pods
NAME READY STATUS RESTARTS
user-service-7d4f8b9c6d-x7m2p 1/1 Running 0
user-service-7d4f8b9c6d-z9k5n 1/1 Running 0

Automated CI/CD pipeline with GitHub Actions, Docker, and Kubernetes orchestration for zero-downtime deployments.