This lesson on Redpanda & Structured Streaming in 10 Minutes is hands-on and example-driven. You'll be able to explain how an append-only log broker like Kafka decouples producers and consumers, scales through topics and partitions, and preserves per-key order. You'll also be able to configure consumer groups, offsets, replication, and retention for reliable streaming pipelines.
What You'll Be Able To Do
- Describe how an append-only commit log decouples producers from consumers.
- Explain how topics and partitions enable horizontal scaling.
- Choose a message key to preserve per-entity ordering.
- Configure a consumer group to parallelize reads without duplicate processing.
- Trace how offsets enable replay and independent consumers.
- Identify how replication and KRaft keep the cluster available.
Detailed Concept Walkthrough
1. Append-Only Commit Log
Kafka is not a traditional message queue; it is a durable, append-only log where producers add events to the end and consumers read at their own pace by tracking an offset.
- Mechanism: When a producer sends an event, Kafka appends it to the end of a partition log and assigns a monotonically increasing offset. The next event gets the next offset, and no event is ever rewritten or deleted just because it was read.
- Under the Hood: Appending to the end of a file is a sequential write, which is far faster than random disk seeks. Kafka also relies on the operating system's page cache, so recently written data is often still in memory when a consumer reads it.
- Zero Copy: When shipping data to consumers, Kafka uses zero-copy transfer, sending bytes directly from the system's memory to the network socket without copying them into Kafka's own memory.
- Best Practice: Treat the log as immutable. Retention is controlled by time or size limits, not by whether a consumer has read a message. This design enables replay and independent consumers.
# Produce events to an append-only log
kafka-console-producer.sh --topic orders --bootstrap-server localhost:9092
> {"order_id": "A1", "status": "created"}
> {"order_id": "A1", "status": "paid"}
# Consume from the beginning and print offsets
kafka-console-consumer.sh --topic orders --from-beginning \
--property print.offset=true --bootstrap-server localhost:9092
Key Takeaway: Kafka is a fast, durable append-only log; consumers read by tracking an offset, not by receiving per-message acknowledgements.
2. Topics, Partitions, and Keys
A topic is a named stream of events, and it is split into partitions so the data can scale across machines. A message key determines which partition an event lands in, preserving order for that key.
- Mechanism: A topic can grow too large for one machine, so Kafka breaks it into partitions. Each partition is an ordered log, and Kafka only guarantees order within a single partition.
- Key Hashing: Every event has a value and an optional key. Kafka hashes the key to a number and uses that number to choose a partition. The same key always lands on the same partition, so all events for one order ID stay in order.
- Ordering Guarantee: If you need every event for a specific entity processed in sequence, you must use that entity's ID as the key. Without a key, events are distributed round-robin and order is not preserved.
- Best Practice: Create extra partitions upfront. If you add partitions later, the hash calculation changes and the same key can start landing on a different partition, breaking ordering.
# Create a topic with 6 partitions and replication factor 3
kafka-topics.sh --create --topic orders --partitions 6 \
--replication-factor 3 --bootstrap-server localhost:9092
# Produce with a key so all events for order A1 go to the same partition
kafka-console-producer.sh --topic orders --property parse.key=true \
--property key.separator=: --bootstrap-server localhost:9092
> A1:{"order_id":"A1","status":"created"}
> A1:{"order_id":"A1","status":"paid"}
Key Takeaway: Partition by key to scale horizontally while preserving per-entity order; never add partitions if key ordering matters.
3. Consumer Groups and Offsets
Consumers track a single number—the offset—to know where they are in a partition. Consumer groups let multiple consumers share the work of reading a topic without processing the same message twice.
- Mechanism: A consumer periodically commits its offset back to Kafka, which stores it in a small internal topic. After a crash or restart, the consumer resumes from the last committed offset.
- Consumer Groups: A consumer group is a set of consumers working together on the same topic. Inside a group, each partition is assigned to exactly one consumer, so all consumers work in parallel without duplicate processing.
- Independence: Different consumer groups are completely independent. A fraud detection system can read every order in real time while an analytics job reads the same orders three days later; they never interfere.
- Replay: Because data is not deleted on read, you can move an offset backwards to reprocess data. For example, if you shipped a bug, you can replay the last week of orders.
- Best Practice: You can never have more active consumers than partitions in a group. If you have 10 consumers and 6 partitions, 4 consumers will be idle.
# Start a consumer group; Kafka assigns partitions automatically
kafka-console-consumer.sh --topic orders --group email-service \
--bootstrap-server localhost:9092
# Scale by adding another consumer to the same group
kafka-console-consumer.sh --topic orders --group email-service \
--bootstrap-server localhost:9092
Key Takeaway: Use consumer groups to parallelize reads; each partition is processed by exactly one consumer in the group, and offsets enable replay.
4. Replication and Fault Tolerance
Each partition is copied across several brokers so the log survives machine failures. One broker is the leader for a partition, and the others hold in-sync replicas.
- Mechanism: Producers always write to the leader for a partition. By default, consumers read from the leader too, but they can be configured to read from a nearby replica.
- Failover: If the leader's machine fails, one of the in-sync replicas is promoted to become the new leader. The cluster keeps running, and consumers continue from the offsets they already committed.
- Durability: A replication factor greater than one ensures that data is not lost if a single broker goes down. For critical topics, a replication factor of three is common.
- Best Practice: Monitor the in-sync replica (ISR) set. If a replica falls too far behind, it is removed from the ISR and cannot be promoted until it catches up.
# Create a topic with replication factor 3
kafka-topics.sh --create --topic payments --partitions 6 \
--replication-factor 3 --bootstrap-server localhost:9092
# Describe the topic to see leader and replicas per partition
kafka-topics.sh --describe --topic payments --bootstrap-server localhost:9092
Key Takeaway: Replication keeps the log available; leader election is transparent to consumers because they only track offsets.
5. Retention and KRaft Metadata
Kafka keeps data for a configurable retention window, regardless of whether it has been read. In Kafka 4, cluster metadata is managed by KRaft, a built-in Raft consensus protocol, replacing Zookeeper.
- Mechanism: You set retention by time (e.g., 7 days) or by size. Everything inside that window stays, whether or not it has been consumed. This is what enables replay and multiple independent consumers.
- KRaft: Kafka used to rely on a separate system called Zookeeper to track brokers and partition leaders. As of Kafka 4, the cluster manages this itself using KRaft, its own implementation of the Raft consensus protocol.
- Best Practice: Set retention based on how far back you might need to replay. If you need to reprocess a week of data, retention must be at least a week.
- Nuance: Retention is independent of consumer offsets. A consumer can still read data that is older than its committed offset as long as the data has not been deleted by retention.
# Set topic retention to 7 days (604800000 ms)
kafka-configs.sh --alter --topic orders \
--add-config retention.ms=604800000 \
--bootstrap-server localhost:9092
Key Takeaway: Retention is time- or size-based and independent of consumption; KRaft removes the Zookeeper dependency in Kafka 4.
Topics Covered in Redpanda & Structured Streaming in 10 Minutes
- Direct Service Calls (0:00 - 1:30) — Services calling each other creates coupling, failures, and traffic spikes.
- Append-Only Log (1:30 - 3:00) — Kafka writes events to a log and consumers read at their own pace.
- Topics and Partitions (3:00 - 4:30) — Topics are split into partitions for scaling; order is only guaranteed within a partition.
- Keys and Ordering (4:30 - 6:00) — Keys are hashed to partitions; same key preserves order, but adding partitions breaks mapping.
- Consumer Offsets and Groups (6:00 - 7:30) — Consumers track offsets; groups assign partitions for parallel, independent reading.
- Replication and Failover (7:30 - 9:00) — Partitions are replicated; leader failure promotes an in-sync replica.
- Performance and Retention (9:00 - 10:30) — Sequential writes, page cache, zero-copy; retention keeps data for a window.
- KRaft and Summary (10:30 - 12:00) — Kafka 4 uses KRaft instead of Zookeeper; summary of append-only log benefits.
Spark Lakehouse Cheat Sheet
-
Append-Only Log— Durable log where events are appended and never rewrittenkafka-console-producer.sh --topic orders --bootstrap-server localhost:9092 -
Topic— Named stream of events, split into partitionskafka-topics.sh --create --topic orders --partitions 6 --replication-factor 3 --bootstrap-server localhost:9092 -
Partition— Ordered chunk of a topic for horizontal scalingkafka-topics.sh --describe --topic orders --bootstrap-server localhost:9092 -
Message Key— Hashed to choose partition; preserves per-key order> A1:{"order_id":"A1","status":"created"} -
Consumer Group— Set of consumers sharing partition assignments without duplicateskafka-console-consumer.sh --topic orders --group email-service --bootstrap-server localhost:9092 -
Offset— Consumer's position in a partition; enables replaykafka-console-consumer.sh --topic orders --property print.offset=true --from-beginning -
Replication Factor— Number of copies of each partition across brokers--replication-factor 3 -
Retention— Time or size limit for keeping events in the logretention.ms=604800000
Comparison Table
| Aspect | Kafka | Traditional Message Queue |
|---|---|---|
| Data model | Append-only commit log | Queue of messages |
| Read tracking | Consumer offset | Per-message acknowledgement |
| Replay | Rewind offset to reprocess | Usually not possible |
| Scaling | Partitions + consumer groups | Often single consumer |
| Retention | Time or size based | Delete on read |
Common Pitfalls
- Mistake: Adding partitions after keyed data is written. Avoid: Create extra partitions upfront to keep key-to-partition mapping stable.
- Mistake: Assuming global ordering across a topic. Avoid: Use a key and rely on per-partition ordering only.
- Mistake: Putting more consumers than partitions in a group. Avoid: Match consumer count to partition count for full parallelism.
- Mistake: Treating Kafka as a queue that deletes on read. Avoid: Set retention and use offsets to replay data.
- Mistake: Forgetting replication factor for critical topics. Avoid: Use replication factor 3 and monitor in-sync replicas.
FAQs
- What happens if the email service is down? With Kafka, the order service just writes an event; the email service can catch up later from its committed offset.
- Can two consumers in the same group read the same partition? No, each partition is assigned to exactly one consumer within a group.
- How do I reprocess last week's orders? Move the consumer offset backwards and read again; data remains until retention expires.
- What is KRaft? Kafka 4's built-in Raft consensus for cluster metadata, replacing Zookeeper.
- Does Kafka delete messages after they are read? No, messages stay until the retention window or size limit is reached.
- Why is Kafka fast if it writes to disk? Sequential appends, page cache, and zero-copy avoid random disk I/O and extra memory copies.