FullStack AI Hub
Studio
System Design · 04. Communication
12 min
10 steps
Intermediate

Event Streaming & Logs

Fan out immutable partitioned commit logs to independent consumer groups with offset replay and lag protection.

System Design04. CommunicationLesson 3 of 4
12 minIntermediate10 steps

Event Streaming & Logs

Fan out immutable partitioned commit logs to independent consumer groups with offset replay and lag protection.

EVENTS / SEC
3,200/s
TEAMS SERVED
1 of 3
REPLAYABLE
No (deleted)
DELIVERY P95
18 ms
OrderPlacedpop & deletealready gonealready goneOrders API34%900Classic Queue42%Delete on ACKBillingWorker48%gets 100%Analytics(Starved)0%0 eventsSearch(Starved)0%0 eventsDESTRUCTIVE QUEUE READLifecyclePop → ACK → DeletedLimitCannot fan out or replay
event append
consumer read
offset replay
hot partition / lag
consumer stall
In This LessonStep 1 of 10
What You Will Master
  • ✓Distinguish destructive point-to-point queues from immutable append-only commit logs
  • ✓Scale throughput with keyed partitions and understand the 1-partition-per-consumer-group rule
  • ✓Diagnose hot-partition skew and consumer lag before retention windows expire
  • ✓Use offset rewinding, time retention, and log compaction to recover from bugs and feed downstream systems

Architectural Trade-Off Comparison Matrix

PatternBest ForKey Trade-Off
Append-Only Commit Log (Kafka)Fanning out one immutable event stream to many independent downstream consumers (Billing, Analytics, Search)Brokers must manage disk retention, replication, and partition leadership
Keyed Partitions + Consumer GroupsHorizontal throughput while preserving strict message ordering per entity key (e.g., `order_id`)Max parallelism per consumer group equals partition count; skewed keys create hot partitions
Offset Replay (Time Travel)Recovering from a downstream consumer bug or bootstrapping a brand-new Search index from historical eventsConsumers must be idempotent because replaying offsets re-delivers previously processed events
Log CompactionChangelog topics (user profiles, config state) where you only need the latest value per key foreverLoses intermediate historical mutations once background compaction cleans older segments

Step-by-Step Architectural Breakdown (10 Steps)

Full architectural walkthrough transcript and state transitions for Event Streaming & Logs.

STEP 01 · SETUPJump to step →

Why a classic queue breaks multi-team fan-out

In a classic point-to-point **Message Queue**, once the **Billing** worker pops and acknowledges an `OrderPlaced` message, the broker deletes it immediately.

When **Analytics** and **Search** ask to read those same order events, the data is already gone—forcing producers to hardcode duplicate pushes to every new downstream team.

watch:Destructive queues are great for background job work-sharing, but poor for broadcasting domain facts to multiple independent systems.

Destructive ReadsPoint-to-Point QueueProducer Coupling
STEP 02 · SOLUTIONJump to step →

Switch to an Append-Only Commit Log

An **Event Streaming Platform** (like Apache Kafka or Redpanda) replaces destructive queues with an **immutable, append-only commit log** stored sequentially on disk.

Every `OrderPlaced` event receives a monotonically increasing **Offset** (`0, 1, 2, 3…`). Sequential disk appends hit gigabytes per second via OS page cache and stay readable for days.

idea:Reading from a commit log never deletes the record—it only advances a tiny 8-byte integer pointer called an offset.

Append-Only LogMonotonic OffsetZero-Copy Disk I/O
STEP 03 · TECHNIQUEJump to step →

Fan-out: independent consumers at their own pace

Now **Billing**, **Analytics**, and **Search** all subscribe to the same `orders.events` topic. Each team tracks its own **committed offset** independently.

Billing stays real-time at offset `#1043`, Search indexes steadily at `#1040`, and an Analytics batch job catches up from `#980`—with zero interference between teams.

idea:Decoupled offsets mean a slow Analytics warehouse never backpressures or slows down real-time Billing.

Pub/Sub Fan-OutIndependent OffsetsProducer Decoupling
STEP 04 · SOLUTIONJump to step →

Scale out with Keyed Partitions

A single log file eventually hits one broker’s NIC and disk ceiling. Split the topic into **Partitions 0, 1, and 2** spread across separate broker machines.

Producers hash a **partition key** (`hash(order_id) % 3`) so every event for the same order (`Created → Paid → Shipped`) lands in the **exact same partition**, guaranteeing strict per-order ordering while tripling cluster throughput.

