← ~/blog

Microservices với NestJS và Redis: bắt đầu từ một ví dụ nhỏ

· 8 min read · Backend & Microservices

Microservices là gì và khi nào nên tách service. Dựng thử hai service NestJS giao tiếp qua Redis Pub/Sub, kèm vài lưu ý khi kết nối PostgreSQL.

1. Bài toán Monolithic vs Microservices

Khi ứng dụng phát triển lớn (chẳng hạn hệ thống E-commerce với hàng chục ngàn đơn hàng/ngày), kiến trúc Monolithic truyền thống bắt đầu bộc lộ nhược điểm:

  • Một module lỗi (ví dụ service gửi Email/SMS) có thể kéo sập toàn bộ hệ thống Thanh toán.
  • Khó scale riêng lẻ từng phần có traffic cao.

Giải pháp: Event-Driven Architecture với NestJS

Với NestJS Microservices, có thể chuyển từ HTTP REST đồng bộ sang giao tiếp bất đồng bộ qua Redis hoặc RabbitMQ.


2. Setup Redis Microservice trong NestJS

Cấu hình Service chính (API Gateway) gửi event tới Order Service:

typescript
// src/order/order.controller.ts
import { Controller, Post, Body, Inject } from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices';

@Controller('orders')
export class OrderController {
  constructor(
    @Inject('ORDER_SERVICE') private readonly client: ClientProxy,
  ) {}

  @Post()
  async createOrder(@Body() createOrderDto: any) {
    // Emit event bất đồng bộ, không block HTTP response
    this.client.emit('order_created', createOrderDto);
    
    return { 
      status: 'success', 
      message: 'Order received and being processed' 
    };
  }
}

Ở phía Order Processor Service, lắng nghe event order_created qua Redis:

typescript
// src/order/order.processor.ts
import { Controller } from '@nestjs/common';
import { EventPattern, Payload } from '@nestjs/microservices';

@Controller()
export class OrderProcessor {
  @EventPattern('order_created')
  async handleOrderCreated(@Payload() data: any) {
    console.log('Processing order:', data.id);
    // 1. Lưu vào PostgreSQL
    // 2. Trừ tồn kho trong MinIO/Database
    // 3. Emit tiếp event 'order_confirmed'
  }
}

3. Tối ưu hóa Database Pool với PostgreSQL

Khi chạy nhiều instance microservice, mỗi instance đều mở các kết nối tới PostgreSQL. Để tối ưu:

  • Đặt tham số connection_limit phù hợp trong connection string: postgresql://user:password@localhost:5432/mydb?connection_limit=10
  • Sử dụng PgBouncer ở tầng trước PostgreSQL nếu số lượng kết nối đồng thời vượt quá 200.

Bình luận