← Building blocks

Fan one event out to many consumers

Decoupling & async workfundamental

Several systems need to react to the same event, and the producer should not know about them.

Say it like this

The post service should not know who cares about a new post, so it publishes one event to a topic and the fan-out, search and analytics consumers each subscribe independently.

Basic solution

Publish to a topic; each subscriber gets its own copy (pub/sub). Add subscribers without touching the publisher.

Kafka topicsSNS + SQSRedis Pub/SubGoogle Pub/Sub

One event, four independent subscribers

The post service publishes once. Each subscription has its own consumers, offset and retries, so the slow one never holds up the others.

How it works

  1. The producer publishes an event, such as PostCreated with the post id and author id, to a named topic. The producer does not know how many subscribers exist or who they are; it only knows the topic name.
  2. The broker appends the event to the topic's storage and makes it available to every subscription on that topic. A subscription is a named reader of the topic, one per consuming system.
  3. Each subscription receives its own copy of the event and processes it at its own pace, with its own retries and its own position in the stream. The search indexer and the fan-out workers each see every event, independently.
  4. Adding a new consumer means creating a new subscription on the topic; the producer code and the existing subscribers are untouched and do not even restart.
  5. Each subscription acknowledges events independently, so a slow analytics consumer that is hours behind never delays the fan-out consumer, which stays at the head of the stream.

Pub/sub vs a queue

  • A queue delivers each message to one of N competing workers: the work is shared out, and once one worker has it nobody else sees it.
  • A topic delivers each message to every subscription: it is a broadcast. Inside a single subscription you can still have competing workers sharing that subscription's copy of the work.

Worked example

News feed post event

  • 50M posts/day ≈ 580/s average, 3K/s at peak. Each PostCreated event is about 400 B.
  • Subscribers: fan-out workers (write into follower timelines), search indexer, notification service (mentions), analytics sink. Four copies of the stream, none aware of the others.
  • Kafka topic sized at 3K/s × 400 B ≈ 1.2 MB/s; seven days retention ≈ 725 GB across the cluster. Cheap.
  • Fan-out consumer needs the most parallelism: 3K posts/s × 200 followers average = 600K timeline inserts/s, spread over 48 partitions keyed by author id.
  • Adding a spam classifier later is a fifth subscription reading from the same topic with no producer change.

Variants and when each wins

Log-based topics (Kafka, Pulsar, Kinesis)

  • The topic is a durable, replayable log and each subscription (consumer group) tracks its own offset, so a subscriber can be offline for hours and catch up from where it stopped. It wins for anything that matters, which is why it is the default.

Managed topic plus queues (SNS fan-out to SQS)

  • The topic broadcasts each event into one queue per subscriber, and each queue has its own retries and dead-letter queue. It wins when you want managed per-subscriber isolation with no cluster to run, and you do not need replay beyond the queue's retention.

Redis Pub/Sub

  • The broker forwards each message to whoever is connected at that instant and stores nothing, so a subscriber that is offline simply misses it. It wins only for ephemeral signals such as "user 42 is now on server C", and is wrong for business events.

Webhooks

  • The publisher POSTs each event to a URL every subscriber registered, with retries. It wins when subscribers are other organisations outside your network, because HTTP is the only transport they share with you.

Use a log-based topic internally; SNS and SQS when you want managed per-subscriber queues; Redis only for transient signals.

Failure modes

Subscriber falls behind silently

  • Search results miss posts from the last few hours. It happens because one subscription cannot keep up with the event rate and nothing watches its lag, so it drifts further behind every minute. Alert on consumer lag per subscription, scale that consumer on its own, and never share one worker pool between subscriptions, or the slow one starves the rest.

Lost events with an ephemeral broker

  • After the indexer restarts, every post published while it was down is missing from search forever. It happens because Redis Pub/Sub, or an unbounded in-memory topic, delivers only to subscribers connected at that moment and keeps nothing. Move business events to a durable log that retains them until each subscriber has read them.

Schema change breaks consumers

  • Three consumers crash minutes after a producer deploy. It happens because the producer renamed or removed a field and the consumers parsed the old shape. Register event schemas and check compatibility before deploy, only ever add optional fields, and carry a version number in each event so consumers can handle both.

Event storm from a bulk job

  • Live posts take minutes to reach timelines during a backfill. It happens because the backfill published 50M events to the same topic the live traffic uses, and consumers process in order. Put bulk traffic on its own topic or priority, and rate-limit the backfill producer so it only uses spare capacity.

Duplicate deliveries

  • A user is notified twice about one mention. It happens because the broker delivers at least once (a retry after a lost acknowledgement sends the event again) and the consumer did nothing to detect the repeat. Dedupe on the event id within each subscription before acting on it.

Interviewer follow-ups

Answer out loud before you open each one.

When would you use a queue instead of a topic?

When there is exactly one kind of consumer and the point is to share work across workers, such as a transcoding job queue. A topic is for broadcast to independent consumers. In practice you often have a topic feeding several queues, one per consumer type.

How do you guarantee every subscriber eventually gets every event?

Use a durable log with retention longer than any plausible outage and have each subscriber commit its own offset only after processing. Pair the producer with a transactional outbox so an event is never lost between the database write and the publish.

What goes in the event payload?

Enough for most consumers to act without a callback: ids, the changed fields, a timestamp and an event version. Avoid dumping the whole entity if it is large or sensitive; consumers that need more fetch by id. Name the event by what happened, not by what to do.

How do you handle one subscriber that needs ordering and others that do not?

Partition the topic by the entity key (author id) so events for one entity are ordered within a partition, and let the ordering-sensitive consumer run one worker per partition. The others can read with higher parallelism and ignore order.

Connections

In study paths
Decoupling with events
Pairs with
Keep events as a replayable log · Publish events reliably with the write · Make retries safe · Precompute on write, read cheaply · React to database changes
Pulls against
Order events within a stream
Used in
Notification System · News Feed (Twitter / X timeline)

The same parts on other clouds

The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.

AWSUsed here asGoogle CloudMicrosoft AzureOpen source and others
Amazon EC2 Auto ScalingPost service, Fan-out workers, Search indexer, Notification serviceCompute Engine managed instance groupsVirtual Machine Scale SetsKubernetes Deployment with a HorizontalPodAutoscaler, Nomad
Amazon MSK (Kafka)Topic: postsManaged Service for Apache KafkaEvent Hubs (Kafka API)Confluent Cloud, Redpanda
Amazon RedshiftAnalytics sinkBigQuerySynapse Analytics, Microsoft FabricClickHouse, Snowflake, Apache Druid