The User Registration Saga implements a distributed transaction pattern that coordinates user account creation across multiple microservices. This ensures data consistency and provides automatic compensation in case of failures.
- Orchestrator: Centralized saga coordinator service
- Participants: Auth, Profile, and Email services
- State Management: Redis for saga state persistence
- Communication: NATS messaging with circuit breaker protection
User Signup Request → Auth Service → Saga Orchestrator
↓
1. User Account Created ← Auth Service ←──┘
↓
2. Profile Creation → Profile Service
↓
3. Welcome Email → Email Service
↓
4. Saga Completion
| Service | Port | Purpose |
|---|---|---|
| Auth Service | 3100 | User account management |
| Profile Service | 3103 | User profile management |
| Email Service | 3106 | Email notifications |
| Saga Orchestrator | 3107 | Saga coordination |
// Saga Subjects
enum SagaSubjects {
UserRegistrationSagaStarted = 'user-registration-saga:started',
UserAccountCreated = 'user-registration-saga:user-account-created',
ProfileCreated = 'user-registration-saga:profile-created',
WelcomeEmailSent = 'user-registration-saga:welcome-email-sent',
UserRegistrationSagaCompleted = 'user-registration-saga:completed',
UserRegistrationSagaFailed = 'user-registration-saga:failed'
}
// Saga States
enum UserRegistrationSagaState {
STARTED = 'STARTED',
ACCOUNT_CREATED = 'ACCOUNT_CREATED',
PROFILE_CREATED = 'PROFILE_CREATED',
EMAIL_SENT = 'EMAIL_SENT',
COMPLETED = 'COMPLETED',
COMPENSATING = 'COMPENSATING',
FAILED = 'FAILED'
}Key Components:
- SagaStateManager: Redis-based state persistence
- UserRegistrationSagaOrchestrator: Main coordination logic
- Event Listeners: Handle saga step completions
- REST API: Manual saga management endpoints
State Management:
interface SagaState {
sagaId: string;
userId?: string;
userEmail: string;
userName: string;
userAvatar: string;
state: UserRegistrationSagaState;
completedSteps: string[];
createdAt: string;
updatedAt: string;
}Compensation Logic:
- Account Deletion: Removes user account if profile creation fails
- Profile Cleanup: Removes profile if email sending fails
- Automatic Rollback: Triggered when any step fails
- Saga Integration: Calls saga orchestrator before publishing events
- Event Publishing: Publishes UserAccountCreated with saga context
- Rollback Handler: Listens for account deletion commands
- Saga Listener: Responds to UserAccountCreated saga events
- Profile Creation: Creates user profile and publishes ProfileCreated
- Compensation: Handles profile deletion requests
- Welcome Email: Sends personalized welcome emails
- Saga Completion: Publishes WelcomeEmailSent event
- Email Content: Customized with user name and platform info
Each service requires:
# NATS Configuration
NATS_CLIENT_ID=service-name-client
NATS_URL=http://localhost:4222
NATS_CLUSTER_ID=auction
# Service-specific
EMAIL=your-email@gmail.com
EMAIL_PASSWORD=your-app-password
# Saga Orchestrator
REDIS_HOST=localhost
REDIS_PORT=6379- Failure Threshold: 3 failures
- Timeout: 30 seconds
- Retry Logic: Exponential backoff
# Start infrastructure
docker-compose up -d redis nats mysql
# Start microservices
cd services/auth && npm start &
cd services/profile && npm start &
cd services/email && npm start &
cd services/saga-orchestrator && npm start &./demo-user-registration-saga.sh# Create user account (triggers saga)
curl -X POST http://localhost:3100/api/auth/signup \
-H "Content-Type: application/json" \
-d '{
"email": "test@example.com",
"password": "password123",
"name": "Test User",
"avatar": "https://example.com/avatar.jpg"
}'
# Check saga status
curl http://localhost:3107/api/saga/status- Auth:
GET /api/auth/health - Profile:
GET /api/profile/health - Email:
GET /api/email/health - Saga:
GET /api/saga/health
- Auth:
GET /api/auth/health/nats - Profile:
GET /api/profile/health/nats - Email:
GET /api/email/health/nats - Saga:
GET /api/saga/health/nats
- Status:
GET /api/saga/status - Specific Saga:
GET /api/saga/status/:sagaId - Retry Failed:
POST /api/saga/retry/:sagaId - Cancel:
POST /api/saga/cancel/:sagaId
# Real-time logs
tail -f logs/auth.log
tail -f logs/profile.log
tail -f logs/email.log
tail -f logs/saga-orchestrator.log- Detection: Profile service fails to create profile
- Compensation: Saga orchestrator deletes user account
- Recovery: User can try registration again
- Detection: Email service unavailable or SMTP issues
- Compensation: Saga orchestrator removes profile and account
- Recovery: Manual retry or automatic retry after service recovery
- Detection: NATS circuit breaker opens
- Behavior: Services fail fast, prevent cascade failures
- Recovery: Automatic reconnection when network recovers
- State Persistence: Saga state stored in Redis
- Recovery: On restart, saga orchestrator resumes from saved state
- Timeout Handling: Redis TTL ensures stale sagas are cleaned up
- All saga operations are idempotent
- Duplicate events are safely ignored
- Retry-safe operations
- Saga state expires after 1 hour
- Step-level timeouts prevent hanging sagas
- Automatic cleanup of expired sagas
- Comprehensive error logging
- Circuit breaker protection
- Graceful degradation
- Health check endpoints
- Real-time status monitoring
- Performance metrics
- Bid Placement Saga
- Auction Completion Saga
- Payment Processing Saga
- Saga visualization dashboard
- Metrics and alerting
- Distributed tracing
- Performance optimization
- Multiple saga orchestrator instances
- Partitioned saga processing
- Event sourcing integration
-
Services not starting
- Check port availability
- Verify environment variables
- Ensure dependencies are running
-
Saga not progressing
- Check NATS connectivity
- Verify event publishing/listening
- Check Redis connection
-
Compensation not working
- Verify compensation handlers
- Check error propagation
- Review saga state transitions
-
Email not sending
- Verify SMTP configuration
- Check email service logs
- Validate email credentials
# Check service connectivity
curl http://localhost:3100/api/auth/health
curl http://localhost:3103/api/profile/health
curl http://localhost:3106/api/email/health
curl http://localhost:3107/api/saga/health
# Check NATS connectivity
curl http://localhost:3100/api/auth/health/nats
# Monitor Redis
redis-cli monitor
# Check NATS server
curl http://localhost:8222/varzThe User Registration Saga provides a robust, fault-tolerant solution for distributed user registration across microservices. With comprehensive error handling, compensation logic, and monitoring capabilities, it ensures data consistency while maintaining system resilience.
The implementation follows saga pattern best practices and provides a solid foundation for additional saga implementations in the auction platform.