Part 2 · Blueprint 07
Message Queues & Event Streams: Kafka vs. RabbitMQ
15 min reading · Sources · Editorial review pending
Removing one job and retaining a history serve different recovery needs.
Page details
Published date · Last edited:
Draft edition. Editorial review and supplementary reading recommendations are still being completed.
Format: Architecture walkthrough. Prerequisites: the beginner bootcamp. Chapter prerequisites: Back-of-Envelope Capacity Estimation, SQL vs. NoSQL vs. NewSQL Storage Engines.
Sixty-second mental model
Problem: Removing one job and retaining a history serve different recovery needs.
Use: Choose delivery, ordering and retention before the broker brand.
Design boundary: Separate work queues from retained logs. A work queue distributes unfinished jobs; a retained partition log lets consumers track positions and replay within retention. Ordering and redelivery guarantees apply at specific boundaries.
Layer 1 · Intuition
Hypothetical scenario: Northwind is designing queues and streams. Maya represents product requirements, Dev implementation and Priya reliability. The team must agree what one message means and which component owns the broker position.
Think of a task tray compared with a replayable newspaper archive. Separate the requester from broker position, then identify the retained evidence needed after failure.
- Authority
- The entitled state owner.
- Invariant
- A rule preserved across concurrency and failures.
- At-least-once delivery
- A delivery guarantee allowing duplicates while requiring delivery under stated assumptions. Analogy: A courier may deliver a parcel again rather than risk omission.
- Broker
- A service mediating message or event publication and delivery. Analogy: A dispatch office coordinating senders and receivers.
- Consumer
- A component receiving and processing messages or records. Analogy: The worker collecting tasks from the tray.
- Consumer group
- A coordinated set of consumers sharing assigned work under a broker's rules. Analogy: A crew splitting responsibility for delivery routes.
- Consumer lag
- Distance between a consumer's progress and a defined stream position. Analogy: How far a reader is behind the latest journal entry.
Architecture and worked reasoning
Design boundary: Separate work queues from retained logs. A work queue distributes unfinished jobs; a retained partition log lets consumers track positions and replay within retention. Ordering and redelivery guarantees apply at specific boundaries.
Responsibilities on the request path
Workload and limiting resource
Choose arrivals of 800 jobs/s and service capacity of 1,000 jobs/s. A 60,000-job backlog drains at 1,000 − 800 = 200 jobs/s, taking 300 seconds under the fixed-rate assumption. Dividing by 1,000 ignores new arrivals. Retaining 800 × 86,400 events/day at 300 bytes is 20.736 GB/day before replication.
Keep the units attached to each intermediate result. The assumptions above describe a hypothetical exercise, not observed vendor capacity. A real capacity decision also needs a measured service curve, skew, recovery traffic and an explicit admission policy for overload.
Mutation or publication path
- Commit domain mutation and outbox intent together.
- Publish with stable event identity.
- Advance progress only after the selected effect/recovery boundary.
Observation and recovery path
- Read assigned partition positions.
- Apply the domain effect idempotently.
- Retain replay and recovery evidence for the promised horizon.
Failure that changes the decision
Hypothetical scenario: A consumer commits its offset before its external payment effect succeeds. Replay can no longer recover the skipped effect from that progress record; join effect and progress where possible or preserve a separate durable obligation.
Attempt this decision before reading the explanation
Does broker exactly-once processing prove that an email or payment happened once?
No. The external service is outside the broker transaction unless its documented contract participates. Preserve logical identity and reconcile uncertain remote effects.
Supporting mechanism analysis
Hypothetical scenario: Northwind. Maya represents product, Dev implementation and Priya reliability. The following workflow develops the underlying mechanism.
Hypothetical scenario. At Northwind, Promotion traffic creates more email work than Northwind can deliver immediately.
In short. Separate accepted work from completed work. Name delivery, ordering and acknowledgement semantics.
Hypothetical scenario. A catalogue update should refresh a search view and notify a seller. Waiting for all downstream work would delay the response, so the application places follow-up work in a queue. The user now needs to know whether the update was accepted, whether processing finished and what happens when a worker fails.
A queue separates the producer's progress from a consumer's progress. It does not remove work or make its effects occur exactly once. A useful design states the acceptance boundary, acknowledgement meanings, backlog limits, retry behavior and how readers observe unfinished processing.
Buffers can absorb a temporary mismatch between arrivals and processing. If sustained work arrives faster than it can be completed, a queue grows until something changes: demand falls, processing increases, work is discarded or admission is limited. Moving a wait from an HTTP request into a broker does not make it disappear.
Acknowledgements also have a particular scope. The AMQP 1.0 transport specification defines delivery state and settlement between communicating nodes. These mechanisms answer different questions; neither automatically includes an external business effect performed by a consumer.
Define the visible state honestly. An accepted background job can be pending, running, completed or failed under the application's contract. Returning “completed” when only a message was accepted hides uncertainty and makes recovery harder to reason about.
Consider which state change must survive together with the intention to do downstream work. One pattern records the authoritative update and an outbox record in the same local database transaction. A dispatcher later publishes the outbox item and marks its progress. This avoids treating two independent writes as one atomic operation, but the dispatcher can still publish duplicates.
The consumer needs an identity for the intended logical operation. If duplicate delivery must not duplicate the effect, enforce that requirement at the effect boundary. Recording “seen” before applying an effect can lose work; recording it afterward without atomic protection can repeat work after a crash. When the effect is external, inspect that service's supported identity and reconciliation contract.
Hypothetical scenario. An order service must notify a buyer after an order is accepted. The product supports email and an in-application inbox. It requires a durable inbox entry for each accepted notification intent; email is a separately tracked delivery attempt. Provider acceptance is not proof that a person received or read a message.
Keep order acceptance and notification intent in one authoritative transaction using an outbox. A publisher retries moving the intent into the delivery path. The notification service resolves the current recipient and channel policy, then creates a channel-specific operation identity from the intent and channel. It records inbox creation and its deduplication state together where the selected store permits that atomic boundary. Email uses the provider's documented identity and status capabilities; a missing response becomes unresolved rather than automatically failed.
Choose 100 accepted orders per minute. Assume exactly 60 require both channels and 40 require only the inbox. Inbox demand is 60 + 40 = 100 operations per minute; email demand is 60. Total channel operations are 100 + 60 = 160 per minute. The fan-out is therefore not simply the number of orders. If six email operations each require one additional attempt, the email path handles 60 + 6 = 66 attempts while still representing 60 logical notifications.
Now choose a ten-minute email-provider interruption. At 60 new email intents per minute, the backlog grows to 60 × 10 = 600, assuming none completes during that interval. After recovery, suppose the permitted processing budget is 100 successful logical notifications per minute while 60 new ones continue arriving. Net drain is 100 − 60 = 40 per minute, so the illustrative backlog needs 600 ÷ 40 = 15 minutes to clear. This budget is a chosen assumption, not a measured provider quota. Unresolved attempts, retries and rejected requests can make actual completion slower.
Separate channel worker pools and bound their queues so an email outage does not consume the resources needed for inbox creation. Preserve notification age, expiry and recipient-policy checks. A stale promotional message may be discarded under a declared policy; a required order-status message may instead need reconciliation or an alternate authorized channel. Do not silently apply the same expiry rule to both.
A cancellation can race with a prepared send. State whether the notification represents the original accepted event or the latest order status, and recheck the state needed by that contract. A later cancellation does not make an already delivered email disappear. Likewise, updating a preference does not prove that every in-flight provider request has stopped.
Evaluate accepted intents, inbox effects, provider-confirmed outcomes, unresolved attempts, queue age and expired work separately. Alert on the user-relevant lag and recovery capacity, not only broker availability. Reject a synchronous provider call inside order acceptance unless the business deliberately accepts its availability dependency. Reject unlimited retry and an “exactly once delivered” promise that the external channel cannot establish.
Design exercise. The provider returns success, but the worker crashes before recording it. Explain the next transition before continuing. The safe answer depends on the provider's actual operation identity and reconciliation contract: reuse that evidence when available, otherwise retain uncertainty and avoid inventing a universal safe retry. A broker acknowledgement alone cannot resolve an email side effect.
A Northwind price change creates work that can be accepted now and applied later. Kafka retains a partitioned event log and allows consumers to track their progress. A work queue such as SQS exposes a different delivery lifecycle, while RabbitMQ uses acknowledgement and routing concepts suited to message handling. These mechanisms decide how work reaches a consumer; the catalogue's accepted price version still belongs to the application's state contract.
Suppose, hypothetically, updates arrive at 120 jobs per second while workers finish 100. The excess is 120 − 100 = 20 jobs per second. Over ten minutes, backlog grows by 20 × 600 = 12,000 jobs. Doubling the buffer allows more waiting, but does not change that growth rate. If arrivals later fall to 60 and completion remains 100, the net drain is 100 − 60 = 40 per second, so the same backlog takes 12,000 / 40 = 300 seconds to clear.
Layer 3 · Internals
Trade-offs and limits
| Approach | Provides | Requires |
|---|---|---|
| Work queue | Shares consumable tasks among workers. | Replay and multiple independent views may need separate retention mechanisms. |
| Retained event stream | Supports positions and replay for independent consumers. | Partition ordering, retention and consumer progress remain operational obligations. |
Recovery decision
Hypothetical scenario: A consumer commits its offset before its external payment effect succeeds. Replay can no longer recover the skipped effect from that progress record; join effect and progress where possible or preserve a separate durable obligation.
No. The external service is outside the broker transaction unless its documented contract participates. Preserve logical identity and reconcile uncertain remote effects.
Write your design before revealing the review
Does broker exactly-once processing prove that an email or payment happened once?
Drafts and self-review are stored only in this browser. They are separate from reading completion.
Compare your decision
No. The external service is outside the broker transaction unless its documented contract participates. Preserve logical identity and reconcile uncertain remote effects.
A consumer commits its offset before its external payment effect succeeds. Replay can no longer recover the skipped effect from that progress record; join effect and progress where possible or preserve a separate durable obligation.
Check your understanding
Does broker exactly-once processing prove that an email or payment happened once?
No. The external service is outside the broker transaction unless its documented contract participates. Preserve logical identity and reconcile uncertain remote effects.
How would you recover from this failure: A consumer commits its offset before its external payment effect succeeds. Replay can no longer recover the skipped effect from that progress record; join effect and progress where possible or preserve a separate durable obligation.
Start with the exact boundary that failed. Commit domain mutation and outbox intent together. Publish with stable event identity. Advance progress only after the selected effect/recovery boundary. Then recover evidence through the observation path: Read assigned partition positions. Apply the domain effect idempotently. Retain replay and recovery evidence for the promised horizon. Preserve the distinction between a confirmed state change and an unavailable response.
Which assumption in the worked calculation would a burst or concentrated tenant invalidate first?
Choose arrivals of 800 jobs/s and service capacity of 1,000 jobs/s. A 60,000-job backlog drains at 1,000 − 800 = 200 jobs/s, taking 300 seconds under the fixed-rate assumption. Dividing by 1,000 ignores new arrivals. Retaining 800 × 86,400 events/day at 300 bytes is 20.736 GB/day before replication. Recompute the affected resource rather than multiplying unrelated storage and network budgets by the same factor. Separate work queues from retained logs. A work queue distributes unfinished jobs; a retained partition log lets consumers track positions and replay within retention. Ordering and redelivery guarantees apply at specific boundaries.
Which component in this topology could be removed from a smaller first deployment?
Outbox, Broker partitions, Consumer group, Effect store name responsibilities, not a requirement for separate processes. Co-locate responsibilities when their transaction, trust and capacity boundaries permit it. Commit domain mutation and outbox intent together.
What evidence must a caller have before relying on the observation path?
Read assigned partition positions. Apply the domain effect idempotently. Retain replay and recovery evidence for the promised horizon. No. The external service is outside the broker transaction unless its documented contract participates. Preserve logical identity and reconcile uncertain remote effects.
45-minute interview rubric
- 00–05: scope message and broker position.
- 05–12: identify the limiting resource and derive the domain workload above, retaining units and assumptions.
- 12–28: draw broker position, numbered writes and reads.
- 28–40: challenge recovery: Choose delivery, ordering and retention before the broker brand.
- 40–45: defend event_id placement, observations and remaining assumptions.
Go deeper
- Design | Apache Kafka · Primary document · kafka.apache.org. Choose delivery, ordering and retention before the broker brand. Verified 2026-10-26.
- Amazon SQS visibility timeout - Amazon Simple Queue Service · Primary document · docs.aws.amazon.com. Choose delivery, ordering and retention before the broker brand. Verified 2026-10-26.
- OASIS Advanced Message Queuing Protocol (AMQP) Version 1.0, Part 2: Transport · Primary document · docs.oasis-open.org. Choose delivery, ordering and retention before the broker brand. Verified 2026-10-26.
- Introduction | Apache Kafka · Primary document · kafka.apache.org. Choose delivery, ordering and retention before the broker brand. Verified 2026-10-26.
- Consumer Acknowledgements and Publisher Confirms | RabbitMQ · Primary document · www.rabbitmq.com. Choose delivery, ordering and retention before the broker brand. Verified 2026-10-26.
Decision log
Choose delivery, ordering and retention before the broker brand.
Preserve message identity and broker position ownership.
Revisit changed workload, observations or recovery objectives.