SkillPilotAI

Pub/Sub vs Message Queue vs Event Streaming

In a queue one message goes to one worker, in Pub/Sub every subscriber gets a copy, and in Event Streaming messages stay saved in a log that anyone can replay at any time.

25.1 Why is it used?

  • One event (order placed) is needed by email, inventory, analytics, fraud - all services; a queue gives it to only one, pub/sub/streaming gives it to all.
  • Choose the wrong tool and you either lose data (Redis Pub/Sub) or get pointless complexity and cost (Kafka for a small job).
  • In some use cases re-reading old data (replay) is essential - for example a new analytics service that must process the last 7 days of data.
  • The most common interview question: 'When will you use Redis vs Kafka vs RabbitMQ?' - this topic clears up exactly that.

25.2 How it works

  • Message Queue (point-to-point): producer -> queue -> one consumer; the message is deleted after the ack. For distributing work (RabbitMQ, SQS).
  • Pub/Sub: the publisher sends to a topic and every subscriber gets its own copy (fan-out). The publisher does not know who is listening.
  • Event Streaming: events are saved on disk in an append-only log (partition); the consumer remembers its offset and can replay (Kafka).
  • Redis Pub/Sub: fire-and-forget - if the subscriber was not connected at that time the message is gone; no persistence or ack.
  • Redis Streams: a log-like data type from Redis 5+ - XADD, consumer groups, XACK, a pending list; persistence depends on Redis RDB/AOF.
  • Kafka: a distributed commit log - topics, partitions, consumer groups, retention (days/weeks), lakhs of msg/sec throughput.
  • RabbitMQ: a smart broker - exchanges (fanout/topic/direct) give both queue and pub/sub patterns; the message is deleted once consumed.
  • Cloud Pub/Sub (Google Pub/Sub, AWS SNS): managed fan-out; SNS pushes to SQS/Lambda/HTTP/SMS, and Google Pub/Sub has pull/push subscriptions and up to 7 days of retention.
A. Message Queue (RabbitMQ / AWS SQS)
  1. 1.Producer: sends a message
  2. 2.Queue: m3 m2 m1
  3. 3.Worker 1: only m1; Worker 2: only m2
  4. 4.Message deleted after ack; 1 msg -> 1 consumer; replay: no
B. Pub/Sub fan-out (Redis Pub/Sub / AWS SNS / Google Pub/Sub)
  1. 1.Publisher: sends a message to a topic
  2. 2.Topic: order-events: m1
  3. 3.Subscribers: Email (m1 copy), Inventory (m1 copy), Analytics (offline -> misses it)
  4. 4.1 msg -> a copy to every subscriber
  5. 5.Redis: an offline subscriber's msg is lost; SNS: pushes to SQS/Lambda/SMS; replay: no
C. Event Streaming log (Kafka / Kinesis / Event Hubs)
  1. 1.Producer: sends an event
  2. 2.Partition log on disk, 7 days retention: offsets 0 1 2 3 4 5
  3. 3.new ML service: replay from offset 0
  4. 4.analytics: offset 2
  5. 5.payment: offset 5
  6. 6.Msg stays in the log; each group has its own offset; replay: yes, order within a partition
D. Real world: using all three in a food delivery app
  1. 1.OrderPlaced event: Kafka 'orders' (12 partitions, 7 days); Restaurant, Payment, Analytics, Fraud - separate consumer groups
  2. 2.Invoice PDF + SMS: RabbitMQ task queue, 10 workers, ack, retry on failure -> DLQ
  3. 3.Rider live location: Redis Pub/Sub, update every 2 sec; WebSocket server -> customer; missing 1 is fine
  4. 4.payment-success: AWS SNS topic, managed fan-out -> SQS queues + Lambda (loyalty points)

25.3 Tools you can use

ToolTypeHow it is used
Redis Pub/Sub & StreamsIn-memory data storePub/Sub for live chat/notification fan-out; Streams for a light durable queue with consumer groups.
Apache KafkaDistributed event streaming platformHigh-throughput event log; clickstream, logs, CDC, order events - multiple teams read the same data independently.
RabbitMQMessage broker (AMQP)Task queues and routing-based pub/sub; a fanout exchange puts one message into multiple queues.
Google Cloud Pub/SubManaged global pub/sub (GCP)Serverless, auto-scale, at-least-once delivery, message retention and seek/replay support.
AWS SNSManaged pub/sub (AWS)Publish to a topic -> fan-out to SQS, Lambda, HTTP, email, SMS, mobile push; SNS + SQS is a common pattern.
AWS Kinesis / Azure Event HubsManaged event streamingA Kafka-like managed stream; Event Hubs also supports the Kafka protocol.

