SD System Design In Depth
0%
design system-designstreamsstorage

Design: a Kafka-style log

Module 6.1: Infrastructure & Core Service Designs Updated Sep 18, 2026
📄 Cheat Sheet
Kafka System Design Deep Dive w/ a Ex-Meta Staff Engineer
Hello Interview Kafka System Design Deep Dive w/ a Ex-Meta Staff Engineer Watch on YouTube ↗

Covers topics, partitions, keys, consumer groups, replication and retention from the angle of designing with and like Kafka. The current video is about database sharding.

0:00 / --:--

The Fundamental Abstraction: The Partitioned Append-Only Log

A distributed log (like Apache Kafka or Apache Pulsar) is an ordered, append-only sequence of immutable records. It serves as the durable event backbone of modern event-driven architectures, decoupling producers from consumers with multi-gigabyte/sec throughput.

Partitioning & Consumer Group Architecture

flowchart LR P["Producers"] --> T["Topic: Orders"] T --> P0["Partition 0: Append-Only Log"] T --> P1["Partition 1: Append-Only Log"] T --> P2["Partition 2: Append-Only Log"] P0 --> C1["Consumer 1: Offset Tracker"] P1 --> C2["Consumer 2: Offset Tracker"] P2 --> C3["Consumer 3: Offset Tracker"]

Why Append-Only Logs Achieve Immense Throughput

  • Sequential Disk I/O: Random I/O is slow on HDDs, and even on SSDs sequential writes are cheaper (less write amplification, better prefetching). Appending to the end of a log segment operates purely on sequential I/O, approaching raw physical disk bandwidth.
  • Zero-Copy Network Transfers: Traditional servers read data into kernel memory, copy it to application user-space memory, and copy it back to the socket buffer. Kafka leverages the Linux sendfile() system call (Zero-Copy), streaming data directly from OS PageCache into the network socket without CPU copying. This applies to plaintext listeners: with TLS enabled on the broker, data must be encrypted in user space and zero-copy is lost.
  • Batching: Producers and consumers batch messages together, amortizing network packet overhead and compression costs across thousands of records.

Offset-Based Consumer State

Traditional message queues (RabbitMQ classic and quorum queues, SQS) delete messages as soon as they are acknowledged by a consumer, incurring state overhead per message. In a distributed log, messages are retained for a configurable time window (e.g., 7 days). Consumers simply advance a lightweight 64-bit integer offset, allowing multiple independent consumer groups to read the same data at their own pace or replay historical events.

Worked example: sizing a cluster

  • Ingest: 500,000 messages per second x 1 KB = about 500 MB/s. With replication factor 3, the cluster writes 1.5 GB/s to disk and moves about 1 GB/s of replication traffic between brokers.
  • Retention: 7 days x 500 MB/s is about 302 TB of logical data, or about 907 TB raw. With 12 brokers that is about 76 TB each. Tiered storage (KIP-405, generally available in Kafka 3.9) can move older segments to object storage instead.
  • Partitions: if one consumer instance can process about 5 MB/s, you need at least 100 partitions to keep up. Choose 128 or more to leave headroom, because adding partitions later changes which partition each key maps to.
  • Consumer lag is the number to alert on: the latest offset minus the committed offset, per partition. Growing lag means consumers cannot keep up.

Log vs queue

PropertyKafka / Kinesis / Pulsar (log)RabbitMQ / SQS (queue)
ConsumptionOffset per consumer group; messages stay after being readMessage removed after acknowledgement (classic queues)
ReplayYes, within retentionNo, except RabbitMQ Streams
OrderingPer partitionPer queue (FIFO variants), weaker with competing consumers
Per-message routing and retriesLimited; you build retry topics yourselfRich: routing keys, dead-letter queues, delays
ThroughputVery high with batchingHigh, lower per node
Best forEvent streams, CDC, analytics fan-outTask queues and work distribution

Delivery semantics

flowchart LR P["Producer, idempotent, acks all"] --> L["Leader replica"] L --> F1["Follower 1, in ISR"] L --> F2["Follower 2, in ISR"] L -->|"high watermark"| C["Consumer reads committed offsets"] C --> O["Commit offset after processing"]
  • At-least-once (the default pattern): process the message, then commit the offset. A crash between the two causes reprocessing, so make handlers idempotent.
  • At-most-once: commit before processing. A crash loses the message.
  • Exactly-once within Kafka: idempotent producers plus transactions that atomically write the output and commit the input offsets (Kafka Streams exactly_once_v2). Side effects outside Kafka, such as sending email or charging a card, still need idempotency keys.
  • Metadata: Kafka 4.0 removed ZooKeeper entirely. Clusters now use the built-in KRaft Raft quorum for controller metadata.
Check yourself Question 1 of 3
Replaying for a new team recall
From: Offset-Based Consumer State

Billing has already consumed the last three days of order events from a Kafka topic with 7-day retention. A new analytics team wants those same three days. What do they do?

Queue Backpressure & Lag
Live
Queue Depth 0 / 100
Lag: 0.0s
Status: OK
Architecture Challenge: Kafka Consumer Lag & Partition Scalability
Staff Level
Context / Scenario: Kafka topic with 10 partitions. High message arrival volume.
Problem: A Kafka topic has 10 partitions. You deploy 15 instances in the same Consumer Group. How many consumer instances will actively process messages?
A The cluster will reject the deployment with a PartitionOverflowException.
B 1 instance will become the leader and process all 10 partitions alone.
C All 15 instances will process messages concurrently via round-robin.
D Exactly 10 instances will each read from 1 partition; 5 instances will sit idle as standby spares.

Key Takeaways

Append-only logs maximize performance by utilizing purely sequential disk I/O.
Linux Zero-Copy (sendfile system call) streams data from OS page cache directly to network sockets without user-space overhead.
Topics are partitioned to enable horizontal scaling and parallel consumption across consumer groups.
Consumers maintain their own offset state, allowing effortless replaying and multi-consumer independence.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue