Nâng CaoNestJS iconNestJS

Tối ưu performance NestJS: Fastify, Caching, Compression, Rate Limiting?

Fastify adapter thay Express: dùng FastifyAdapter từ @nestjs/platform-fastify, nhanh hơn 2-3x nhờ JSON serialization tối ưu và request/response lifecycle hiệu quả hơn. Nhưng một số middleware Express không tương thích, cần kiểm tra.

Caching: @nestjs/cache-manager với Redis store qua CacheModule.registerAsync(). @UseInterceptors(CacheInterceptor) tự động cache GET responses, @CacheTTL(seconds) override TTL cho route cụ thể. Programmatic cache với CacheManager.get/set/del.

Compression: app.use(compression()) giảm response size 60-70% cho text content.

Rate Limiting: @nestjs/throttler với ThrottlerModule.forRoot() config nhiều tiers (short/long). APP_GUARD với ThrottlerGuard apply global, @Throttle() override cho route cụ thể, @SkipThrottle() bỏ qua.

DB connection pooling: TypeORM extra.max tăng max connections theo CPU cores. Lazy loading modules với LazyModuleLoader cho microservices để giảm startup time.

Xem toàn bộ NestJS cùng filter theo level & chủ đề con.

Mở danh sách NestJS