skip to content
jaeaeichsysdes

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 orders topic at independent speeds.
  • Time-travel replay: after fixing a corrupted search index, reset that group’s offset to 0 and 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

event log · 3 partitionsretained: 6 · queue deleted: 6
P0
0ord-415
orders @1 · analytics @1
P1
0ord-1011ord-101
orders @2 · analytics @2
P2
0ord-2041ord-3092ord-204
orders @3 · analytics @3
consumer group: orders-svcprocessed: 6 · replays: 0
C1P0
C2P1
C3P2
C4IDLE · 0 partitions

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_id table (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?

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.

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.

esc
⌘B sidebar[ ] prev/next1–5 flowsc copyt theme