Design a Distributed Message Queue

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2024-04-16

Problem Statement

Design a distributed, fault-tolerant, and scalable message queue system that enables reliable asynchronous communication between producers and consumers. The system should support high throughput, low latency, durability, and at-least-once message delivery guarantees while operating efficiently in a distributed environment.


🔶 Functional Requirements


1.Producer Support
•Publish messages to a queue or topic.
•Support batching for high throughput.
•Configurable acknowledgment modes (ack after write, ack after replication).
2.Consumer Support
•Subscribe to a queue/topic.
•Support push or pull delivery modes.
•Allow multiple consumers (competing consumers for queues, pub-sub for topics).
•Message acknowledgment and redelivery if consumer fails.
3.Message Features
•FIFO ordering within partitions/queues.
•Configurable retention policies (time-based, size-based).
•Support for delayed messages / scheduled delivery.
•Support message replay from a given offset.
4.Queue/Topic Management
•Create, delete, and list queues/topics.
•Partitioning for scalability.
•Configurable replication factor.
5.Monitoring & Ops
•Metrics: message lag, throughput, consumer offsets.
•Dead-letter queues for poison messages.
•Alerting on failures or backlogs.


🔶 Non-Functional Requirements


•Scalability: Must handle millions of messages per second across partitions.
•Availability: Survive broker/node failures without data loss (replication).
•Durability: Messages must be persisted before acknowledgment.
•Consistency: Ordering should be preserved per partition.
•Latency: End-to-end publish-to-consume latency should be <100 ms for most workloads.
•Security: Authentication, authorization, TLS encryption in transit.


🔶 Constraints


•Messages may be very large (up to a few MBs).
•Producers/consumers can be globally distributed.
•Writes must be append-only for efficiency.
•Replication across nodes for fault tolerance.
•Metadata (like offsets, partitions, queue definitions) must be strongly consistent.


🔶 Example Use Cases


1.E-commerce Order Processing → order service publishes order events, inventory/payment services consume asynchronously.
2.Log/Event Streaming → application logs/events pushed into topics for analytics.
3.IoT Event Pipeline → millions of IoT devices send telemetry, consumers aggregate/process.
4.Payment System → transaction events are queued and consumed by fraud detection + notification services.


👉 Summary:

"Design a distributed, fault-tolerant, and scalable Message Queue system that supports asynchronous producer-consumer communication, ensures durability and reliability with partitioning & replication, and provides monitoring, replay, and dead-letter handling."

Solution

Comments