Pub/Sub & Event Streams
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
| Pattern | Who receives | Kept after reading | Replay | Example |
|---|---|---|---|---|
| Work queue | One worker per delivery | Usually until acknowledgement | Not after deletion | Audio conversion |
| Pub/sub | Each subscription | Depends on settings | Not inherent | Charger notifications |
| Event stream | Each independent group | Until retention expires | Yes, while retained | Trade history |
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.
- Choose a key
Use a show ID when each show needs its own ordered votes.
- Assign partitions
Keep routing for each key stable; changing partition assignments requires a plan to preserve order.
- 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.
The insight
Pub/sub fans one event out to many services.
Stock matching engine
Trades are appended to a stream configured to retain seven years of history. Auditors replay a day using their own offset.
Configure seven-year retention and enough durable storage. An auditor resets its own offset without moving the live billing reader.
stream: trades
retention: 7 years
audit_reader_start: first offset from last Tuesday
live_reader_position: unchangedThe insight
Streams keep history you can replay.
Tv voting
Votes use show ID as their partition key. Six counters share the stream’s partitions as one consumer group.
Six counters share a consumer group. They can all work only when the stream has at least six assigned partitions.
Votes route by show to partitions A and B with separate counters; four other partitions feed the other four counters.
One show remains on one partition. Extra counters do not divide that show’s ordered work.
The insight
Partitions give order per key and parallelism across keys.
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
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.