25.4 Comparison: Redis vs Kafka vs RabbitMQ vs Cloud Pub/Sub

PointRedis (Pub/Sub/Streams)KafkaRabbitMQCloud Pub/Sub (Google Pub/Sub / AWS SNS)
ModelPub/Sub: fire-and-forget fan-out; Streams: append-only log with consumer groupsDistributed partitioned log (event streaming)Broker with queues + exchanges (both queue and pub/sub)Managed topic + subscriptions fan-out
DurabilityPub/Sub: none, an offline subscriber misses it; Streams: memory + RDB/AOFReplicated on disk (replication factor 3 is common), very durableDurable queue + persistent message on diskManaged, replicated; Google 7 days default retention, SNS does not store
ReplayPub/Sub: no; Streams: yes, you can re-read by IDYes, reset the offset and replay anything within retentionNo, the message is deleted after ack (Streams feature in newer versions)Google: replay via seek/snapshot; SNS: no (archive in SQS)
ThroughputVery fast, lakhs of msg/sec, but limited to one node's memoryHighest - millions of msg/sec in a clusterGood, tens of thousands of msg/sec per nodeAuto scale, practically unlimited, the cloud manages it
OrderingOrdered within a single stream in StreamsStrict order within a partitionOrder in a single queue, can break with multiple consumersGoogle: via ordering key; SNS: via FIFO topic
DeliveryPub/Sub at-most-once; Streams at-least-once (XACK)At-least-once by default, exactly-once (idempotent producer + transactions)At-least-once with ack, or at-most-onceAt-least-once (Google has an exactly-once option too)
Ops complexitySimplest, free if you already have RedisHeaviest - managing brokers, partitions, KRaft (or MSK/Confluent)Medium - cluster, queues, monitoringZero ops, fully managed, pay per message
Best forLive chat, notifications, cache invalidation, small queuesClickstream, logs, analytics, event sourcing, CDCTask queues, complex routing, RPC-style jobsServerless fan-out, cloud-native apps, cross-service events

25.5 When to choose which

  • Redis Pub/Sub: when you need real-time broadcast and missing a message or two is fine (typing indicator, live score).
  • Redis Streams: small-medium load, Redis already in place, and you want a simple durable queue with consumer groups.
  • Kafka: high-volume events, replay needed, multiple teams will read the same events (analytics + search + fraud).
  • RabbitMQ: background jobs, per-message ack/retry, complex routing and low-latency task distribution.
  • Google Pub/Sub / AWS SNS: you are on the cloud and want fan-out without managing servers; SNS + SQS for durable fan-out.

25.6 Real-world example

A hypothetical scenario: using all three in a food delivery app

  1. The user places an order; the Order Service publishes an 'OrderPlaced' event to the Kafka topic 'orders' (12 partitions, 7 days retention).
  2. Restaurant Service, Payment Service, Analytics and Fraud - each creates its own consumer group and reads the same event independently.
  3. A new ML recommendation service launches; it sets its offset to the start and replays the last 7 days of orders.
  4. Generating the invoice PDF and sending SMS goes into a RabbitMQ task queue; 10 workers process with ack, and failures go to the DLQ.
  5. The delivery partner's live location travels over Redis Pub/Sub to the customer's WebSocket server - if one update is missed the next arrives in 2 sec.
  6. On the cloud side, 'payment-success' goes to an AWS SNS topic that fans out to SQS queues and Lambda (loyalty points).
  7. Result: the right tool for each job - durability where needed, speed where needed, and lower cost.

25.7 Interview tips

  • Common mistake: treating Redis Pub/Sub as a reliable queue - if the subscriber disconnects the message is lost forever.
  • Common mistake: using Kafka like a simple task queue - no per-message retry/DLQ built in, and consumers beyond the partition count sit idle. Setting up a Kafka cluster from day one in a small startup is also wrong - high ops cost; start with SQS/SNS or Redis Streams.
  • Interview tip: always state 3 things - does the message remain after being consumed (replay), how many consumers get it, and how ordering is achieved.
  • Worth quoting: Kafka replication factor 3, retention 7 days; Google Pub/Sub default retention 7 days; Redis Pub/Sub is at-most-once.