Part 6 of 6 · 2 min read
Exercises
Easy
Easy 1: Write the requirements (20 minutes)
Write seven numbered requirements for a message system and three questions you would ask.
Done when: seven rules and three questions exist, and two features are marked out of scope.
Hint
Think about what happens when a subscriber is slow or fails.
Easy 2: Run the broker (20 minutes)
Type the code and reproduce the first three rows of the run table.
Done when: billing and email both print [0:o1, 1:o2], and audit sees three messages.
Hint
The test subscriber can add strings to a list.
Easy 3: Follow the offsets (20 minutes)
Topic "t" gets messages a, b, c, d. Subscriber S has polled once with a batch of 3. Give S's position, then the messages it gets on the next poll.
Done when: you write position 3, then the single message d at offset 3.
Hint
A batch of 3 from offset 0 covers offsets 0, 1 and 2.
Medium
Medium 1: Start at the end (45 minutes)
Add an option so a new subscriber starts at the current end of the topic and sees only new messages.
Done when: a test shows the new subscriber missing old messages and receiving later ones.
Hint
Read the topic size at subscribe time.
Medium 2: Filtering (45 minutes)
Let a subscriber give a predicate on key. Only matching messages are delivered, but the position still moves past the others.
Done when: a test with three keys shows delivery of one key and a position that equals the topic size.
Hint
Skip the call but still advance next.
Medium 3: Dead list replay (45 minutes)
Add a method that re-publishes all messages in a consumer's dead list to a named topic.
Done when: a test sends the dead messages to "retry" and an ordinary subscriber of "retry" receives them.
Hint
The dead list already holds whole messages.
Hard
Hard 1: Blocking read (90 minutes)
Make poll wait up to a time limit for new messages, using a condition on the topic. Wake waiting readers when a message is appended.
Done when: a reader thread wakes within a few milliseconds of a publish, and a time-out returns an empty batch.
Hint
Condition.await with a time limit.
Hard 2: Partitions (90 minutes)
Split a topic into 4 partitions, choosing the partition by key hash. Show that messages for one key stay in order, and that publishing from 8 threads is faster than with one lock.
Done when: a test checks per-key order, and a table compares timings.
Hint
Math.floorMod(key.hashCode(), 4) picks the partition.
Hard 3: Retention (90 minutes)
Keep only the last 1,000 messages in a topic. Offsets must keep counting up, and a consumer behind the first kept message must jump forward.
Done when: a test with 5,000 messages shows the first valid offset moving, and a slow consumer resuming at it.
Hint
Keep a base offset, and subtract it when reading the list.