System Design Academy

Saving…

Lesson 7 of 9 in Module 40 of 54 lessons complete

Pub/Sub & Event Streams

Module 4, lesson 7Concept12 min

Definition

Event

In plain English
An event is a record of something that already happened, named in the past tense, such as OrderPlaced.
Think of it like
It is like a stamped entry in a diary.

Topic

In plain English
A topic is a named channel that messages are published to.
Think of it like
Like a radio frequency.

Publish/subscribe

In plain English
Publish/subscribe means a publisher sends a message to a topic, and every subscriber to that topic gets its own copy.
Think of it like
It is like a radio broadcast heard by every tuned-in listener.

Event stream

In plain English
An event stream is an ordered, stored log of events that consumers read at their own pace and can re-read later.
Think of it like
It is like a ship's logbook anyone can read from any page.

Offset

In plain English
An offset is a consumer's position in an event stream.
Think of it like
Like a bookmark in the logbook.

Consumer group

In plain English
A consumer group is a set of consumers sharing the work of reading one stream, each event going to one member of the group.
Think of it like
Like a team splitting a long logbook between them.

Partition key

In plain English
A partition key is a chosen value, such as a show ID, that determines which partition receives an event.
Think of it like
It is like the ship name that tells you which logbook to write in.

Partition

In plain English
A partition is one ordered slice of a stream; events with the same key go to the same partition and stay in order.
Think of it like
Like one logbook per ship in a fleet.

Why it exists

Each charger event matters to billing, maps, and analytics. One shared work queue would split events between them.

Auditors also need last month’s trades. Deleting every message after its first reader finishes cannot provide that history.

How it works

PatternWho receivesKept after readingReplayExample
Work queueOne worker per deliveryUsually until acknowledgementNot after deletionAudio conversion
Pub/subEach subscriptionDepends on settingsNot inherentCharger notifications
Event streamEach independent groupUntil retention expiresYes, while retainedTrade history

A charger publishes to one topic, which sends separate copies to billing, map, and analytics subscriptions.

Each subscription receives the charger event.

A charger publishes to one topic, which sends separate copies to billing, map, and analytics subscriptions.

Five offsets form a log; an audit reader points to offset 1 while a live reader points to offset 4.

Readers can track different positions in retained history.

Five offsets form a log; an audit reader points to offset 1 while a live reader points to offset 4.

  1. Choose a key

    Use a show ID when each show needs its own ordered votes.

  2. Assign partitions

    Keep routing for each key stable; changing partition assignments requires a plan to preserve order.

  3. Split the work

    One group member owns a partition at a time and processes its events in order.

Order is within each partition, not across the whole stream. Replay needs retained events and compatible readers. Monitoring and retries still matter.

Examples

Ev charging

A SessionEnded event goes to billing, the live map, and analytics through separate subscriptions. A new carbon-report subscriber requires no charger changes.

Billing, the map, and analytics use separate subscriptions. A new carbon-report subscription receives future events without changing the charger publisher.

SessionEnded → Topic → Separate subscriber copies.

One fact can trigger several independent reactions.

SessionEnded → Topic → Separate subscriber copies.

The insight

Pub/sub fans one event out to many services.

Trade-offs

Pros

  • Deliver a fact to several services.
  • Replay retained history.
  • Process partitions in parallel.

Cons

  • Ordering is limited to each partition.
  • Retention needs storage.
  • Readers track positions and handle repeats.

Use when

  • Several services need each event.
  • Audits or rebuilding need retained history.

Avoid when

  • One background worker is enough.
  • History storage has no useful purpose.

Quick check

Billing, maps and analytics all need every charger event. Queue or pub/sub?
Auditors need to re-read last Tuesday's trades. What makes this possible?

Takeaways

  • Queues share work; pub/sub sends copies to subscriptions.
  • Streams retain history for replay within their retention window.
  • Partitions provide local order and parallel work across keys.

Try the quick check above whenever you feel ready.

Previous: Message Queues