Bird
Raised Fist0

Design a Distributed Message Queue - Kafka-Like Architecture End to End

HLDhardInterview freq: high
Choose your preparation mode4 modes available
🎯
Design a Distributed Message Queue - Kafka-Like Architecture End to End
45 min · senior
senior

You are tasked with designing a distributed message queue system similar to Apache Kafka for a startup with 50 million daily active users (DAU). The system must support high throughput messaging with strong ordering guarantees per topic partition, ensure end-to-end encryption, provide offline message delivery, and maintain a P99 latency under 200ms for message publish and consume operations. The system should be highly available and scalable to handle peak loads of up to 1 million messages per second. Clients include mobile and web applications that require real-time and offline message synchronization.

  • End-to-end encryption of messages in transit and at rest is mandatory
  • Offline message delivery with client-side cursor-based sync
  • P99 latency for publish and consume must be under 200ms
  • Support 50M DAU with peak 1M messages/sec throughput
  • Strong ordering guarantees per topic partition
  • High availability with zero single points of failure
📋
Guided Steps
1/5
Step 1
Partitioning and Ordering Strategy
How will you design the partitioning scheme to ensure strong ordering guarantees and scalability?
💡 Hint
Consider how Kafka uses partition keys to guarantee order within partitions and enable parallelism.
Use Kafka-style partitioning with a consistent partition key (e.g., conversation_id) to ensure all related messages land in the same partition, enabling ordered writes and reads within that partition.
Mid-level: Names Kafka partitioning concept. Senior: Explains why conversation_id is chosen as partition key to maintain order and avoid cross-partition reordering, and why sender_id or random keys fail at scale.
⚠️Common Wrong Answer
Using sender_id or random partition keys causing messages of the same conversation to scatter across partitions, breaking ordering guarantees and complicating consumer logic.
Step 2
Storage and Data Model
What storage technology and data model will you use to persist messages efficiently and support cursor-based consumption?
💡 Hint
Think about write-optimized storage engines that support range scans and high write throughput.
Use Apache Cassandra with partition_key=topic_partition and clustering_key=offset to leverage LSM-tree writes for linear scalability and efficient range queries for cursor-based message consumption.
Mid-level: Suggests a NoSQL store. Senior: Explains why Cassandra's LSM-tree design fits high write throughput and range scans, and why relational databases or HBase are less suitable due to write amplification or latency.
⚠️Common Wrong Answer
Using relational databases for message storage leading to write bottlenecks and poor range scan performance at 1M msg/sec scale.
Step 3
Message Delivery and Offline Sync
How will you design the message delivery mechanism to support offline clients and ensure exactly-once delivery semantics?
💡 Hint
Consider how Kafka consumers track offsets and how offline clients can resume consumption without missing messages.
Implement consumer offset tracking stored in Apache Zookeeper or Kafka’s internal __consumer_offsets topic, enabling clients to resume from last committed offset; use client-side cursor-based sync with message retention in Cassandra for offline delivery.
Mid-level: Mentions offset tracking. Senior: Details offset commit protocols, retention policies, and how offline clients use cursor-based sync to fetch missed messages without duplicates.
⚠️Common Wrong Answer
Stateless consumers without offset tracking causing message loss or duplicates during offline periods.
Step 4
Security and Encryption
How will you ensure end-to-end encryption of messages both in transit and at rest?
💡 Hint
Think about TLS for transport and encryption at the storage layer, plus key management.
Use TLS 1.3 for all client-broker and inter-broker communication; enable server-side encryption at rest using AWS KMS-integrated encryption for Cassandra SSTables; implement client-side encryption SDKs for end-to-end confidentiality.
Mid-level: Mentions TLS and encryption at rest. Senior: Explains why TLS 1.3 is chosen for performance and security, how KMS integration secures keys, and the trade-offs of client-side encryption for end-to-end security.
⚠️Common Wrong Answer
Relying only on TLS without encryption at rest or ignoring key management, risking data exposure if storage is compromised.
Step 5
Latency and Scalability
How will you architect the system to maintain P99 latency under 200ms at peak load of 1M messages/sec?
💡 Hint
Consider partition count, replication, and broker scaling strategies.
Deploy a Kafka cluster with 1000 partitions distributed across 50 brokers, each with SSD storage and replication factor 3 for fault tolerance; use Kafka’s ISR (in-sync replicas) to maintain low latency; employ load balancing and backpressure mechanisms.
Mid-level: Suggests partitioning and replication. Senior: Calculates partition count based on throughput (1M msg/sec / 1000 partitions = 1000 msg/sec per partition), explains broker sizing, ISR impact on latency, and backpressure handling.
⚠️Common Wrong Answer
Using too few partitions causing broker overload and high latency, or too many partitions causing metadata overhead and instability.
🔌
API Design Challenge
Sketch 3 core API endpoints for the distributed message queue system.
  • How does a client publish a message to a topic partition?
  • How does a client consume messages and maintain cursor state?
  • How does a client acknowledge message processing or commit offsets?
