Module 5: Async Processing
Message Brokers
Kafka vs RabbitMQ — logs vs smart brokers, delivery guarantees, ordering, DLQs, and when to pick each in system design.
Brokers decouple producers from consumers so spikes and slow workers do not stall user requests. Interviews care which broker mindset you pick — work queue vs replayable log — and how you handle duplicates.
What you will learn
- Explain why systems introduce a message broker
- Contrast RabbitMQ-style queues with Kafka-style logs
- State delivery guarantees and why consumers must be idempotent
- Describe ordering, partitions, and consumer groups
- Choose a broker class for a scenario and mention DLQs
Why use a broker?
- Synchronous chains couple latency and failure. A broker absorbs work and lets producers return quickly.
- Decouple producers from consumer speed and count
- Smooth traffic spikes — queue/buffer instead of dropping user requests
- Retry and delay failed work without blocking the API thread
- Fan-out one write to many independent consumers
- Cost: ops complexity, eventual processing, duplicate delivery to design for
Core vocabulary
- Align terms before comparing products.
- Producer / publisher — writes messages into the broker
- Consumer / subscriber — reads and processes messages
- Queue / topic / partition — how messages are organized and scaled
- ACK / offset commit — how progress and retries are tracked
- Command vs event — “do this work” vs “this fact happened” (deeper in the next lesson)
RabbitMQ-style (smart broker / queues)
- Classic message-oriented middleware: the broker routes and tracks messages for consumers.
- Exchanges route to queues via bindings (direct, topic, fanout, …)
- Competing consumers on a queue share work (work-queue pattern)
- Per-message ACK; unacked messages can be redelivered
- Flexible routing and priority/delay patterns are common strengths
- Great for task distribution: emails, image jobs, webhook delivery
Kafka-style (dumb broker / log)
- An append-only partitioned log. The broker stores the stream; consumers pull and track offsets.
- Topics split into partitions for parallelism
- Ordering is per partition, not global across the topic
- Consumer groups: each partition goes to one consumer in the group
- Replay: rewind offsets to reprocess history (within retention)
- Excellent for high-throughput event streams, analytics pipelines, CDC
Queues vs logs — quick comparison
- Use this side-by-side when asked Kafka vs RabbitMQ (as archetypes).
- Primary model — queue: consume & ACK remove/advance work; log: retain & replay by offset
- Routing — queue: rich broker routing; log: produce to topic/partition, consumers choose
- Throughput — logs often win for huge sustained streams
- Ops mental model — queue: message lifetimes; log: retention, partitions, consumer lag
- Interview move: pick by workload shape, not by résumé fashion
Delivery guarantees
- Most brokers give at-least-once in practical setups. Exactly-once needs extra care.
- At-most-once — may lose messages; simplest, rarely OK for money/side effects
- At-least-once — may duplicate after retries; default assumption in interviews
- Exactly-once — end-to-end needs idempotent writes / transactional outbox + careful sinks
- Network blips and worker crashes are why duplicates appear
- Design consumers so processing twice does not double-charge or double-email
Ordering & partitioning
- Total global order is expensive. Scope order to a key.
- Same partition/key ⇒ ordered processing for that key (e.g. orderId)
- Different partitions process in parallel — no cross-partition order
- More partitions ⇒ more parallelism; too many ⇒ overhead
- Hot keys can overload one partition — same skew lesson as sharding
- If order matters across entities, say so and accept the bottleneck
Failures, retries, and DLQs
- Poison messages and bad payloads must not block the whole queue forever.
- Retry with backoff for transient errors (timeouts, 503s)
- Dead-letter queue/topic (DLQ) for poison messages after N failures
- Alert on DLQ depth — silent DLQ is a production foot-gun
- Idempotency keys / dedupe tables for side-effecting consumers
- Visibility timeouts / ACK deadlines: crash before ACK ⇒ redelivery
Backpressure & lag
- Producers can outrun consumers. Lag is a first-class metric.
- Scale consumers horizontally (competing consumers / more group members)
- Slow the producer or shed load when lag exceeds SLO
- Separate critical queues from bulk/analytics so noisy jobs do not starve
- Batch or parallelize carefully without breaking per-key order needs
- Lag dashboards beat “is the consumer running?” alone
When to pick which
- Map workload to broker mindset before naming a vendor.
- Work queue / task fan-out / RPC-ish jobs → RabbitMQ-style queues
- Event stream, replay, many independent readers, high throughput → Kafka-style log
- Simple cloud task queues (SQS-like) often enough for basic async jobs
- You can use both: commands on a queue, domain events on a log
- Ops skill and existing platform matter as much as features
Interview framing
- Use this order when async processing appears in a design.
- Show the sync API path returning after enqueue
- Name queue vs log mindset and why
- State at-least-once + idempotent consumer
- Define partition/routing key if order matters
- Add DLQ + lag monitoring in one sentence
Quick revision checklist
- Always mention consumer idempotency.
- Call out poison messages / DLQ.
- Pick queue vs log by workload, not hype.
- Ordering is per partition/key — say it.
- Watch consumer lag as an SLO signal.
- Prefer at-least-once as the default delivery story.
FAQs
Can Kafka replace RabbitMQ?
Sometimes, but operational models differ. Prefer the tool that matches delivery semantics, routing needs, and ops skill — not hype.
Do brokers give exactly-once delivery?
End-to-end exactly-once is hard. Plan for at-least-once and make consumers idempotent; stronger guarantees need transactions/outbox patterns and careful sinks.
How do I get ordering with Kafka?
Put all messages that must stay ordered in the same partition (same key). There is no cheap global order across partitions.
What is a dead-letter queue?
A holding place for messages that fail repeatedly. It keeps poison payloads from blocking healthy traffic while you inspect and replay them.
Queue vs pub/sub?
A work queue usually delivers each message to one competing consumer. Pub/sub fans one event out to many subscribers. Logs often support many consumer groups reading the same stream.
When should I not use a broker?
When the work must finish in the user request with a strong sync answer and low complexity — e.g. a simple read. Do not add a broker just to look distributed.
Learner’s Task
- Practice out loud for 8–10 minutes. Design async processing for one user action.
- Pick an action (place order or upload image) and split sync API response vs async worker steps.
- Choose queue-style vs log-style broker and justify with workload shape.
- State delivery guarantee + how the consumer stays idempotent.
- Define ordering key (if any), retry policy, and DLQ behavior.
- Write a 4-sentence closing: why broker, which mindset, duplicate handling, and lag/DLQ ops note.