Queues vs Event Logs
Deriving work queues and partitioned event logs from first principles: competing consumers, consumer groups, offsets, replay, and backpressure.
Synchronous HTTP (POST /send-receipt) multiplies availability and latency
across services: a 30-second stall in Service B blocks A’s worker threads and
creates a cascading SPOF. An asynchronous broker breaks that coupling
by durably buffering messages — splitting into two opposite consumption models.
Work queues vs append-only event logs
Work queues (RabbitMQ, SQS)
Every message is a one-time task pulled by competing workers and deleted
on ack.
- Strengths: balances uneven job durations (a 10-minute 4K transcode never stalls shorter jobs), per-message retry timers, and unbounded worker scaling with no partition rebalancing.
- Limits: once acked, the message is gone — no new downstream subscribers and no replay to rebuild buggy state.
Partitioned event logs (Kafka, Redpanda, Pulsar)
Every message is an immutable fact appended to segmented disk files and retained by time (e.g. 7 days) or size. Each consumer group tracks its own monotonic integer offset per partition.
1 owner
max consumers per partition in a group
O(1)
sequential append & offset seek
N groups
independent fan-out & replay
- Zero-copy fan-out: billing, analytics, and fraud all read the same
orderstopic at independent speeds. - Time-travel replay: after fixing a corrupted search index, reset that
group’s offset to
0and reprocess the log.
Partition ordering vs parallelism
To scale past one sequential reader, event logs split a topic into N
partitions (P0, P1, P2, …):
- Per-key ordering: strictly ordered within a partition, never across
them — hash a key (
order_id,user_id) so one entity’s events stay in order. - Parallelism ceiling: inside one consumer group, each partition has at most one consumer; on a 3-partition topic, a 4th consumer sits IDLE.
Adjust consumers below on a 3-partition topic, publish events, and click replay from offset 0.
interactive · adjust consumers on 3 partitions, publish events, or replay from offset 0
consumers: 4 / 3 partitions · 1 IDLEmax parallelism = partition count
note
Avoid over-partitioning: each partition adds file handles, leader-election cost, and rebalance latency. Size for peak parallelism (typically 2×–3× current consumers), not 10,000 partitions.
Delivery guarantees, idempotency, and dual writes
If a crash or packet drop happens after processing but before the ack or
offset commit, the broker redelivers the message:
- At-most-once: commit before processing — fast, but crashes drop messages.
- At-least-once: commit after processing — zero loss, but duplicates occur.
- Idempotency: pair at-least-once with a unique-constrained
event_idtable (or natural-key upsert) so redelivery is a safe no-op. - No dual writes: never write to your DB and Kafka in two separate calls — a crash between them causes permanent drift. Use the outbox pattern to commit both in one local DB transaction, then relay asynchronously.
Backpressure and Dead Letter Queues
An unbounded queue is just a delayed OOM crash. Guard pipelines with two valves:
- Backpressure: bound buffers and track consumer lag (latest offset minus committed offset); when lag breaches SLA, shed load, throttle producers, or autoscale consumers up to the partition count.
- Dead Letter Queues (DLQs): stop malformed “poison pill” messages from blocking a partition in an infinite crash loop — after k exponential-backoff retries, route payload + stack trace to a DLQ.
System design decision matrix
| Requirement | Best fit | Why |
|---|---|---|
| Long-running background jobs (video encoding, PDF generation) | Work queue (SQS / RabbitMQ) | Per-message ack and visibility timeouts balance uneven job durations across any number of workers |
| Multi-team fan-out, audit trail, or rebuilding derived state | Partitioned event log (Kafka) | Immutable retention and independent consumer-group offsets allow replay without touching producers |
| Strict per-entity ordering (bank ledger, chat room, order state) | Partitioned log keyed by entity ID | Hashing entity_id guarantees all events for one entity land on one ordered partition |
| Unbounded worker scaling without partition limits | Work queue | Competing consumers are not capped by a fixed partition count |
Where this shows up in system design
- Uber trip events fan-outTrip lifecycle events land in a partitioned event log so dispatch, surge pricing, receipts, and fraud models consume independently.
- Video transcoding job queuesChunked transcoding tasks sit in a work queue so spot workers pull jobs, ack on completion, and scale elastically without partition limits.
- Real-time analytics / clickstreamsHigh-throughput append-only logs buffer millions of pageviews per second for stream processors and warehouse ingestion with replay.
- Notification & email pipelinesPriority queues decouple user-facing APIs from slow SMTP/push providers, routing persistent failures to a Dead Letter Queue.
Check yourself: system design scenarios
interactive · pick an architecture for each prompt to test your intuition
1. Consumer lag stuck after scaling pods
6 partitions · 1 consumer group · 12 pods
Your Kafka topic `orders` has 6 partitions. Consumer lag is spiking during a flash sale, so the on-call engineer scales the `orders-svc` deployment from 6 pods to 12 pods — yet throughput does not budge. Why?
Select an option to check the trade-off.
2. Video transcoding with wildly uneven job times
uneven job duration · 200 workers · no replay needed
Users upload videos ranging from 5-second clips to 2-hour 4K streams. You want 200 stateless workers to pull jobs, retry crashed workers after a visibility timeout, and delete jobs once encoded. No other service needs the job payload.
Select an option to check the trade-off.
3. Rebuilding a corrupted search index
48h replay · independent teams · no producer changes
A bug in your search indexer dropped product attributes for the past 48 hours. You fixed the code and now need to reprocess all product update events from the last 2 days without asking the catalog service to resend anything or disrupting the billing service.
Select an option to check the trade-off.
SPOFreliability
Single Point of Failure: any component whose failure stops the whole system because there is no independent fallback.
monotonicordering
Always moving in one direction: every newly generated value is strictly greater than (or never less than) the previous one.
idempotencyreliability
Property where repeating the exact same request multiple times produces the same final state as executing it once.
outbox patternmessaging
Writing a domain change and its outgoing event to the same DB transaction, then relaying the event asynchronously to avoid dual writes.
backpressuretraffic
Signaling upstream producers to slow down or shed load when downstream consumers cannot keep up, preventing unbounded queues.