📊
Scoring Rubric
▼ Show
Must Have - 60%
  • Kafka-style partitioning with strong ordering guarantees per partition
  • Use of Apache Cassandra for scalable, write-optimized message storage
  • Consumer offset tracking for offline delivery and exactly-once semantics
  • End-to-end encryption with TLS and encryption at rest
  • Cluster sizing and replication strategy to meet latency and availability SLAs
Good to Have - 30%
  • Client-side cursor-based sync protocol design
  • Use of Kafka ISR for consistency and latency trade-offs
  • Load balancing and backpressure mechanisms
Bonus - 10%
  • Discussion of key management and client-side encryption trade-offs
  • Detailed API design for publish, consume, and offset
✅
Model Answer
▼ Reveal

Design a Kafka-like distributed message queue system supporting 50M DAU with 1M msg/sec throughput, strong ordering per partition, end-to-end encryption, offline delivery, and P99 latency under 200ms.

  • Use Kafka-style partitioning with conversation_id to guarantee ordering and scalability
  • Persist messages in Apache Cassandra leveraging LSM-tree writes for high throughput and efficient range scans
  • Track consumer offsets in Kafka’s __consumer_offsets topic to enable exactly-once delivery and offline sync
  • Encrypt all communication with TLS 1.3 and encrypt data at rest using AWS KMS integrated with Cassandra SSTables
  • Deploy 1000 partitions across 50 brokers with replication factor 3 to meet latency and availability SLAs
  • Implement client-side cursor-based sync to support offline clients and minimize duplicate processing
  • Use Kafka ISR to maintain low latency and high availability without sacrificing consistency
  • Apply load balancing and backpressure to prevent broker overload and maintain stable throughput
  • Kafka-style partitioning with conversation_id as partition key to guarantee ordering and scalability
  • Apache Cassandra for message storage leveraging LSM-tree writes and efficient range scans for cursor-based consumption
  • Consumer offset tracking via Kafka’s __consumer_offsets topic enabling offline client sync and exactly-once delivery
  • TLS 1.3 for transport encryption combined with AWS KMS-integrated encryption at rest for end-to-end security
  • Kafka cluster with 1000 partitions across 50 brokers, replication factor 3, SSD storage to meet 200ms P99 latency at 1M msg/sec
  • Client-side cursor-based sync protocol to support offline message delivery and minimize duplicate processing
  • Use of Kafka’s ISR mechanism to ensure low latency and high availability without sacrificing consistency
  • Load balancing and backpressure mechanisms to prevent broker overload and maintain stable throughput

Practice

(1/5)
1. In the message delivery trace, at Step 4 the Delivery Worker checks the recipient's presence TTL in Redis. What happens if the TTL has expired?
easy
A. Delivery Worker immediately marks message as delivered without sending
B. Delivery Worker retries presence check indefinitely until TTL refreshes
C. Delivery Worker deletes the message assuming recipient is offline permanently
D. Delivery Worker queues message for offline delivery and marks as sent but not delivered

Solution

  1. Step 1: Trace Delivery Worker decision at Step 4 -- Bob's presence TTL

    If TTL expired, recipient is considered offline; message cannot be delivered immediately.
  2. Step 2: Check Delivery Worker behavior on expired TTL

    Delivery Worker deletes message from active queue to avoid indefinite retries; offline delivery handled elsewhere.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Expired presence TTL -> message deleted from active delivery queue [check]
