Overview
The API Gateway serves as the central entry point for all notification requests in the distributed notification system. Built with NestJS and TypeScript, it handles request validation, rate limiting, correlation tracking, and intelligent routing to downstream services.Purpose and Responsibilities
- Request validation - Validates incoming notification requests using class-validator DTOs
- Rate limiting - Protects downstream services with Redis-backed throttling
- Correlation tracking - Assigns unique correlation IDs to every request for distributed tracing
- Service orchestration - Fetches user and template data from respective services
- Message routing - Routes notifications to RabbitMQ queues based on notification type
- API documentation - Provides Swagger/OpenAPI documentation at
/api
Tech Stack
- Framework: NestJS 10.x
- Language: TypeScript 5.x
- Message Broker: RabbitMQ (via amqp-connection-manager)
- Cache/Rate Limiting: Redis (ioredis)
- Validation: class-validator, class-transformer
- API Documentation: Swagger/OpenAPI
- HTTP Client: Axios (for service-to-service communication)
Configuration
Port: 8000 (default 3000) Environment Variables:Key Features
1. Rate Limiting
The gateway implements Redis-backed rate limiting to protect downstream services from overload.Rate limits are configurable via
THROTTLE_TTL and THROTTLE_LIMIT environment variables.
Default: 10 requests per 60 seconds.2. Correlation IDs
Every request is assigned a unique correlation ID for distributed tracing across services. Implementation (main.ts:26-31):X-Correlation-Id header, or one will be auto-generated.
3. Request Validation
All incoming requests are validated using DTOs with class-validator decorators. Notification Request DTO (dto/create-notification.dto.ts:29-84):4. Service Orchestration
The gateway fetches user and template data in parallel before routing to queues. Parallel Data Fetching (notifications.service.ts:26-29):5. RabbitMQ Routing
Notifications are routed to different queues based onnotification_type.
Exchange Configuration:
- Exchange:
notifications.direct - Type: Direct
- Routing Keys:
email,push
Messages are published with
deliveryMode: 2 to ensure persistence and survive broker restarts.API Endpoints
POST /api/v1/notifications
Queue a notification for delivery. Request Body:GET /health
Health check endpoint for monitoring and orchestration. Response (200 OK):GET /api
Swagger UI for interactive API documentation.Dependencies on Other Services
User Service (Port 8001)
The gateway calls the User Service to fetch user data and preferences. Endpoint:GET /api/v1/users/:id
Implementation (services/user.service.ts):
Template Service (Port 8002)
The gateway calls the Template Service to fetch notification templates. Endpoint:GET /api/v1/templates/:code
Implementation (services/template.service.ts):
RabbitMQ Message Broker
The gateway publishes messages to RabbitMQ for asynchronous processing by email and push services. Queues:email.queue- Consumed by Email Servicepush.queue- Consumed by Push Service
Logging and Observability
Logging Interceptor
The gateway includes a global logging interceptor for request/response logging. Implementation (main.ts:41):Correlation ID Tracking
All logs include correlation IDs for tracing requests across services.Running the Service
Development
Production
Docker
Error Handling
The gateway handles errors gracefully and returns structured error responses. Example Error Handling (notifications.service.ts:57-64):Performance Considerations
- Parallel requests: User and template data are fetched concurrently
- Connection pooling: RabbitMQ and Redis use connection managers for efficiency
- Rate limiting: Protects downstream services from overload
- Persistent connections: RabbitMQ publisher maintains persistent channel
Related Services
- User Service - User management and preferences
- Template Service - Notification template management
- Email Service - Email delivery processing
- Push Service - Push notification delivery