A scalable, microservices-based messaging platform supporting SMS, Email, and WhatsApp channels with CQRS architecture, dynamic rate limiting, and multi-tenant configuration.
- π― Overview
- ποΈ Architecture
- β¨ Features
- π Quick Start
- π¦ Services
- π§ Configuration
- π API Documentation
- π§ͺ Testing
- π Monitoring
- π³ Docker Deployment
- π Security
- π Additional Documentation
- Multi-Channel Messaging: Unified API for SMS, Email, and WhatsApp
- Enterprise Scalability: Handle 10,000+ messages per minute
- Real-Time Tracking: Complete message lifecycle visibility
- Multi-Tenant Architecture: Secure isolation for multiple clients
- Production-Ready: Comprehensive monitoring, error handling, and logging
| Component | Technology | Purpose |
|---|---|---|
| API Gateway | Node.js + Express | Request routing, authentication, dynamic rate limiting |
| Query Service | Node.js + TypeScript | Read operations, data queries |
| Message Orchestrator | Node.js + TypeScript | Write operations, business logic |
| Tenant Config Service | Node.js + TypeScript | Multi-tenant configuration, rate limits |
| Channel Services | Node.js + TypeScript | Provider integrations (Twilio, SendGrid, Meta) |
| Message Broker | Apache Kafka | Event streaming, async processing |
| Database | MongoDB | Message storage, conversation history |
| Service Registry | Node.js + Express | Service discovery, health aggregation |
| Caching | Redis | Session storage, rate limiting, config caching |
| Containerization | Docker + Docker Compose | Development and deployment |
βββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β Clients βββββΆβ API Gateway βββββΆβ Query Service β
β β β (Port 3002) β β (Port 3200) β
βββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β β
β βΌ
βββββββββββββββββββ βββββββββββββββββββ
β Tenant Config β β MongoDB β
β Service (3300) β β (Port 27017) β
βββββββββββββββββββ βββββββββββββββββββ
β β
βΌ βΌ
βββββββββββββββββββ ββββββββββββ
β Kafka β β Redis β
β (Port 9092) β β (Cache) β
βββββββββββββββββββ ββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Channel Services β
βββββββββββββββββββ¬ββββββββββββββββββ¬ββββββββββββββββββββββ€
β SMS Service β Email Service β WhatsApp Service β
β (Port 3005) β (Port 3003) β (Port 3004) β
βββββββββββββββββββ΄ββββββββββββββββββ΄ββββββββββββββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββ βββββββββββββββ βββββββββββββββββββ
β Twilio β β SendGrid β β Meta WhatsApp β
β API β β API β β API β
βββββββββββββββ βββββββββββββββ βββββββββββββββββββ
- Write Path: API Gateway β Kafka β Message Orchestrator β MongoDB
- Read Path: API Gateway β Query Service β MongoDB
- Event Path: External Webhooks β API Gateway β Kafka β Status Updates
- β Multi-Channel Support: SMS, Email, WhatsApp in unified API
- β Bulk Messaging: Send up to 10,000 messages in single request
- β Real-Time Tracking: Message status and delivery events
- β Rich Content: HTML emails, media attachments, templates
- β Conversation Management: Thread messages by conversation
- β Multi-Tenant Architecture: Complete tenant isolation
- β JWT Authentication: Secure token-based auth with scopes
- β Dynamic Rate Limiting: Tenant-specific, channel-based rate limits
- β Service Tiers: Free, Starter, Pro, Enterprise with different limits
- β Configuration Management: Centralized tenant configuration service
- β Health Monitoring: Comprehensive health checks and metrics
- β Circuit Breakers: Automatic failover and service protection
- β RESTful APIs: Intuitive, well-documented endpoints
- β Docker Compose: One-command development setup
- β Comprehensive Testing: Postman collection with 40+ test cases
- β Structured Logging: Correlation IDs and detailed error tracking
- β Service Discovery: Automatic service registration and health checks
- Docker & Docker Compose (Latest)
- Node.js 18+ (for local development)
- Git
- Postman (for API testing)
git clone <repository-url>
cd messaging-platform# Copy environment templates
cp api-gateway/.env.example api-gateway/.env
cp query-service/.env.example query-service/.env
# Edit environment files with your configuration
# See Configuration section for details# Start all services in development mode
docker-compose up -d
# View service logs
docker-compose logs -f api-gateway
docker-compose logs -f query-service
# Check service status
docker-compose ps# Check API Gateway health
curl http://localhost:3002/health
# Check Query Service health
curl http://localhost:3200/api/v1/health
# Check Tenant Config Service health
curl http://localhost:3300/health
# View Kafka topics (optional)
curl http://localhost:8080- Import
Messaging-Platform-Complete.postman_collection.json - Set collection variables:
api_gateway_url:http://localhost:3002query_service_url:http://localhost:3200tenant_config_service_url:http://localhost:3300tenant_id:your-tenant-id
- Run the "Health Checks" folder to verify everything is working
- Try sending a test message with "Send SMS Message"
| Service | Status | Port | Health Check | Description |
|---|---|---|---|---|
| API Gateway | β Production Ready | 3002 | /health |
Main entry point, routing, auth, dynamic rate limiting |
| Query Service | β Production Ready | 3200 | /api/v1/health |
Read operations, message queries |
| Message Orchestrator | 9090 | /health |
Write operations, business logic | |
| Tenant Config Service | β Production Ready | 3300 | /health |
Multi-tenant configuration, rate limits |
| SMS Service | 3005 | /health |
Twilio integration | |
| Email Service | 3003 | /health |
SendGrid integration | |
| WhatsApp Service | 3004 | /health |
Meta WhatsApp integration | |
| Service Registry | β Production Ready | 3100 | /health |
Service discovery |
| Service | Port | Purpose | Management UI |
|---|---|---|---|
| MongoDB | 27017 | Message storage | - |
| Kafka | 9092 | Event streaming | http://localhost:8080 (Kafka UI) |
| Zookeeper | 2181 | Kafka coordination | - |
| Redis | 6379 | Caching, sessions | - |
# api-gateway/.env
NODE_ENV=development
PORT=3002
JWT_SECRET_KEY=your-jwt-secret-here
# External Services
QUERY_SERVICE_URL=http://query-service:3200
TENANT_CONFIG_SERVICE_URL=http://tenant-config-service:3300
SERVICE_REGISTRY_URL=http://service-registry:3100
# Message Broker
KAFKA_BROKERS=kafka:29092
KAFKA_CLIENT_ID=api-gateway
# Database & Caching
REDIS_URL=redis://redis:6379
MONGODB_URI=mongodb://mongodb:27017
# Rate Limiting (Default fallback values - 50 r/s as per requirements)
RATE_LIMIT_DEFAULT=50
RATE_LIMIT_WINDOW_MS=1000
RATE_LIMIT_MAX_BURST=100
# Tenant Configuration Service
TENANT_CONFIG_SERVICE_URL=http://tenant-config-service:3300
TENANT_CONFIG_TIMEOUT=5000
TENANT_CONFIG_CACHE_TTL=300# query-service/.env
NODE_ENV=development
PORT=3200
SERVICE_NAME=query-service
# MongoDB (Read-optimized)
MONGODB_URI=mongodb://mongodb:27017
MONGODB_DB_NAME=messaging_platform
MONGODB_MAX_POOL_SIZE=10
# API Configuration
API_PREFIX=/api/v1
MAX_PAGE_SIZE=100
DEFAULT_PAGE_SIZE=20
# Logging
LOG_LEVEL=infoKey environment variables in docker-compose.yaml:
environment:
# Production settings
- NODE_ENV=production
# Service URLs (internal Docker network)
- QUERY_SERVICE_URL=http://query-service:3200
- KAFKA_BROKERS=kafka:29092
- MONGODB_URI=mongodb://mongodb:27017
# Security
- JWT_SECRET_KEY=${JWT_SECRET_KEY}
# Performance tuning
- MONGODB_MAX_POOL_SIZE=20
- KAFKA_BATCH_SIZE=16384- API Gateway:
http://localhost:3002/api/v1 - Query Service:
http://localhost:3200/api/v1 - Tenant Config Service:
http://localhost:3300/api/v1
All requests require JWT token in Authorization header:
Authorization: Bearer <your-jwt-token>
X-Tenant-ID: <your-tenant-id>POST /api/v1/messages
Content-Type: application/json
{
"to": "+1234567890",
"channel": "sms",
"content": {
"text": "Hello from the messaging platform!",
"type": "text"
},
"priority": "normal",
"metadata": {
"campaign": "welcome-series"
}
}GET /api/v1/messages/{messageId}/status
Authorization: Bearer <token>
X-Tenant-ID: <tenant-id>GET /api/v1/messages?channel=sms&status=delivered&page=1&limit=20
Authorization: Bearer <token>
X-Tenant-ID: <tenant-id>GET /api/v1/conversations/{conversationId}/history?page=1&limit=50
Authorization: Bearer <token>
X-Tenant-ID: <tenant-id># Create tenant configuration
POST /api/v1/configs
Content-Type: application/json
{
"tenantId": "acme-corp",
"name": "ACME Corporation",
"tier": "pro",
"limits": {
"global": {
"rateLimit": 100,
"burstLimit": 200,
"windowMs": 60000
}
},
"features": {
"bulkMessaging": true,
"webhooks": true
}
}
# Get tenant configuration
GET /api/v1/configs/{tenantId}
# Update tenant configuration
PUT /api/v1/configs/{tenantId}All endpoints return standardized JSON responses:
{
"success": true,
"data": {
"messageId": "msg_12345",
"status": "delivered",
"timestamp": "2024-01-01T12:00:00Z"
},
"meta": {
"requestId": "req_67890",
"timestamp": "2024-01-01T12:00:00Z"
}
}{
"success": false,
"error": {
"code": "NOT_FOUND",
"message": "Message with ID 'abc123' not found",
"statusCode": 404
},
"meta": {
"requestId": "req_67890",
"timestamp": "2024-01-01T12:00:00Z"
}
}The platform includes a comprehensive automated test suite covering all aspects of the system:
# Quick test (basic functionality)
./test.sh fast
# Full test suite (all tests)
./test.sh run all
# Specific test categories
./test.sh run isolation # Channel isolation tests
./test.sh run ordering # Message ordering tests
./test.sh run health # Health check tests
# Run tests against existing services (fastest)
npm run test:e2etests/
βββ config/ # Test configuration files
β βββ jest.unit.config.js # Unit test configuration
β βββ jest.e2e.config.js # E2E test configuration
β βββ docker-compose.test.yml # Full test environment
β βββ docker-compose.fast-test.yml # Minimal test environment
βββ e2e/ # End-to-end tests
β βββ channel-services.test.js # Channel service integration tests
β βββ isolation.test.js # Channel isolation tests
β βββ ordering.test.js # Message ordering tests
βββ unit/ # Unit tests
β βββ basic.test.js # Basic functionality tests
βββ setup/ # Test setup/teardown
βββ run-tests.sh # Main test runner script
βββ test-services.sh # Service verification script
| Test Category | Tests | Status | Coverage |
|---|---|---|---|
| Channel Services | 9 tests | β Passing | Health checks, message processing, performance |
| Message Ordering | 3 tests | β Passing | Sequential ordering, concurrent handling, high-frequency |
| Channel Isolation | 3 tests | β Passing | Independent operation, tenant isolation |
| Unit Tests | 6 tests | β Passing | Basic functionality, environment validation |
| Total | 21 tests | β 100% Pass | Complete system validation |
- Import Collection: Import
Messaging-Platform-Complete.postman_collection.json - Set Variables: Configure collection variables (URLs, tenant ID, JWT token)
- Run Health Checks: Start with "Health Checks & Monitoring" folder
- Test Message Flow: Use "Message Operations" folder to test sending
- Query Operations: Test "Query Operations" to verify data retrieval
- End-to-End Testing: Run "End-to-End Flow Test" for complete validation
# Test API Gateway health
curl http://localhost:3002/health
# Test Query Service health
curl http://localhost:3200/api/v1/health
# Test Tenant Config Service health
curl http://localhost:3300/health
# Send a test SMS (requires JWT token)
curl -X POST http://localhost:3002/api/v1/messages \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-H "X-Tenant-ID: test-tenant" \
-d '{
"to": "+1234567890",
"channel": "sms",
"content": {
"text": "Test message",
"type": "text"
}
}'
# Query message status (replace MESSAGE_ID)
curl "http://localhost:3200/api/v1/messages/MESSAGE_ID?tenantId=test-tenant"| Service | Endpoint | Response Time | Key Metrics |
|---|---|---|---|
| API Gateway | /health |
< 100ms | Service status, uptime |
| API Gateway Deep | /health/deep |
< 500ms | All dependencies status |
| Query Service | /api/v1/health |
< 200ms | MongoDB connectivity, uptime |
| Tenant Config Service | /health |
< 100ms | Service status, Redis connectivity |
| Service Registry | /health |
< 100ms | Registered services count |
# View all service health
curl http://localhost:3002/health/deep
# Check registered services
curl http://localhost:3100/services
# MongoDB connection status via Query Service
curl http://localhost:3200/api/v1/health# View live logs for all services
docker-compose logs -f
# Service-specific logs
docker-compose logs -f api-gateway
docker-compose logs -f query-service
# Filter by correlation ID for request tracing
docker-compose logs | grep "req_12345"Access Kafka UI at http://localhost:8080 to monitor:
- Topic message rates
- Consumer group lag
- Broker status
- Message content inspection
# Start all services
docker-compose up -d
# Start specific services
docker-compose up -d api-gateway query-service mongodb
# Rebuild after code changes
docker-compose up -d --build api-gateway
# View service status
docker-compose ps# Use production compose file
docker-compose -f docker-compose.prod.yml up -d
# Scale services horizontally
docker-compose up -d --scale api-gateway=3 --scale query-service=2
# Update single service
docker-compose up -d --no-deps --build api-gateway# Stop all services
docker-compose down
# Stop and remove volumes (clears data)
docker-compose down -v
# View resource usage
docker-compose top
# Execute commands in running containers
docker-compose exec api-gateway bash
docker-compose exec mongodb mongosh| Service | Internal Port | External Port | Network |
|---|---|---|---|
| API Gateway | 3002 | 3002 | assignment-network |
| Query Service | 3200 | 3200 | assignment-network |
| Tenant Config Service | 3300 | 3300 | assignment-network |
| Service Registry | 3100 | 3100 | assignment-network |
| SMS Service | 3005 | 3005 | assignment-network |
| Email Service | 3003 | 3003 | assignment-network |
| WhatsApp Service | 3004 | 3004 | assignment-network |
| MongoDB | 27017 | 27017 | assignment-network |
| Kafka | 29092 (internal), 9092 (external) | 9092 | assignment-network |
| Kafka UI | 8080 | 8080 | assignment-network |
- Client Authentication: JWT token with tenant and scope claims
- Token Validation: API Gateway validates signature and expiration
- Context Extraction: Extract tenantId, userId, scopes from token
- Request Authorization: Validate scopes for requested operation
- Tenant Isolation: All database queries scoped by tenantId
{
"sub": "user-123",
"tenantId": "tenant-456",
"scopes": ["messages:read", "messages:write", "conversations:read"],
"iat": 1704067200,
"exp": 1704153600
}# Required headers for authenticated requests
Authorization: Bearer <jwt-token>
X-Tenant-ID: <tenant-id>
X-Correlation-ID: <correlation-id> # Optional but recommended// All database queries include tenant filter
const messages = await db.messages.find({
tenantId: tenantId, // Always required
messageId: messageId
})
// Conversation key validation prevents cross-tenant access
if (!conversationKey.startsWith(`${tenantId}:`)) {
throw new UnauthorizedError('Cross-tenant access denied')
}- Joi Schema Validation: All request bodies validated
- SQL Injection Prevention: MongoDB parameterized queries
- XSS Prevention: HTML content sanitization
- Rate Limiting: Per-tenant and global rate limits
docs/
βββ architecture-diagrams.md # System architecture and flow diagrams
βββ system-design-documentation.md # Comprehensive design decisions
βββ api-gateway/docs/ # API Gateway specific docs
β βββ configuration.md
β βββ troubleshooting.md
β βββ getting-started.md
βββ query-service/README.md # Query Service documentation
- Architecture Diagrams: Visual system architecture, CQRS pattern, deployment diagrams
- System Design Documentation: Design decisions, flows, scalability patterns
- API Gateway Docs: Detailed API Gateway configuration and usage
- Query Service README: Query Service API reference and deployment
- Postman Collection:
Messaging-Platform-Complete.postman_collection.json - Test Scripts: Environment setup, automated validation, end-to-end testing
- Clone repository
- Install Docker & Docker Compose
- Copy and configure
.envfiles - Run
docker-compose up -d - Import Postman collection
- Test health endpoints
- Send first test message
- Review architecture documentation
- Review security configuration
- Set production environment variables
- Configure external provider APIs (Twilio, SendGrid, Meta)
- Set up monitoring and alerting
- Configure load balancing
- Plan backup strategy for MongoDB
- Test disaster recovery procedures
- Import Postman collection
- Configure test tenant and JWT tokens
- Run all health check tests
- Test message sending across all channels
- Validate query operations
- Test webhook scenarios
- Run end-to-end flow tests
- Performance testing with bulk messages
- Port Conflicts: Check if ports 3002, 3200, 9092, 27017 are available
- Docker Memory: Ensure Docker has at least 4GB RAM allocated
- JWT Token: Use a valid JWT token for testing authenticated endpoints
- Service Startup: Wait 30-60 seconds for all services to fully initialize
- MongoDB Connection: Query Service requires MongoDB to be healthy first
- Development: Use
docker-compose logs -fto monitor service health - Production: Scale API Gateway and Query Service horizontally for load
- Database: MongoDB indexes are pre-configured for optimal query performance
- Monitoring: Use Kafka UI to monitor message processing throughput
- Make code changes
- Rebuild specific service:
docker-compose up -d --build service-name - Test with Postman collection
- Check logs:
docker-compose logs -f service-name - Validate health endpoints
Once you have the platform running:
- Send Your First Message: Use Postman to send an SMS or email
- Explore the APIs: Try different endpoints in the Postman collection
- Monitor the System: Check health endpoints and Kafka UI
- Review Architecture: Study the system design documentation
- Configure Providers: Set up real Twilio/SendGrid/Meta credentials
- Scale Testing: Try bulk message operations
- Integrate: Connect your application using the REST APIs
π You now have a production-ready, enterprise-grade messaging platform!
For questions, issues, or contributions, please refer to the documentation in the docs/ folder or check the service-specific README files.