resilient-nestjs-patterns

Implements five NestJS architectural patterns for service layering, resilience, and event-driven design.

Updated Jun 14, 2026
One-click install
npx skills add https://github.com/ironkid90-s/lucky5-v7 --skill resilient-nestjs-patterns-ironkid90-s
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: resilient-nestjs-patterns
Source: https://github.com/ironkid90-s/lucky5-v7/tree/main/.ptah/skills/resilient-nestjs-patterns
Command: npx skills add https://github.com/ironkid90-s/lucky5-v7 --skill resilient-nestjs-patterns-ironkid90-s

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? NestJS backends often degrade as they grow: controllers accumulate business logic, services become god classes with dozens of unrelated methods, external API calls fail silently, and side-effects like emails block HTTP responses. This Skill provides five composable architectural patterns that restructure a NestJS codebase for maintainability and resilience. ## Core Features & Use Cases - Service Orchestration & Domain Layering: Decompose god services into focused child services coordinated by a thin orchestrator, and enforce Controller-Service-DbService separation so HTTP, business rules, and Prisma data access never mix. - Retry, Fallback & Circuit Breaker: Wrap external API calls with exponential backoff retry, local-database fallback strategies, and opossum-based circuit breakers for high-volume integrations. - Event-Driven Architecture & Dynamic Modules: Decouple side-effects (emails, cache invalidation, audit logs, SSE broadcasts) using NestJS EventEmitter, and build configurable modules with forRoot/forTesting patterns and environment-driven provider selection. - Use Case: A subscription billing module where the controller stays thin, the service orchestrates payment API calls with retry, database writes run in transactions via a DbService, and welcome emails plus SSE updates fire as async event listeners. ## Quick Start Ask the AI to refactor a fat NestJS service into the orchestrator pattern with Controller-Service-DbService layering and retry logic for its external API calls.

Frequently Asked Questions about resilient-nestjs-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I split a large NestJS service into smaller services?

Use the Service Orchestration pattern: group methods into clusters by concern, extract each cluster into its own injectable child service, and convert the original service into a thin orchestrator that delegates. Apply this when the file exceeds 500 lines or has more than 6 constructor dependencies.

How to implement retry with exponential backoff in NestJS?

Wrap external calls in a withRetry utility that doubles the delay each attempt, starting from a base delay like 1000ms with a maximum of 3 attempts. Only retry transient errors such as timeouts and HTTP 408, 429, 500, 502, 503, and 504 status codes.

Should NestJS controllers call Prisma directly?

No. Use the 3-tier layering pattern: controllers handle only HTTP concerns, services contain business rules, and a dedicated DbService owns all Prisma queries and transactions. This keeps business logic testable and prevents transaction management from scattering across controllers.

When should I use NestJS EventEmitter instead of direct service calls?

Use events for consequences like sending emails, invalidating caches, or writing audit logs that must not block the response. Use direct calls when the result is needed for the operation to complete, such as validating a payment before creating a subscription.

What is the forRoot and forTesting pattern in NestJS dynamic modules?

It is a static factory method pair on a module: forRoot wires real providers using environment configuration, while forTesting registers mock implementations of external clients. This lets integration tests import the same module with controlled dependencies via Test.createTestingModule.

When should I not split a NestJS service into smaller services?

Avoid splitting when the service is under 200 lines, all methods share the same dependencies, or tests are already easy to write. Premature decomposition of a small service adds file overhead without improving maintainability.