Design a Message Queue System to Handle Millions of Messages

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2025-04-13

Problem Statement

Design a highly available, distributed message queue system that allows producers to publish messages and consumers to process them asynchronously.

The system should support millions of messages in flight, ensure durability, scalability, and fault tolerance, and allow multiple consumer models (e.g., competing consumers, fan-out).


✅ Functional Requirements

Message Publishing

•Producers should be able to send messages to a specific topic or queue.
•The system should acknowledge messages only after persistence or replication.

Message Consumption

•Consumers should be able to pull or receive messages from queues.
•Support at-least-once, at-most-once, and exactly-once delivery semantics.

Persistence & Durability

•Messages should be persisted to disk or replicated across nodes to prevent loss.

Ordering

•Maintain message order within a partition or topic.

Scalability

•The system must handle millions of messages per second by horizontally scaling.

Acknowledgements & Retries

•Implement acknowledgements for successful processing.
•Re-queue or move messages to a dead-letter queue (DLQ) on repeated failure.

Monitoring & Metrics

•Track queue depth, message lag, throughput, and consumer health.


⚙️ Non-Functional Requirements


Category with Requirement


Availability

> 99.99% uptime with no single point of failure.


Durability

Messages should never be lost (replication or persistent storage).


Latency

Message publish-to-consume latency < 100 ms ideally.


Scalability

Should scale horizontally to handle millions of messages/sec.


Fault Tolerance

Auto-recover from node or network failures.


Consistency

Eventual consistency acceptable across replicas.


🧩 Key Components / Subsystems


Component with Responsibility


Producer API Gateway

Entry point for message publishing. Handles authentication and rate-limiting.


Broker / Queue Manager

Core service responsible for queueing, routing, and persisting messages.


Storage Layer

Durable storage for messages (e.g., Kafka log, SSTable, RocksDB).


Consumer Manager

Handles consumer subscriptions, offsets, and delivery.


Replication Manager

Ensures data redundancy and failover support.


Monitoring & Metrics Service

Tracks performance, health, and anomalies.


Dead Letter Queue (DLQ)

Stores messages that repeatedly fail processing.


🧱 High-Level Design Considerations


•Partition queues across multiple brokers for horizontal scalability.
•Use log-structured storage for fast sequential writes.
•Use leader–follower replication for durability and availability.
•Implement offset tracking for consumer progress management.
•Support exactly-once semantics using idempotent producers and transactional writes.
•Integrate backpressure mechanisms to prevent message overflow.


🧠 Discussion Points (for Interviews)

•How would you partition queues and route messages?
•How would you ensure durability while maintaining high throughput?
•What strategy would you use for message replay or reprocessing?
•How would you handle consumer lag or unresponsive consumers?
•How would you scale horizontally while maintaining ordering guarantees?


🧾 Final Interview Prompt

Design a distributed message queue system capable of handling millions of messages per second.
It should support reliable delivery, persistence, and scalability.
Discuss key components, data flow, storage strategy, fault tolerance, and monitoring approach.
Explain trade-offs between latency, consistency, and throughput.


Solution

Comments