idea:Kafka guarantees strict ordering ONLY within a single partition—never globally across partitions. Always pick a partition key that matches your ordering unit.

Topic PartitionsPartition Key HashingPer-Key Ordering
STEP 05 · TECHNIQUEJump to step →

Consumer Groups: 1 partition = max 1 active worker

To process all 3 partitions in parallel inside Billing, spin up 3 instances sharing `group.id = "billing"`. Kafka assigns **exactly one partition to each worker** (P0→C1, P1→C2, P2→C3).

If a worker crashes, the group **rebalances** its partition onto a survivor. Crucially, spinning up a **4th worker** leaves it completely idle because two workers in the same group can never read the same partition concurrently.

idea:Rule of parallelism: your partition count is the hard ceiling on active consumers inside a single consumer group.

Consumer GroupsPartition OwnershipRebalancing
STEP 06 · PROBLEMJump to step →

The hot-partition trap: skewed keys & consumer lag

Suppose someone chooses `merchant_id` as the partition key instead of `order_id`. When a mega-retailer runs a flash drop, **80% of all events hash to Partition 1**.

Consumers C1 and C3 sit nearly idle at 15% CPU while **Consumer C2 drowns**, building **180,000 messages of Consumer Lag**. Adding more workers won’t help—only salting the key or choosing a higher-cardinality key (`order_id`) fixes the skew.

watch:Watch Consumer Lag (`latest_offset - committed_offset`) per partition, not just cluster average—one hot partition can fall hours behind while averages look green.

Hot Partition SkewConsumer LagKey Cardinality & Salting
STEP 07 · SOLUTIONJump to step →

Superpower: Offset Replay (Time Travel Recovery)

At 2:00 PM you discover a tax-rounding bug deployed to **Billing** at noon. In a traditional queue, those 2 hours of events are lost forever.

With an immutable commit log, you simply deploy the fixed Billing binary, reset its consumer group offset back to **12:00 PM (`offset #42,000`)**, and deterministically **replay** every order with idempotent upsert keys.

idea:Because replay delivers historical events a second time, downstream consumers MUST be idempotent (e.g., deduplicating by `event_id`).

Offset RewindDeterministic ReplayIdempotent Consumers
STEP 08 · TRADE-OFFJump to step →

Managing disk growth: Time Retention vs Log Compaction

Logs cannot grow forever. For high-volume telemetry or order streams, use **Time/Size Retention** (e.g., drop segments older than 7 days after archiving to S3).

For entity state topics like `user.profiles`, enable **Log Compaction**: the broker retains the **latest value for every key** forever and deletes superseded older versions—letting any new service rebuild a full cache straight from the topic.

idea:Time retention keeps a rolling history window; Log Compaction turns a Kafka topic into a durable key-value changelog.

Time RetentionLog CompactionTombstone Deletes
STEP 09 · SOLUTIONJump to step →

End-to-end streaming: CDC → Log → Stream Processor → Sinks

In production, **Change Data Capture (CDC)** tails the primary database’s WAL and publishes every committed row into a partitioned Kafka cluster—eliminating dual-write inconsistencies.

Downstream **Stream Processors** compute 1-minute tumbling fraud windows in real time while independent connectors fan out the same stream into Search, Billing, and Analytics.

idea:Tailing the database WAL via CDC guarantees that every committed DB transaction appears in the event stream—zero lost writes.

Change Data Capture (CDC)Stream ProcessingTumbling Windows
STEP 10 · RECAPJump to step →

Recap: the Event Streaming Mental Model

Event streaming turns your company’s state changes into a **durable, replayable nervous system** instead of fragile point-to-point API calls.

Remember the four rules: **Logs don’t delete on read**, **ordering is per-partition only**, **max active workers per group = partition count**, and **every replayable consumer must be idempotent**.

idea:Pick a Queue (SQS/RabbitMQ) for simple competing background jobs; pick an Event Log (Kafka) for multi-team fan-out, ordered streams, and offset replay.

Commit LogPartitionsConsumer GroupsOffset Replay

Related Production Deep-Dives & Simulators

Related Systems Course
Free · Verifiable Credential

FS-401RAG Engineer

Build evidence-carrying retrieval systems from ingestion and provenance through search reranking grounded generation and evaluation.

21 concepts~25h self-paced
Start Free Course
Weekly Engineering Dispatch

Get the weekly systems teardown

One technical note per week covering inference bottlenecks, retrieval harnesses, and commit-pinned teardowns.