Part of series: System Design Roadmap
Week 5 Day 1: Message Queues - Decoupling Components
When User A registers, you need to:
- Create DB entry (50ms).
- Send Welcome Email (2000ms).
- Update CRM (500ms).
If you do this Synchronously, user waits 2.5s. If Email Service is down, Registration fails.
Solution: Message Queues
- User Registers -> Create DB entry (50ms).
- Publish Event: “UserCreated” -> Queue (5ms).
- Return “Success” to user (Total 55ms).
- Worker Service picks up event from Queue and sends email in background.
Benefits
- Decoupling: The Email service can be down, but registration still works. Messages just pile up in the Queue until it recovers.
- Traffic Spikes: If 1M users join at once, the queue acts as a buffer. Workers consume at their own pace.
Common Technologies
- RabbitMQ: Traditional AMQP. Good for complex routing.
- Kafka: High throughput log. Good for streaming data.
- AWS SQS: Simple, managed, infinite scaling.
Tomorrow: What if multiple services need to hear the same message? Pub/Sub. 📢