KatInterview

Design a Tag-Based Pub/Sub Message Broker

System DesignModel answer
HardModel answerSystem Designmessage-queuepub-subbitmask

Last updated

Design a message queue where each message carries multiple topic tags and consumers receive only messages whose tags overlap their subscription set.

Background

You are building a messaging layer for a platform where producers emit events and consumers process them. Unlike a simple single-topic queue, every message can belong to several topics simultaneously, and each consumer opts in to a personal collection of topics. A consumer should receive a message if and only if at least one of its subscribed topics appears on that message — but it should receive the message at most once regardless of how many tags match.

What You Need to Design

Core operations

  • enqueue(producer_id, payload, topics) — publish a message labelled with one or more topic identifiers.
  • subscribe(consumer_id, topics) — add topics to a consumer's interest set.
  • unsubscribe(consumer_id, topics) — remove topics from a consumer's interest set; only affects messages not yet delivered.
  • dequeue(consumer_id) — return the next eligible message for this consumer in arrival order, or nothing if none exists.

Query operations

  • Given two arbitrary topic sets (for example, the tags on a message and the subscription mask of a consumer), return the topics they have in common.
  • Given a set of required producer → consumer delivery pairs, determine the minimum number of topic channels that can satisfy all of them without a consumer receiving messages from a producer it is not paired with.

What Is Expected in Your Answer

Walk through the full design in the order below.

  1. Clarify assumptions — delivery semantics, scale (order-of-magnitude numbers), and how many topics a typical message or subscriber touches.
  2. API and data model — how you store messages, track subscriptions, and route events.
  3. Representing topic sets — pick a concrete data structure and justify it; describe what happens as the number of topics grows.
  4. Fan-out strategy — compare writing to each consumer's queue at enqueue time versus filtering at dequeue time; explain when you would choose each.
  5. Handling unsubscribe — explain how pending messages are dealt with without expensive queue scans.
  6. Topic intersection and union — describe the operations and their cost.
  7. Minimum channel cover — model the problem formally, state its theoretical complexity, and give at least one practical algorithm or bound you would use in an interview or in production.
  8. Complexity analysis — time and space for every operation.

Include pseudocode or working code for the core queue operations.

Hints

Hint 1

Think about how a set of topic IDs can be packed into an integer and what CPU operations that unlocks for intersection and union checks.

Hint 2

Fan-out on write keeps dequeue fast but multiplies storage; fan-out on read keeps enqueue cheap but makes dequeue scan heavy — the right choice depends on your read/write ratio and average fan-out width.

Hint 3

The minimum channel cover maps onto a biclique cover of a bipartite graph, which is NP-hard in general; in an interview, an efficient upper bound based on grouping nodes by identical neighbor sets is usually what the interviewer is looking for.