Hint: Expired presence TTL means no immediate delivery, message dropped [OK]
Common Mistakes:
2. For adaptive bitrate streaming, which architecture best balances latency and bandwidth efficiency when delivering HLS segments to mobile users with fluctuating network conditions?
medium
A. Use a single high-bitrate stream and rely on client buffering to handle fluctuations
B. Use a fixed medium bitrate stream to avoid complexity of switching
C. Use multiple bitrate streams segmented into small chunks with client-driven bitrate switching
D. Use server-driven bitrate adaptation by detecting client bandwidth and pushing segments accordingly

Solution

  1. Step 1: Understand adaptive bitrate streaming principles

    Client-driven switching with multiple bitrate streams allows clients to adapt quickly to network changes, reducing buffering and wasted bandwidth.
  2. Step 2: Evaluate other options

    Single high-bitrate wastes bandwidth; server-driven adaptation adds latency and complexity; fixed bitrate can't adapt to network variability.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Client-driven multi-bitrate switching optimizes latency and bandwidth [check]
Hint: Client controls bitrate switching for best latency and bandwidth [check]
Common Mistakes:
3. If the Kafka event queue used to stream code updates to the vector index is down, what exactly breaks in the AI Code Assistant's update pipeline?
medium
A. The code generator fails to produce completions due to missing context embeddings
B. The vector index update worker blocks waiting for new events, delaying embedding refreshes but serving existing queries
C. The Retriever immediately returns stale embeddings without any fallback
D. The system loses all code update events permanently, causing data loss

Solution

  1. Step 1: Understand Kafka's role in update pipeline

    Kafka streams code updates to vector index workers; if down, updates stop flowing.
  2. Step 2: Trace impact on components

    Retriever can still serve existing embeddings (A and B incorrect); system does not lose all events permanently due to Kafka's durability (D incorrect).
  3. Step 3: Identify generator failure

    Without fresh embeddings, generator lacks up-to-date context, causing failures or degraded completions (C correct).
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Kafka down blocks embedding updates, generator lacks context [check]
Hint: Kafka down stalls embedding updates, generator lacks fresh context [check]
Common Mistakes:
4. Without the consistent hashing ring metadata, what exactly breaks in a distributed cache system when a client requests a key?
medium
A. The cache nodes lose synchronization and data becomes corrupted
B. The cache node receiving the request cannot determine the correct node, causing request failure or indefinite retry
C. The client receives stale data from an outdated cache node
D. The system falls back to a centralized database for all requests, increasing latency

Solution

  1. Step 1: Role of consistent hashing ring metadata

    The ring metadata maps keys to nodes; without it, nodes cannot route requests correctly, leading to inconsistent data placement.
  2. Step 2: Consequence of missing ring metadata

    Without this metadata, nodes lose synchronization on key ownership, causing data corruption as writes go to wrong nodes; request failure (The cache node receiving the request cannot determine the correct node, causing request failure or indefinite retry) is secondary, and fallback (The system falls back to a centralized database for all requests, increasing latency) is not automatic.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Missing ring metadata causes data corruption due to lost key-node mapping [check]
Hint: No ring metadata -> lost key-node mapping -> data corruption [check]
Common Mistakes:
5. At 10x traffic scale, which component is most likely to fail first in a system without rate limiting, and why?
hard
A. The backend cache, because its memory fills up causing evictions and cache misses
B. The database, due to increased write load causing slow queries and deadlocks
C. The network load balancer, as it cannot handle the increased number of TCP connections
D. The API servers, because they maintain stateful connections that cannot be easily sharded

Solution

  1. Step 1: Analyze component scalability at 10x traffic

    Databases (A) and caches (D) can be scaled horizontally or sharded; load balancers (C) can be scaled or replaced with L4/L7 solutions.
  2. Step 2: Identify API servers as bottleneck

    API servers maintaining stateful connections (e.g., WebSockets) are hard to shard or move; connection limits cause saturation and failure first.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Stateful API servers hit connection limits first at scale [check]
Hint: Stateful API servers fail first due to connection limits [check]
Common Mistakes:
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard