Part 5 of 6 · 3 min read
Edge cases and changes
Slow readers, memory and scale.
Step 5: edge cases, tests and change requests
Edge cases:
| Case | Behaviour |
|---|---|
| Subscriber throws | Retried, then the message goes to the dead list |
| Subscriber is slow | It falls behind. Others are unaffected |
| New subscriber arrives late | Starts at a chosen offset, here 0 |
| Seek past the end | readFrom returns nothing until new messages come |
| Topic that nobody follows | Messages still pile up in the log |
| The log grows without end | Memory runs out, so real brokers delete old messages by age or size |
| Publisher and subscriber on different threads | append and readFrom are synchronized, and the consumer's own position is touched by one thread at a time |
The main limits of this design should be said plainly. The log is in memory, so a crash loses everything. The Consumer.poll method is not thread-safe, so each consumer must be polled by one thread. A message is in one topic, and one topic is one log under one lock, so a very busy topic is limited by that lock.
How real systems go further: they write the log to disk in segments, so history survives restarts. They split a topic into partitions, each its own log, and send messages with the same key to the same partition, which keeps order per key and spreads load. They let a group of consumers share a topic, each taking some partitions. They store each consumer's position durably.
Tests to write first: each row of the run table; offsets unique under 8 publishers; a subscriber that throws once, many times, and never; seeking back; and a topic with no subscriber.
Change requests and the part that changes:
- "Filter messages by key or content." A predicate in the subscription, applied in
poll. - "Wait for new messages instead of polling." A blocking read, using wait and notify or a condition on the topic.
- "Several consumers share the work." A consumer group with partitions.
- "Keep messages for 7 days." A clean-up task that removes old messages and tracks the first valid offset.
- "Order across topics." Not offered. Say that ordering is per topic.
- "Push model for low latency." A dispatcher thread per subscriber, with a bounded queue, as in Level 5, module 4.
- "Dead list inspection and replay." Expose the dead list, and a method to re-publish its messages.
Common mistake
Calling every subscriber directly from the publisher's thread. One slow or failing subscriber then slows or breaks publishing for everyone. Store the message first, and let each subscriber read at its own pace.
Interview angle
Answer in your head first, then open it.
How is this different from the observer pattern?
Observer calls listeners at once in the same process. Publish-subscribe stores messages, so subscribers read later, at their own speed, and can replay.
Why a log with offsets?
The message is stored once, each subscriber keeps a position, and replay is moving the position back.
What delivery promise do you give?
At-least-once, with idempotent handlers. Exactly-once needs more.
How do you scale it?
Partition the topic by key, give each partition its own log, and share partitions among consumers in a group.
Check yourself: 1. What breaks if a subscriber is called from the publisher's thread? 2. What does partitioning buy? 3. Why does a topic with no subscriber still grow?
Words you will meet
| Word | Meaning |
|---|---|
| Topic | A named stream of messages |
| Publisher | The sender of messages |
| Subscriber | A receiver that follows a topic |
| Offset | The position of a message in a topic's log |
| Dead list | Messages set aside after too many failures |
| Partition | One slice of a topic, with its own log |