Skip to content
Preptima
hardCodingDesignMidSenior

Build an in-memory pub-sub with topics, many subscribers per topic and at-least-once delivery. How does a message get redelivered?

An in-memory pub-sub with at-least-once delivery stores each topic as an append-only log and gives every subscriber its own cursor and in-flight set. A message stays in flight until acknowledged and is redelivered after an ack timeout, so a slow subscriber only delays itself and every handler must tolerate duplicates.

6 min readUpdated 2026-10-10

Answer

Short answer

Give each subscriber its own position. An in-memory pub-sub with at-least-once delivery stores every topic as an append-only log, and each subscription keeps a cursor plus a set of in-flight offsets with ack deadlines. A message leaves the in-flight set only when the subscriber acknowledges it; if the deadline passes first, it is delivered again. That design isolates slow subscribers, keeps publish order for first deliveries, and means handlers must be idempotent because duplicates are part of the contract.

Was this answer helpful?

Likely follow-ups

  • A handler takes longer than the ack timeout. What does the subscriber see, and how should it cope?
  • Two subscribers on one topic want to share the work instead of each receiving every message. What changes in your classes?
  • A subscriber has been stalled for an hour and memory is climbing. Which choice keeps at-least-once, and which breaks it?
  • How would you guarantee order per customer id without forcing the whole topic through one message at a time?

Related questions