Kafka in plain words
Most streams in this track read from Kafka, so it pays to know what Kafka actually is. It is not a queue that hands out messages and forgets them: it is a log that keeps every message, in order, for as long as you tell it to. This lesson explains topics, partitions, offsets, brokers and retention, and why that design lets many readers share one stream.
Step 4 · Structured Streaming · Bronze · Kafka basics, lesson 1 of 2
On this page
Read first: Sources and sinks (skip if you know it)
In plain words first
Think of a shop's CCTV recorder. It records everything, all the time, with a timestamp on every second. The security guard watches it live. The manager rewinds to yesterday afternoon. The insurance company asks for last Tuesday. Nobody's viewing deletes anything; the recorder only overwrites footage when it is older than, say, a week.
Kafka is that recorder for events. Applications write events into it, and Kafka keeps them in order. Any number of readers can watch live, or rewind to any point that is still kept. Reading never removes anything: each reader just remembers how far it has watched.
A log, not a queue
In a classic queue, a message is delivered to one worker and then deleted. Kafka works differently. It appends each message to the end of a log, and the message stays there until its retention time runs out, whether or not anyone has read it. That one choice explains most of Kafka:
Many readers
Rewind
Slow readers are fine
Topics, partitions and offsets
A topic is a named stream, such as orders. A topic is split into partitionspartition: In Kafka, one ordered slice of a topic, read in order. In Spark, one chunk of a DataFrame's rows, processed as one task. Learn more →, and each partition is its own log. Inside a partition, every message gets the next number, its offset: 0, 1, 2, and so on, never reused.
Write · producer
c-17. Kafka picks a partition from the message key (c-17), say partition 1, and appends the message at offset 842.Order · per partition
c-17 goes to partition 1 too, at offsets 843, 844 and on. Inside partition 1 the order is fixed. Across partitions there is no order at all: offset 842 in partition 1 says nothing about offset 842 in partition 2.Read · consumersconsumer: An application that reads events from a topic and keeps track of how far it has read. Learn more →
Expire · retention
Partitions are how Kafka scales. Each partition can live on a different server, and each one can be read by a different worker, so a topic with 12 partitions can be written and read 12 ways at once. The price is ordering: Kafka orders messages only within a partition. Ordering and out-of-order events shows how keys put the messages that need order into the same partition.
What is in a message
| Part | What it holds | Example |
|---|---|---|
| Key | Optional. Decides the partition; messages with the same key stay in order | c-17 |
| Value | The event itself, as bytes: JSON, AvroAvro: A compact binary format whose messages need a schema to be read; common on Kafka. Learn more → or Protobuf | {"order_id": "o-9", "amount": 42.5} |
| Timestamp | When the producer created it (or when Kafka stored it, if the topic is set that way) | 2026-11-28 10:15:02 |
| Headers | Optional key-value pairs, like the labels on an envelope | source=mobile-app |
Kafka does not look inside the value. To Kafka it is just bytes, which is why Spark gives you key and value as binary columns that you cast and parse yourself.
Brokers and replicas
A Kafka cluster is a group of servers called brokers. Each partition is stored on several brokers: one copy is the leader, which takes all the writes, and the others are followers that copy it. If the leader's broker dies, a follower that is fully caught up takes over, so no data is lost. How many copies to keep, and how many must confirm a write, is a production question covered in Kafka in production.
Retention decides how far back you can replay
Retention is set per topic, by time (retention.ms, 7 days by default) or by size (retention.bytes, unlimited by default). It is also your replay window. If a Spark job is down for longer than the retention, the messages it still needed are gone, and the query stops with a data-loss error (failOnDataLoss). Some topics instead keep the latest message per key forever, called compaction; that suits "current state" topics such as customer profiles.
How Spark fits in
-
Producers
apps, CDC tools
-
Kafka topic
partitions, offsets
-
Spark query
offsets in the checkpointcheckpoint: In streaming, the folder where Spark records which input it has processed and its state, so a restarted query continues where it stopped. Learn more →
-
Delta table
bronze
Spark reads a topic like any other reader: for each micro-batchmicro-batch: One small round of a stream: Spark takes the data that arrived since the last round, processes it like a little batch job, and saves its progress. Learn more → it records which offsets it will read in every partition, reads them, and marks the batch done. Because the messages stay in Kafka, a failed batch can be read again from the same offsets. The next lesson, Consumer groups and offsets, shows how ordinary Kafka readers share the work, and why Spark keeps track of offsets differently.
Common mistakes
- Treating Kafka like a queue that empties — Messages stay until retention ends. Reading does not remove them; each reader keeps its own position.
- Expecting order across a whole topic — Order holds only within a partition. Use a key so related messages share a partition.
- Keeping retention shorter than your worst outage — A job that is down longer than retention loses data. Size retention for recovery, and land raw data in Delta.
- Putting large files inside messages — Messages are meant to be small (up to about 1 MB by default). Store the file elsewhere and send its path.
Key takeaways
- Kafka is a log: messages stay until retention ends, and reading never removes them.
- A topic is split into partitions; each partition is an ordered log numbered by offsets.
- Order holds only within a partition, and the key decides the partition.
- Retention is your replay window, so size it for your longest outage.
Interview prep
THE QUESTION
"What is the difference between Kafka and a traditional message queue?"
Avoid: "Kafka is a message queue" without saying that messages stay after they are read, that order holds only within a partition, or that retention is the replay window.
Follow-ups · answer out loud, then open
Follow-up"Why does Kafka split a topic into partitions?"
Scenario"A downstream job was down for longer than the topic's retention. What happened, and how do you prevent it next time?"
Trap"Offsets are global across the topic, so offset 500 is older than offset 900."
Check yourself
3 questionsA consumer reads a message from a Kafka topic. What happens to the message?
Show the answer
It stays until the topic's retention removes it. Kafka keeps messages for the retention period; each reader only remembers its own position.
What does an offset identify?
Show the answer
A message's position within one partition. Offsets number the messages in each partition: 0, 1, 2 and so on.
Your Spark job was down for 9 days and the topic keeps 7 days. What happens on restart?
Show the answer
Messages it needed are gone; with failOnDataLoss on, the query stops. Retention does not wait for readers. The safe default stops the query so someone notices the gap.
Practise this
Interview problems on streaming, graded on hidden tests
Keep going
Up next · Kafka basics · lesson 2 of 2 · 7 min readConsumer groups and offsets
How a group shares partitions, rebalances, committed offsets and lag, and why Spark keeps its offsets in the checkpoint instead.
Related lessons
Sources and sinksStructured Streaming · 7 min read
Ordering and out-of-order events
Previous: Checkpoints and state stores
Primary sources: Kafka introduction · Kafka design · Topic configs (retention.ms, cleanup.policy) · Spark Kafka integration