Events, HTTP Module & Outbound Resilience
Events, HTTP Module & Outbound Resilience
Not every action should be tightly coupled to a request handler. Domain events decouple internal reactions, while HttpModule provides an injectable Axios client for outbound calls. Together they support clean integrations when paired with timeouts, retries, and idempotency.
Core idea
This feature is about controlling how the application is organized and how it behaves at runtime. These are the points a developer should understand before using it in a real project:
- EventEmitterModule lets one event trigger multiple listeners without the publisher knowing them.
- Events are useful for local side effects such as audit logs, email requests, and cache invalidation.
- HttpModule.registerAsync() configures Axios clients from ConfigService or secret-backed settings.
- Outbound calls need timeouts and controlled retries; never allow a downstream API to block a request indefinitely.
- Use idempotency keys when retrying write operations against external services.
Practical example
The following example shows the idea in a practical NestJS project. The goal is not to memorize the snippet, but to understand where it belongs in the architecture:
Production checklist
- Name events with domain language, not implementation details.
- Keep listeners idempotent when they may be retried manually.
- Set outbound HTTP timeouts globally.
- Wrap external writes with idempotency keys and clear error mapping.
Summary
This lesson covers an advanced NestJS area that matters when building enterprise applications. Focus on clear boundaries, testable behavior, and choosing the right tool for the context instead of using every feature everywhere.